DEV Community

Cover image for 25+ Essential Linux Bash Commands Every Aspiring DevOps Must Know
Dhvani
Dhvani

Posted on

25+ Essential Linux Bash Commands Every Aspiring DevOps Must Know

I'm on a journey to become a DevOps professional, so like many others, I jumped straight into Docker—it's undeniably essential. I even learned the basics check out this guide. But later, I realized just how important Linux is, so I naturally started with Bash😅.

If you want video explanation then please refer this


Table of Contents

1. Getting Started: The Terminal Environment
2. Basic Navigation and Directory Management
3. File and Directory Management
4. Viewing and Editing File Content
5. Searching and Filtering Text
6. Managing Permissions and System Commands
7. Additional Helpful Commands
8. Pipes and Redirection
9. Best Practices and Tips for Beginners
10. Conclusion


1. Getting Started: The Terminal Environment

Before diving into commands, remember that the terminal is your gateway to interacting directly with the operating system.

Quick Tips:

Shortcut Action
Ctrl + C Force-stop a running command
Ctrl + Z Pause a process
Tab Auto-complete filenames
Ctrl + R Search command history
!! Repeat the last command

2. Basic Navigation and Directory Management

ls – Listing Directory Contents

Lists files and directories within the current folder.

  • Basic usage:
  ls
Enter fullscreen mode Exit fullscreen mode

Sample Output:

  Desktop  Documents  Downloads  Music  Pictures  Public  Templates  Videos
Enter fullscreen mode Exit fullscreen mode
  • Detailed list:
  ls -l
Enter fullscreen mode Exit fullscreen mode

Sample Output:

  drwxr-xr-x  2 user user 4096 Feb 20 09:00 Desktop
  drwxr-xr-x  5 user user 4096 Feb 19 08:45 Documents
  -rw-r--r--  1 user user  123 Feb 20 08:00 file.txt
Enter fullscreen mode Exit fullscreen mode

Note: We'll understand this zombie language in the output later—don't worry!

  • Including hidden files:
  ls -a
Enter fullscreen mode Exit fullscreen mode

Sample Output:

  .  ..  .bashrc  Desktop  Documents  Downloads  Music  Pictures  Public  Templates  Videos
Enter fullscreen mode Exit fullscreen mode

Explanation:

  • -l = "long format" (detailed information).
  • -a = "all", including hidden files (files starting with a dot).

cd – Changing Directories

Navigate between directories.

  • Move into a directory:
  cd Documents
Enter fullscreen mode Exit fullscreen mode

Output:

  <your_email_or_username>:~/Documents
Enter fullscreen mode Exit fullscreen mode
  • Go back to the parent directory:
  cd ..
Enter fullscreen mode Exit fullscreen mode

(your prompt reflects the parent directory.)

  • Return to your home directory:
  cd ~
Enter fullscreen mode Exit fullscreen mode

Tip: Simply typing cd without arguments does the same.


pwd – Print Working Directory

Displays the full path of your current directory.

  • Usage:
  pwd
Enter fullscreen mode Exit fullscreen mode

Sample Output:

  /home/username/Documents
Enter fullscreen mode Exit fullscreen mode

3. File and Directory Management

mkdir – Making Directories

Create new folders.

  • Basic creation:
  mkdir new_folder
Enter fullscreen mode Exit fullscreen mode
  • Creating nested directories:
  mkdir -p Projects/2025/January
Enter fullscreen mode Exit fullscreen mode

touch – Creating or Updating Files

Creates an empty file or updates its modification timestamp.

  • Example:
  touch file.txt
Enter fullscreen mode Exit fullscreen mode

cp – Copying Files and Directories

Copies files or directories to another location.

  • Copy a file:
  cp file.txt file_backup.txt
Enter fullscreen mode Exit fullscreen mode
  ls
Enter fullscreen mode Exit fullscreen mode

Sample Output:

  file.txt  file_backup.txt  Documents  Downloads  ...
Enter fullscreen mode Exit fullscreen mode
  • Copy a directory recursively:
  cp -r folder1 folder2
Enter fullscreen mode Exit fullscreen mode
  • Copy Directory Command (Explicit Example):
  cp -r /path/to/source_directory /path/to/destination_directory
Enter fullscreen mode Exit fullscreen mode

Explanation:

The -r flag (recursive) ensures that the entire directory—including subdirectories and files—is copied.


mv – Moving and Renaming Files

Moves files between directories or renames them.

  • Move a file to another directory:
  mv file.txt Documents/
Enter fullscreen mode Exit fullscreen mode

Output:

  • The file is now inside the Documents directory.

    • Rename a file:
  mv oldname.txt newname.txt
Enter fullscreen mode Exit fullscreen mode

Output:

  • The file now appears as newname.txt in the directory.

rm – Removing Files and Directories

Deletes files or directories (use with caution—removals are irreversible by default).

  • Remove a file:
  rm file.txt
Enter fullscreen mode Exit fullscreen mode
  • Remove a directory and its contents:
  rm -r folder
Enter fullscreen mode Exit fullscreen mode
  • Force removal without prompting:
  rm -rf dangerous_folder
