Skip to content
Menu
Sri Kode
  • Home
  • Contact
Sri Kode
August 23, 2024September 2, 2024

Linux Shell Commands for DevOps

DevOps engineers rely on shell commands to manage systems, automate processes, and ensure smooth deployments. Below, we’ll explore both basic and advanced shell commands, structured into categories that are particularly useful for DevOps tasks.

Basic Shell Commands

Navigation:

1. cd – Change Directory

cd is a command used to change the current working directory. It allows you to navigate through the file system to different locations.

Examples:

  • cd ~ – Moves to your home directory
  • cd documents– Moves to specific directory named “documents”
  • cd .. – Moves up one directory (to the parent directory of current directory)
  • cd / – Moves to root directory

2. pwd – Print Working Directory

pwd is a command used to print the current working directory. It helps you determine your location within the file system.

Examples:

  • pwd – Displays the absolute path of the current directory you are in.
  • `pwd && ls – Prints the current directory and then list the contents of that directory.

3. ls – List Files and Directories

ls used to list files and directories within a specified directory. `ls [options] [directory]` provides information about the contents of a directory, including file names, permissions, size, modification time, and ownership.

Examples:

  • ls documents – Lists files in “documents” directory
  • ls -l – Displays information about each file, including permissions, owner, group, size, and modification date.
  • ls -a – Lists all files, including hidden files (those starting with a dot).
  • ls -r – Lists files in reverse alphabetical order.
  • ls -R – List files in current directory and subdirectories (Recursive Listing).

4. mkdir – Create a Directory

mkdir is a command used to create new directories.

Examples:

  • mkdir new_directory – Creates new directory with name “new_directory”
  • mkdir dir_1 dir_2 dir_3 – Creates 3 directories with “dir_1”, “dir_2”, “dir_3” names in current directory.
  • mkdir -p Documents/Work/Projects – creates the directories “Documents”, “Documents/Work”, and “Documents/Work/Projects” if they don’t already exist.
  • mkdir -pv Documents/Work/Projects – Creates the specified directories recursively and display the name of each directory as it is created (verbose mode).
  • mkdir -m 754 Projects – Creates “Projects” directory with read, write, and execute permissions for the owner, read and execute permissions for the group, and read permissions for others. (-m option sets the permissions for the new directory)

Note:

–> You need to have appropriate permissions to create directories.

–> Avoid spaces and special characters like periods, commas, and symbols when naming directories.

–> Use letters, numbers, underscores, and hyphens for directory names to ensure compatibility and avoid potential issues.

5. rmdir – Remove an Empty Directory

rmdir is a command used to remove empty directories.

Examples:

  • rmdir temp– Removes an empty directory named “temp”
  • rmdir dir_1 dir_2 dir_3 – Removes multiple empty directories in one command.
  • rmdir -pv Documents/Work/Projects– Removes parent directories if they become empty after removing child directories.

Note:

–> rmdir only works on empty directories. If a directory contains files or subdirectories, you’ll get an error.

–> Always verify that a directory is empty before using rmdir to avoid accidental data loss.

6. rm – Remove Files or Directory

rm is a command used to remove files or directories. It stands for “remove”

Example:

  • rm myfile.txt – Removes a single file.
  • rm -r documents – Removes “documents” directory and its contents recursively.
  • rm -f documents – Forcefully removes “documents” directory and its content without confirmation even any write-protected files.
  • rm -i documents – Prompts for confirmation before deleting. (Safe option)
  • rm -v file1 file2 – Displays name of each (file1, file2) deleted file.

Note:

–> Always back up important files before using the rm command.

File Manipulation:

1. cp` – Copy Files or Directories

cp is a command used to copy files or directories from one location to another. It stands for “copy”.

Examples:

  • cp file1.txt new_file.txt – Copies single file.
  • cp -r src_dir dest_dir – Recursively copies source_directory and its contents to destination_directory.
  • cp -p file.txt new_file.txt – Create an exact copy of a file, including its metadata (Preserves permissions, modification times, ownership, and other attributes)

Note:

