This content originally appeared on DEV Community and was authored by Jimmy McBride
Welcome back, fellow shell wizards! đ§ââď¸ Now that weâve got you comfortable with the basics of the terminal, itâs time to really dig into the core commands youâll be using on a daily basis. These commands are like your spellbookâmaster them, and youâll be able to conjure up powerful actions with just a few keystrokes. Weâll cover the most essential commands for file manipulation, text manipulation, and permissions. And, because youâre on your fast track to shell mastery, Iâll show you how to combine them to level up your workflow.
File Manipulation Commands
File manipulation is the bread and butter of your command-line experience. Youâll be moving, copying, and deleting files constantly, so these commands will quickly become second nature.
1. Copy Files (cp
)
Want to make a copy of a file? Easy:
cp original.txt copy.txt
This creates a duplicate of original.txt
called copy.txt
. If youâre copying a directory and its contents, use the -r
flag for recursive:
cp -r source_directory/ destination_directory/
2. Move/Rename Files (mv
)
Need to move a file? The mv
command is your go-to. This moves file.txt
to a new directory:
mv file.txt /path/to/destination/
Want to rename a file? The mv
command handles that too:
mv oldname.txt newname.txt
One command, two purposesâhow efficient is that? đ
3. Delete Files (rm
)
Be careful with this one! Deleting files is easy with rm
, but rememberâthereâs no trash bin. Once itâs gone, itâs gone for good.
To delete a file:
rm file.txt
To delete a directory and all its contents, use the recursive -r
flag (and maybe take a deep breath before hitting Enter):
rm -rf folder_name/
Use this command carefully, especially when youâre working with important files. Triple-check your target!
Text Manipulation Commands
Next up: working with text files. Whether youâre extracting info or transforming text, these commands are essential to any shell wizard's toolkit.
1. View File Contents (cat
)
The cat
command lets you quickly view the contents of a file:
cat file.txt
Itâs simple, but when combined with other commands (like grep
), it becomes way more powerful.
2. Search Inside Files (grep
)
Need to find a specific line of text in a file? grep
is like a magical search tool for text files:
grep "search_term" file.txt
Itâll search through the file and show you all the lines containing "search_term"
. Combine this with cat
to search through a whole directory of files:
cat *.txt | grep "search_term"
3. Stream Editing (sed
)
Want to replace specific text within a file? sed
(stream editor) is your best friend for quick, in-place edits. Letâs say you want to replace âhelloâ with âgoodbyeâ:
sed 's/hello/goodbye/g' file.txt
This command will output the updated content to the terminal. If you want to save the changes back to the file, use the -i
flag:
sed -i 's/hello/goodbye/g' file.txt
4. Awkwardly Powerful (awk
)
Donât be put off by awk
âs intimidating syntaxâonce you get used to it, itâs a powerhouse for processing and analyzing text files. Hereâs an example that prints the first column of a file:
awk '{print $1}' file.txt
Thereâs a whole lot you can do with awk
, but weâll keep it simple for now.
Permissions and File Ownership
In the world of Linux, every file and directory has a set of permissions and an owner. Understanding how to manage these is key to keeping your system secure and organized.
1. Change File Permissions (chmod
)
Letâs break this down: permissions determine who can read, write, or execute a file. To view file permissions, use:
ls -l
The output will look something like this:
-rw-r--r-- 1 user group 4096 Sep 16 12:34 file.txt
Here, the first part (-rw-r--r--
) represents the permissions:
- The owner can read and write (
rw
). - The group and others can only read (
r--
).
To change permissions, use chmod
. If you want to give the owner execute permissions, for example:
chmod u+x file.txt
You can also use numeric mode (e.g., 755
):
chmod 755 file.txt
2. Change File Ownership (chown
)
The chown
command is used to change the owner of a file. Hereâs how youâd give ownership of file.txt
to a user named newowner
:
chown newowner file.txt
You can also change both the owner and the group:
chown newowner:newgroup file.txt
Combining Commands
Now that youâve got the basics, letâs talk about combining these commands to work faster and smarter. One of the best things about the shell is how you can pipe commands together to create powerful one-liners.
For example, hereâs how to search for a word in all .txt
files and display the results with line numbers:
grep -n "search_term" *.txt
Or, if you want to search, replace, and save changes in multiple files:
grep -l "old_text" *.txt | xargs sed -i 's/old_text/new_text/g'
Wrapping Up
By now, youâve learned the essentials of file manipulation, text processing, and managing permissions. These commands are like the spells youâll use every day in your shell wizardry. Once youâve mastered them, youâll be able to fly through tasks that would take ages with a graphical interface.
In the next part of this series, weâll dive even deeper into shell scripting and automating your workflow. Until then, keep practicingâshell mastery is all about repetition and building your muscle memory.
Join the Community!
If youâre into coding, Linux, and just love being around people who want to learn, grow, and help each other out, come hang out with us on Discord! Itâs a community of like-minded folks who share tips, talk shop, and support each other on our coding journeys. Whether you're a beginner or a seasoned pro, there's a place for you.
Click here to join the conversation!
This content originally appeared on DEV Community and was authored by Jimmy McBride
Jimmy McBride | Sciencx (2024-09-21T00:16:16+00:00) Bash-n-Dash: Fast-Track Your Way to Shell Mastery. Retrieved from https://www.scien.cx/2024/09/21/bash-n-dash-fast-track-your-way-to-shell-mastery/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.