Enter fullscreen mode Exit fullscreen mode

4. Viewing and Editing File Content

cat – Concatenating, Displaying, and Appending File Content

The cat command is versatile and can be used to display file contents, merge files, or even append new content to an existing file.

  • Display a file’s content:
  cat file.txt
Enter fullscreen mode Exit fullscreen mode

Sample Output:

  This is a sample text file.
  It has multiple lines.
Enter fullscreen mode Exit fullscreen mode
  • Merge files into one:
  cat file1.txt file2.txt > combined.txt
Enter fullscreen mode Exit fullscreen mode

Output:

(No direct output; use cat combined.txt to view the merged content.)

  • Append text to a file interactively:
  cat >> file.txt
Enter fullscreen mode Exit fullscreen mode

Usage:

  • After running the command, your terminal will wait for you to enter text.
  • Type in your additional content.
  • When you’re finished, press Ctrl+D (EOF) to save the appended text and return to the command prompt.

Example Interaction:

  cat >> file.txt
  This is an appended line.
  And another appended line.
  [Press Ctrl+D here]
Enter fullscreen mode Exit fullscreen mode

Result:

The contents of file.txt will now include the new lines at the end:

  This is a sample text file.
  It has multiple lines.
  This is an appended line.
  And another appended line.
Enter fullscreen mode Exit fullscreen mode

less – Viewing Files Page-by-Page

Ideal for browsing large files.

  • Usage:
  less longfile.txt
Enter fullscreen mode Exit fullscreen mode
  • Behavior:
    • The content of longfile.txt is displayed one screen (or line) at a time.
    • Navigate using the arrow keys, space bar, Enter, or Page Up/Page Down.
    • Exit by pressing q.

Note: While less is more feature-rich, the more command also allows you to view text files one page at a time.

head and tail – Viewing the Beginning or End of Files

Quickly view the first or last few lines of a file.

  • Display the first 10 lines:
  head file.txt
Enter fullscreen mode Exit fullscreen mode

Sample Output:

  Line 1: Introduction to Linux
  Line 2: Basic Commands
  ...
  Line 10: Summary
Enter fullscreen mode Exit fullscreen mode
  • Display the last 10 lines:
  tail file.txt
Enter fullscreen mode Exit fullscreen mode

Sample Output:

  Line 90: Advanced Topics
  Line 91: Tips and Tricks
  ...
  Line 100: Conclusion
Enter fullscreen mode Exit fullscreen mode
  • Custom number of lines:
  head -n 5 file.txt
  tail -n 5 file.txt
Enter fullscreen mode Exit fullscreen mode

clear – Clearing the Terminal Screen

Keep your workspace uncluttered.

  • Usage:
  clear
Enter fullscreen mode Exit fullscreen mode

Output:

(Clears the terminal, leaving you with a fresh prompt.)


5. Searching and Filtering Text

grep – Searching for Patterns

Find specific text within files.

  • Basic search for a pattern:
  grep "error" log.txt
Enter fullscreen mode Exit fullscreen mode

Sample Output:

  [ERROR] 2025-02-20 09:30: An error occurred in the application.
Enter fullscreen mode Exit fullscreen mode
  • Case-insensitive search:
  grep -i "warning" log.txt
Enter fullscreen mode Exit fullscreen mode

Sample Output:

  [WARNING] 2025-02-20 09:31: This is a warning message.
Enter fullscreen mode Exit fullscreen mode
  • Recursive search in directories:
  grep -r "pattern" /path/to/directory
Enter fullscreen mode Exit fullscreen mode

Output:

(Displays matching lines from all files within the directory tree.)


find – Locating Files and Directories

Search for files by name or other attributes.

  • Find a file by name in the current directory:
  find . -name "file.txt"
Enter fullscreen mode Exit fullscreen mode

Sample Output:

  ./Documents/file.txt
Enter fullscreen mode Exit fullscreen mode
  • Search for directories starting with "config":
  find . -type d -name "config*"
Enter fullscreen mode Exit fullscreen mode

Output:

(Lists directories that match the given pattern.)


6. Managing Permissions and System Commands

chmod – Changing File Permissions

File Permissions

Modify access permissions for files and directories.

  • Example:
  chmod 755 script.sh
Enter fullscreen mode Exit fullscreen mode

Explanation & Output:

  • 755 means:
    • Owner: read, write, execute
    • Group and Others: read, execute
  • If successful, verify using:

    ls -l script.sh
    

    Sample Verification Output:

  -rwxr-xr-x 1 user user 1024 Feb 20 09:00 script.sh
Enter fullscreen mode Exit fullscreen mode

Tip: Experiment with different permission levels to learn more about access control.


sudo – Executing Commands with Superuser Privileges

Run commands that require administrative rights.

  • Example (updating package lists on a Debian-based system): (If you’re not familiar with Debian-based systems, check this out.)
  sudo apt update
Enter fullscreen mode Exit fullscreen mode

Sample Output:

  Hit:1 http://archive.ubuntu.com/ubuntu focal InRelease
  Get:2 http://archive.ubuntu.com/ubuntu focal-updates InRelease [114 kB]
  ...
  Reading package lists... Done