–> -f option forcefully overwrites without prompting -i option Prompts for confirmation before overwriting.

2. mv – Move or Rename Files or Directories

mv is a command used to move files or directories from one location to another or to rename them within the same directory.

Examples:

  • mv file.txt /home/user/documents/ – Moves file.txt to the /home/user/documents/ directory.
  • mv oldname.txt newname.txt – Renames oldname.txt to newname.txt.
  • mv -i file.txt /home/user/documents/ – Prompts before overwriting any existing files in the destination.

Note:
–> mv does not create copies of files, it just moves or renames them.

3. cat – Concatenate and Display Files

cat is a command used to concatenate and display the content of files.

Examples:

  • cat file.txt` – Displays the content of file.txt.
  • cat file1.txt file2.txt – Concatenates and displays the contents of file1.txt and file2.txt.
  • cat file1.txt file2.txt > combined.txt` – Concatenates file1.txt and file2.txt and writes the output to combined.txt.

4. less – View Files Page by Page

less is a command used to view the contents of a file page by page in the terminal.

Examples:

  • less file.txt – Opens file.txt for viewing. You can navigate through the file using arrow keys or spacebar.
  • less -N file.txt – Displays line numbers while viewing file.txt.

Note:
–> less is useful for viewing large files as it doesn’t load the entire file into memory at once.

5. head – Display the First Few Lines of a File

head is a command used to display the first few lines of a file.

Examples:

  • head file.txt – Displays the first 10 lines of file.txt.
  • head -n 20 file.txt – Displays the first 20 lines of file.txt.

6. tail – Display the Last Few Lines of a File

tail is a command used to display the last few lines of a file.

Examples:

  • tail file.txt – Displays the last 10 lines of file.txt.
  • `tail -n 20 file.txt` – Displays the last 20 lines of file.txt.
  • tail -f log.txt – Continuously displays new lines added to log.txt (useful for monitoring logs in real-time)

Process Management:

1. ps – Display Information About Running Processes

ps is a command used to display information about currently running processes.

Examples:

  • ps – Displays a snapshot of the current processes.
  • ps -ef – Displays full-format listing, including parent-child relationships.
  • ps -ef | grep jenkins – To see if particular process (Eg: Jenkins) is running

2. `kill` – Terminate a Process

kill is a command used to terminate a running process by sending it a signal.

Examples:

  • kill 1234 – Sends the default SIGTERM signal to process with PID 1234 (requests to terminate).
  • kill -9 1234 – Forcefully kills the process with PID 1234 using the SIGKILL signal (orders to terminate).
  • killall process_name – Terminates all processes with the name process_name.

3. top – Display Real-Time System and Process Information

top is a command used to display a dynamic, real-time view of system processes.

Examples:

  • top – Opens an interactive view showing real-time information about system processes, including CPU and memory usage.
  • top -u username – Displays processes owned by a specific user.

Note:
–> Use top to monitor system performance and resource usage.

Permissions:

1. chmod – Change File Permissions

chmod is a command used to change the access permissions of files or directories.

Examples:

  • chmod 755 file.txt – Sets read, write, and execute permissions for the owner, and read and execute permissions for the group and others.
  • chmod +x script.sh – Adds execute permission to script.sh.
  • chmod -R 644 directory/ – Recursively sets read and write permissions for the owner, and read-only permissions for group and others for all files in the directory.

Note:
–> Permissions are represented by a three-digit number, where each digit represents owner, group, and others, respectively.

2. chown – Change File Ownership

chown is a command used to change the owner and/or group of a file or directory.

Examples:

  • chown user file.txt – Changes the owner of file.txt to user.
  • chown user:group file.txt – Changes the owner to user and the group to group for file.txt.
  • chown -R user:group directory/ – Recursively changes the owner and group for all files and directories within directory/.

Note:
–> You need superuser (root) privileges to change file ownership.

Searching:

1. grep – Search for Patterns Within Files

grep is a command used to search for specific patterns within files.

