Top 10 Bash Commands You Must Know Master commands (With Examples)

1. ls – List Directory Contents The ls command is used to view files and folders in the current directory. Examples: bashCopyEditls # Basic listing ls -l # Long listing with details (permissions, size, date) ls -a # Includes hidden files (those starting with .) ls -lh # Long listing with human-readable file sizes Use Case: Check file permissions, ownership, and whether hidden config files exist in a directory. 2. cd – Change Directory The cd command lets you move between folders. Examples: bashCopyEditcd /home/user/Documents # Move to Documents cd .. # Go up one directory cd # Go to your home directory Use Case: Navigate the file system to perform tasks in the right folder. 3. mkdir – Create Directories Use mkdir to create one or more folders. Examples: bashCopyEditmkdir myfolder # Create a single folder mkdir -p a/b/c # Create nested folders Use Case: Organize project files or prepare folder structures for applications. 4. rm – Remove Files and Folders Deletes files or directories. Be very careful with this one. Examples: bashCopyEditrm file.txt # Delete a file rm -r myfolder # Delete a folder recursively rm -rf myfolder # Force delete folder without confirmation ⚠️ rm -rf / can destroy your system. Always double-check the path. 5. cat – View File Content The cat command reads and displays file contents. Examples: bashCopyEditcat file.txt # Show entire file cat file1.txt file2.txt # Concatenate and show both files Use Case: Quickly check the contents of configuration or log files. 6. grep – Search Text in Files grep filters lines that match a pattern. It’s incredibly powerful for searching logs and files. Examples: bashCopyEditgrep "error" logfile.txt # Find lines with "error" grep -i "error" logfile.txt # Case-insensitive match grep -r "TODO" . # Recursively search current dir grep -v "success" results.txt # Show lines NOT containing "success" Use Case: Find specific error messages in huge logs or search for keywords in code. 7. echo – Display Text or Variables Used to print messages or write content to files. Examples: bashCopyEditecho "Hello, World" # Print a simple message echo $HOME # Print the value of the HOME variable echo "Line" >> file.txt # Append a line to a file Use Case: Debug scripts, print values, or create quick files. 8. chmod – Change File Permissions chmod modifies who can read, write, or execute a file. Examples: bashCopyEditchmod +x script.sh # Make script executable chmod 755 file.sh # rwxr-xr-x (owner can do everything, others can read/execute) Use Case: Give execute permissions to a shell script so it can be run. 9. ps – View Active Processes Shows the currently running processes. Examples: bashCopyEditps # Shows your current processes ps -ef # Show all processes in full detail ps -ef | grep nginx # Filter and find specific process Use Case: Check if a program is running, find its PID, or troubleshoot issues. 10. sudo – Execute Commands as Root sudo gives you superuser (root) access — required for administrative tasks. Examples: bashCopyEditsudo apt update # Update packages (Debian/Ubuntu) sudo rm -rf /root/folder # Delete a folder owned by root Use Case: Install software, modify system configs, or perform root-level tasks. Conclusion : The article provides a comprehensive overview of the top 10 essential Bash commands, each with examples and use cases. Here's a summary of the key points: ls – List Directory Contents: Useful for viewing files and folders, checking file permissions, ownership, and hidden files. cd – Change Directory: Allows navigation between directories to perform tasks in the correct location. mkdir – Create Directories: Helps in organizing project files and creating necessary folder structures. rm – Remove Files and Folders: Used to delete files or directories, with a caution to use it carefully to avoid system damage. cat – View File Content: Displays file contents, useful for quickly checking configuration or log files. grep – Search Text in Files: Filters lines matching a pattern, ideal for searching logs and code for specific keywords. echo – Display Text or Variables: Prints messages or writes content to files, useful for debugging a

May 4, 2025 - 09:36
 0
Top 10 Bash Commands You Must Know Master commands (With Examples)

1. ls – List Directory Contents

The ls command is used to view files and folders in the current directory.

Examples:

bashCopyEditls             # Basic listing
ls -l          # Long listing with details (permissions, size, date)
ls -a          # Includes hidden files (those starting with .)
ls -lh         # Long listing with human-readable file sizes

