DEV Community

Cover image for Mastering Bash: Essential Commands for Everyday Use
Oliver Bennet for GraphPe

Posted on • Edited on

Mastering Bash: Essential Commands for Everyday Use

Bash, the Bourne Again Shell, is a powerful tool for interacting with your Linux system. Whether you're a beginner or an experienced user, mastering essential Bash commands can significantly boost your productivity. In this article, we’ll cover some of the most useful and frequently used Bash commands, with examples to help you get started.

1. Navigating File System with cd, pwd, and ls

cd(Change Directory)
The cdcommand is used to navigate through the file system. You can move between directories easily with this command:

cd /path/to/directory
Enter fullscreen mode Exit fullscreen mode

To go back to the previous directory:

cd -
Enter fullscreen mode Exit fullscreen mode

pwd(Print Working Directory)
If you ever get lost in the terminal and want to know where you are, use pwd:

pwd
Enter fullscreen mode Exit fullscreen mode

It prints the full path of your current directory.

ls(List Directory Contents)
Want to see what files and directories are inside the current directory? Use ls:

ls
Enter fullscreen mode Exit fullscreen mode

To display more details, such as file permissions, size, and modification date, use the -l flag:

ls -l
Enter fullscreen mode Exit fullscreen mode

2. Managing Files with cp, mv, and rm

cp(Copy Files or Directories)
The cpcommand is used to copy files or directories from one location to another:

cp file1.txt /path/to/destination/
Enter fullscreen mode Exit fullscreen mode

To copy a directory and its contents, use the -r (recursive) option:

cp -r /path/to/source/ /path/to/destination/
Enter fullscreen mode Exit fullscreen mode

mv(Move or Rename Files)
The mv command can either move files to a new location or rename them:

mv oldfile.txt newfile.txt  # Renames file
mv file.txt /new/location/  # Moves file to new directory
Enter fullscreen mode Exit fullscreen mode

rm(Remove Files or Directories)
To delete files, use the rmcommand:

rm file.txt
Enter fullscreen mode Exit fullscreen mode

For directories and their contents, use the -r option:

rm -r /path/to/directory/
Enter fullscreen mode Exit fullscreen mode

Be careful when using rm, as deleted files are not moved to a trash bin.

3. Viewing and Editing Files with cat, less, and nano

cat(Concatenate and Display Files)
The cat command displays the content of a file directly in the terminal:

cat file.txt
Enter fullscreen mode Exit fullscreen mode

To concatenate multiple files and display them, list all files:

cat file1.txt file2.txt
Enter fullscreen mode Exit fullscreen mode

less(View File Content One Page at a Time)
The less command lets you view large files page by page, making navigation easier:

less largefile.txt
Enter fullscreen mode Exit fullscreen mode

You can scroll through the file using arrow keys and press q to quit.

nano (Simple Text Editor)
For quick file editing, nanois a lightweight and user-friendly command-line text editor:

nano file.txt
Enter fullscreen mode Exit fullscreen mode

Once you're done editing, press Ctrl + X to exit, Y to save changes, and Enter to confirm.

4. Searching with grepand find

grep(Search Within Files)
The grepcommand searches for a specific pattern within files. For example, to search for the word "error" in a log file:

grep "error" logfile.txt
Enter fullscreen mode Exit fullscreen mode

To ignore case sensitivity, add the -i option:

grep -i "error" logfile.txt
Enter fullscreen mode Exit fullscreen mode

find(Search for Files or Directories)
To locate files or directories on your system, use the findcommand. For example, to search for a file named document.txt:

find / -name "document.txt"
Enter fullscreen mode Exit fullscreen mode

The findcommand is powerful and can be combined with other options to search by date, size, or file type.

5. Viewing System Information with df, du, and top

df (Disk Free Space)
The df command displays the amount of available disk space on file systems:

df -h
Enter fullscreen mode Exit fullscreen mode

The -h option shows the output in a human-readable format (KB, MB, GB).

du (Disk Usage)
If you need to know how much space a directory is using, ducan help:

du -sh /path/to/directory/
Enter fullscreen mode Exit fullscreen mode

This command provides a summary of the directory’s size.

top(Monitor System Processes)
The topcommand gives you a real-time view of running processes and their resource usage:

top
Enter fullscreen mode Exit fullscreen mode

You can monitor CPU, memory usage, and even kill processes directly from this interface.

Output of  raw `top` endraw

Conclusion

These essential Bash commands are your gateway to mastering the Linux command line. By practicing these commands and incorporating them into your daily workflow, you’ll become more efficient and capable of handling system tasks with ease. Bash is vast and powerful, and this guide is just the start of your journey into command-line mastery.

🔗 Support my Work

▶️ Support by Subscribing my YouTube
▶️ Explore more open-source tutorials on my website
▶️ Follow me on X
Buy me a Coffee

Top comments (0)