Enter fullscreen mode Exit fullscreen mode

Note: You’ll be prompted to enter your password.


man – Accessing Manual Pages

View detailed documentation for commands.

  • Example:
  man ls
Enter fullscreen mode Exit fullscreen mode

Output:

  • Opens the manual page for ls in a paginated view.
  • Navigate using arrow keys and press q to exit.

echo – Printing Text and Redirecting Output

Display messages or write text to files.

  • Print a message to the terminal:
  echo "Hello, Linux!"
Enter fullscreen mode Exit fullscreen mode

Sample Output:

  Hello, Linux!
Enter fullscreen mode Exit fullscreen mode
  • Write text to a file (overwriting the file):
  echo "Hello, Linux!" > greetings.txt
Enter fullscreen mode Exit fullscreen mode

Output:

(No output; verify by running cat greetings.txt.)

  • Append text to a file:
  echo "Welcome back!" >> greetings.txt
Enter fullscreen mode Exit fullscreen mode

Output:

(No output; the text is appended to greetings.txt.)


7. Additional Helpful Commands

history – Viewing Command History

Review commands you’ve recently executed.

  • Usage:
  history
Enter fullscreen mode Exit fullscreen mode

Sample Output:

  1  ls -l
  2  cd Documents
  3  cat file.txt
  ...
Enter fullscreen mode Exit fullscreen mode

alias – Creating Command Shortcuts

Simplify long commands by creating aliases.

  • Example:
  alias ll='ls -alF'
Enter fullscreen mode Exit fullscreen mode

Output:

(No output; the alias is now set for the current session. Add to your ~/.bashrc for persistence.)


df and du – Disk Space Usage

Monitor your disk usage.

  • df – Display disk free space in a human-readable format:
  df -h
Enter fullscreen mode Exit fullscreen mode

Sample Output:

  Filesystem      Size  Used Avail Use% Mounted on
  /dev/sda1        50G   20G   28G  42% /
  tmpfs           7.8G     0  7.8G   0% /dev/shm
Enter fullscreen mode Exit fullscreen mode
  • du – Show disk usage for files and directories:
  du -sh *
Enter fullscreen mode Exit fullscreen mode

Sample Output:

  4.0K    file.txt
  1.2M    Documents
  500K    Downloads
Enter fullscreen mode Exit fullscreen mode

8. Pipes and Redirection

Pipes and redirection are powerful features in Bash that allow you to control how data flows between commands and files, enabling you to build complex command sequences and automate tasks efficiently.

Pipes (|)

  • Purpose:

    A pipe (|) takes the output (stdout) of one command and sends it directly as input (stdin) to another command.

  • Example 1: Paginating Output

  ls -l | less
Enter fullscreen mode Exit fullscreen mode

Explanation:

  • ls -l produces a detailed list of files.
  • less displays this output one page at a time.

    • Example 2: Filtering Data
  dmesg | grep "error"
Enter fullscreen mode Exit fullscreen mode

Explanation:

  • dmesg outputs system messages.
  • grep "error" filters for lines containing "error".

Redirection

Redirection lets you change where the output of a command goes or where the command reads its input.

Standard Output Redirection (>)

  • Purpose: Redirects command output to a file, overwriting the file if it exists.
  • Example:
  echo "Hello, Linux!" > greetings.txt
Enter fullscreen mode Exit fullscreen mode

Explanation:

  • Writes "Hello, Linux!" to greetings.txt.

Appending Output (>>)

  • Purpose: Appends command output to the end of a file instead of overwriting it.
  • Example:
  echo "Welcome back!" >> greetings.txt
Enter fullscreen mode Exit fullscreen mode

Explanation:

  • Adds "Welcome back!" to the end of greetings.txt.

Standard Input Redirection (<)

  • Purpose: Directs a command to take input from a file.
  • Example:
  sort < unsorted.txt
Enter fullscreen mode Exit fullscreen mode

Explanation:

  • sort reads from unsorted.txt and outputs sorted results.

Combining Pipes and Redirection

You can mix pipes and redirection for advanced tasks. For example:

  • Save Filtered Output to a File:
  dmesg | grep "error" > errors.txt
Enter fullscreen mode Exit fullscreen mode

Explanation:

  • Filters dmesg output for "error" and saves it to errors.txt.

9. Best Practices and Tips for Beginners

  • Double-check before deleting: Always review what you’re deleting, especially when using recursive options like rm -rf.
  • Use man or --help: If in doubt, check the manual or use command --help for guidance.
  • Keep your system updated: Regularly run commands like sudo apt update (on Debian-based systems) to maintain software currency.
  • Experiment safely: Use a test directory or virtual machine to try out commands without risking important files.

10. Conclusion

That's all for not, this guide covers the essentials of the Linux Bash terminal—from navigation and file management to searching, permissions, and system maintenance. With these commands, sample outputs, and best practices at your fingertips, you're well on your way to mastering the Linux command line.a critical skill for any aspiring DevOps professional.

Happy coding and exploring!


Top comments (0)