Use Case:

Check file permissions, ownership, and whether hidden config files exist in a directory.

2. cd – Change Directory

The cd command lets you move between folders.

Examples:

bashCopyEditcd /home/user/Documents     # Move to Documents
cd ..                       # Go up one directory
cd                         # Go to your home directory

Use Case:

Navigate the file system to perform tasks in the right folder.

3. mkdir – Create Directories

Use mkdir to create one or more folders.

Examples:

bashCopyEditmkdir myfolder              # Create a single folder
mkdir -p a/b/c              # Create nested folders

Use Case:

Organize project files or prepare folder structures for applications.

4. rm – Remove Files and Folders

Deletes files or directories. Be very careful with this one.

Examples:

bashCopyEditrm file.txt                 # Delete a file
rm -r myfolder              # Delete a folder recursively
rm -rf myfolder             # Force delete folder without confirmation

⚠️ rm -rf / can destroy your system. Always double-check the path.

5. cat – View File Content

The cat command reads and displays file contents.

Examples:

bashCopyEditcat file.txt                # Show entire file
cat file1.txt file2.txt     # Concatenate and show both files

Use Case:

Quickly check the contents of configuration or log files.

6. grep – Search Text in Files

grep filters lines that match a pattern. It’s incredibly powerful for searching logs and files.

Examples:

bashCopyEditgrep "error" logfile.txt           # Find lines with "error"
grep -i "error" logfile.txt        # Case-insensitive match
grep -r "TODO" .                   # Recursively search current dir
grep -v "success" results.txt      # Show lines NOT containing "success"

Use Case:

Find specific error messages in huge logs or search for keywords in code.

7. echo – Display Text or Variables

Used to print messages or write content to files.

Examples:

bashCopyEditecho "Hello, World"               # Print a simple message
echo $HOME                        # Print the value of the HOME variable
echo "Line" >> file.txt           # Append a line to a file

Use Case:

Debug scripts, print values, or create quick files.

8. chmod – Change File Permissions

chmod modifies who can read, write, or execute a file.

Examples:

bashCopyEditchmod +x script.sh               # Make script executable
chmod 755 file.sh                # rwxr-xr-x (owner can do everything, others can read/execute)

Use Case:

Give execute permissions to a shell script so it can be run.

9. ps – View Active Processes

Shows the currently running processes.

Examples:

bashCopyEditps                             # Shows your current processes
ps -ef                         # Show all processes in full detail
ps -ef | grep nginx            # Filter and find specific process

Use Case:

Check if a program is running, find its PID, or troubleshoot issues.

10. sudo – Execute Commands as Root

sudo gives you superuser (root) access — required for administrative tasks.

Examples:

bashCopyEditsudo apt update               # Update packages (Debian/Ubuntu)
sudo rm -rf /root/folder      # Delete a folder owned by root

Use Case:

Install software, modify system configs, or perform root-level tasks.

Conclusion :

The article provides a comprehensive overview of the top 10 essential Bash commands, each with examples and use cases. Here's a summary of the key points:

  1. ls – List Directory Contents: Useful for viewing files and folders, checking file permissions, ownership, and hidden files.

  2. cd – Change Directory: Allows navigation between directories to perform tasks in the correct location.

  3. mkdir – Create Directories: Helps in organizing project files and creating necessary folder structures.

  4. rm – Remove Files and Folders: Used to delete files or directories, with a caution to use it carefully to avoid system damage.

  5. cat – View File Content: Displays file contents, useful for quickly checking configuration or log files.

  6. grep – Search Text in Files: Filters lines matching a pattern, ideal for searching logs and code for specific keywords.

  7. echo – Display Text or Variables: Prints messages or writes content to files, useful for debugging and creating files.

  8. chmod – Change File Permissions: Modifies file permissions, allowing scripts to be executable.

  9. ps – View Active Processes: Shows running processes, useful for checking program status and troubleshooting.

  10. sudo – Execute Commands as Root: Provides superuser access for administrative tasks like software installation and system configuration.

These commands are fundamental for anyone working with the command line, providing essential functionality for file management, system navigation, and administrative tasks.