Examples:

  • grep "error" file.txt – Searches for “error” in file.txt and displays matching lines.
  • grep -i "pattern" file.txt – Case-insensitive search for “pattern” in file.txt.
  • grep -r "pattern" /path_to_directory/ – Recursively searches for “pattern” in all files within a directory.

2. `find` – Search for Files Based on Criteria

find is a command used to search for files and directories based on various criteria, such as name, size, or modification date.

Examples:

  • find /path_to_search -name "document" – Searches for files named document in the specified path.
  • find /path_to_search -type f -size +100M – Finds all files larger than 100MB in the specified path.
  • find /path/to/search -mtime -7 – Finds files modified in the last 7 days.

User Management:

1. `useradd` – Add a New User

useradd is a command used to create a new user account.

Examples:

  • useradd newuser – Creates a new user account named newuser.
  • useradd -m newuser – Creates a new user account and a home directory for newuser.
  • useradd -g devops newuser – Adds newuser to the devops group, granting permissions and privileges associated with that group.

Note:
–> After creating a user, you typically set a password for the account using passwd.

2. `passwd` – Change User Password

`passwd` is a command used to change the password for a user account.

Examples:

  • passwd – Changes the password for the current user.
  • passwd username – Changes the password for the specified user (requires root privileges).

3. usermod – Modify a User Account

usermod is a command used to modify an existing user account.

Examples:

  • usermod -l newname oldname – Changes the username from oldname to newname.
  • usermod -g primary username – Adds username to a primary group named primary.
  • usermod -aG groupname username – Adds username to an additional group named groupname.
  • usermod -L username – Locks the user account, preventing login.

4. deluser – Delete a User Account

deluser is a command used to delete a user account.

Examples:

  • deluser username – Deletes the user account named username.
  • deluser -r username – Deletes the user account and the user’s home directory `/home/username`.

Note:
–> Ensure that important data is backed up before deleting a user account.

Package Management:

1. `yum` – Yellowdog Updater Modified (RHEL/CentOS)

yum is a package management utility for Red Hat-based Linux distributions used to install, update, and remove software packages.

Examples:

  • yum install package_name – Installs the specified package.
  • yum update – Updates all installed packages to the latest version.
  • yum remove package_name – Removes the specified package.

2. dnf – Dandified Yum (RHEL/CentOS 8+)

dnf is the next-generation version of yum, used for package management in Red Hat-based Linux distributions (RHEL/CentOS 8+).

Examples:

  • dnf install package_name – Installs the specified package.
  • dnf update – Updates all installed packages to the latest version.
  • dnf remove package_name – Removes the specified package.

Note:
–> dnf has better performance and dependency management compared to yum.

Advanced Shell Commands

Text Manipulation:

1. sed – Stream Editor for Filtering and Transforming Text

sed is a powerful command-line utility for parsing and transforming text. It allows you to perform various text manipulations, such as searching, find and replace, insertion, and deletion.

Examples:

  • sed 's/old/new/' file.txt – Replaces the first occurrence of “old” with “new” in each line of file.txt.
  • sed -i 's/old/new/g' file.txt – Replaces all occurrences of “old” with “new” in file.txt and saves the changes (in-place).
  • sed -n '1,5p' file.txt` – Prints lines 1 to 5 of file.txt.
  • sed -e '/word_to_delete/d' file.txt – Deletes all lines with word in file.txt.

2. `awk` – Powerful Text Processing Tool

awk is a versatile programming language used for pattern scanning and processing. It is used to extract and manipulate text based on patterns and rules.

Examples:

  • awk '{print $1}' file.txt – Prints the first column from file.txt.
  • awk '/pattern/ {print $0}' file.txt – Prints all lines in file.txt that contain “pattern”.
  • awk '{sum += $1} END {print sum}' file.txt – Sums the values in the first column of file.txt.

Note:
–> awk is highly useful for processing and analyzing text data, especially in files with structured data.

3. cut – Extract Columns from Text

cut is a command used to extract specific sections of text from each line of a file or standard input.

Examples:

  • cut -d ':' -f 1 /etc/passwd – Extracts the first field of each line in /etc/passwd, using : as the delimiter.
  • cut -c 1-10 file.txt – Extracts the first 10 characters from each line of file.txt.
  • cut -f 2,4 -d ',' data.csv< - Extracts the second and fourth fields from data.csv, using , as the delimiter.

4. paste – Merge Lines of Files

paste is a command used to merge lines of one or more files side by side (horizontally).

Examples:

  • paste file1.txt file2.txt – Merges corresponding lines of file1.txt and file2.txt.
  • paste -d ',' file1.txt file2.txt – Merges corresponding lines of file1.txt and file2.txt using a comma as a delimiter.
  • paste -s file1.txt – Merges all lines of file1.txt into a single line (serial mode).

Note:
–> paste is useful for combining data from multiple files into a single view.

System Information:

1. df – Display Disk Space Usage

df is a command used to display the total size of each mounted file system, the amount of space used, the amount of space available, and the percentage of space used.

Examples:

  • df – Displays disk space usage for all mounted filesystems.
  • df -h – Displays disk space usage in a human-readable format (e.g., 1K, 234M, 2G).
  • df -T – Displays filesystem type along with disk space usage.

2. du – Estimate Disk Space Usage

du command estimates the disk space used by files and directories. It calculates the amount of disk space occupied by each file or directory, including the space used by subdirectories and their contents.

Examples:

  • du – Displays disk usage for each file and directory in the current directory.
  • du -h – Displays disk usage in a human-readable format.
  • du -sh * – Displays the total disk usage of each item in the current directory in a summarized format.

Note:
–> du is helpful for finding out which files or directories are consuming a lot of disk space.

3. free – Display System Memory Usage

free is a command used to display the total amount of free and used physical and swap memory in the system, as well as the buffers used by the kernel.

Examples:

  • free – Displays memory usage statistics in kilobytes.
  • free -h – Displays memory usage in a human-readable format (e.g., 1K, 234M, 2G).
  • free -m – Displays memory usage in megabytes.

4. uname – Print System Information

uname is a command used to display information about the system’s operating system and hardware.

Examples:

  • uname – Prints the system’s kernel name.
  • uname -a – Prints all available system information (kernel name, version, machine, etc.).
  • uname -r – Prints the kernel release version.

Networking:

1. ping – Test Network Connectivity

ping is a command used to test the reachability of a host on a network and measure the round-trip time for messages sent from the local host to a destination computer.

Examples:

  • ping google.com – Sends ICMP echo requests to google.com to check connectivity.
  • ping -c 4 google.com – Sends 4 ICMP echo requests to Google DNS server (google.com) and then stops.
  • ping -i 2 google.com – Sends ICMP echo requests every 2 seconds to google.com.

2. netstat – Display Network Connections

netstat is a command used to display network connections, routing tables, interface statistics, masquerade connections, and multicast memberships.

Examples:

  • netstat – Displays information about network connections, routing tables, interface statistics, and other network-related parameters.
  • netstat -a – Displays all network connections, including active, passive, and inactive connections.

3. ssh – Secure Shell for Remote Login

ssh is a command used to remotely log in to another machine securely over an encrypted network connection.

Examples:

  • ssh user@hostname – Connects to the specified host as the specified user.
  • ssh -p 2222 user@hostname – Connects to the host on port 2222.
  • ssh -i /path_to_privatekey user@hostname – Uses the specified private key for authentication.
«Previous Post
Next Post»

To dive deeper… Check out these related topics:

  • AWS
    • EC2
    • IAM
  • DevOps
    • Lifecycle
    • Linux
      • Linux Administration
      • Server Access & Management
    • Role & Responsibilities
  • Docker
  • Introduction
  • Jenkins
  • Terraform

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • Preventing Data Loss in Containers with Docker Volumes
  • Fixing “Permission Denied” Errors in Containers
  • Fixing Port Accessibility Issues in Docker Containers
  • Artifact Repositories in DevOps: Why They Matter
  • Commonly Used Maven Build Targets in Daily DevOps Workflows
©2025 Sri Kode