A Beginner's Guide to Key Linux Commands for DevOps

Mohi uddinMohi uddin
6 min read

Basic File and Directory Operations

  1. ls

    • Purpose: List files and directories.

    • Usage: ls

    • Options:

      • -l (long format): Provides detailed information.

      • -a (all): Includes hidden files.

    • Example: ls -la (lists all files, including hidden ones, with detailed info).

  2. cd

    • Purpose: Change the current working directory.

    • Usage: cd [directory]

    • Example: cd /var/log (changes to the /var/log directory).

  3. pwd

    • Purpose: Print the current working directory.

    • Usage: pwd

    • Example: pwd (displays the full path of the current directory).

  4. cp

    • Purpose: Copy files or directories.

    • Usage: cp [source] [destination]

    • Options:

      • -r (recursive): Copy directories recursively.
    • Example: cp file.txt /backup/ (copies file.txt to the /backup/ directory).

  5. mv

    • Purpose: Move or rename files or directories.

    • Usage: mv [source] [destination]

    • Example: mv oldname.txt newname.txt (renames oldname.txt to newname.txt).

  6. rm

    • Purpose: Remove files or directories.

    • Usage: rm [file]

    • Options:

      • -r (recursive): Remove directories and their contents recursively.

      • -f (force): Ignore nonexistent files and never prompt.

    • Example: rm -r /temp/ (removes the /temp/ directory and its contents).

  7. mkdir

    • Purpose: Create a new directory.

    • Usage: mkdir [directory]

    • Example: mkdir newfolder (creates a new directory named newfolder).

  8. rmdir

    • Purpose: Remove an empty directory.

    • Usage: rmdir [directory]

    • Example: rmdir oldfolder (removes the empty directory oldfolder).

File Viewing and Editing

  1. cat

    • Purpose: Concatenate and display file content.

    • Usage: cat [file]

    • Example: cat file.txt (displays the content of file.txt).

  2. less

    • Purpose: View file content interactively.

    • Usage: less [file]

    • Example: less largefile.txt (opens largefile.txt for interactive viewing).

  3. head

    • Purpose: Display the beginning of a file.

    • Usage: head [file]

    • Options:

      • -n [number]: Show the first number lines.
    • Example: head -n 10 file.txt (shows the first 10 lines of file.txt).

  4. tail

    • Purpose: Display the end of a file.

    • Usage: tail [file]

    • Options:

      • -n [number]: Show the last number lines.
    • Example: tail -n 10 file.txt (shows the last 10 lines of file.txt).

  5. nano / vim / vi

    • Purpose: Text editors for modifying files.

    • Usage: nano [file], vim [file], vi [file]

    • Example: nano config.txt (opens config.txt in the Nano text editor).

File Permissions and Ownership

  1. chmod

    • Purpose: Change file permissions.

    • Usage: chmod [permissions] [file]

    • Example: chmod 755 script.sh (sets permissions to rwxr-xr-x for script.sh).

  2. chown

    • Purpose: Change file owner and group.

    • Usage: chown [owner:group] [file]

    • Example: chown user:group file.txt (changes owner to user and group to group).

  3. chgrp

    • Purpose: Change group ownership of a file.

    • Usage: chgrp [group] [file]

    • Example: chgrp admin file.txt (changes the group of file.txt to admin).

System Monitoring and Information

  1. top / htop

    • Purpose: Display real-time system processes.

    • Usage: top, htop

    • Example: top (shows a real-time view of system processes).

  2. ps

    • Purpose: Display information about running processes.

    • Usage: ps [options]

    • Options:

      • -e or -A: Show all processes.

      • -f: Full-format listing.

    • Example: ps aux (shows detailed information about all processes).

  3. df

    • Purpose: Report file system disk space usage.

    • Usage: df [options]

    • Options:

      • -h: Human-readable format (e.g., GB, MB).
    • Example: df -h (shows disk usage in a human-readable format).

  4. du

    • Purpose: Estimate file and directory space usage.

    • Usage: du [options] [directory]

    • Options:

      • -h: Human-readable format.
    • Example: du -sh /home/user (shows total disk usage of /home/user in a human-readable format).

  5. free

    • Purpose: Display memory usage.

    • Usage: free [options]

    • Options:

      • -h: Human-readable format.
    • Example: free -h (shows memory usage in a human-readable format).

  6. uptime

    • Purpose: Show how long the system has been running.

    • Usage: uptime

    • Example: uptime (displays system uptime along with load averages).

Networking

  1. ping

    • Purpose: Test network connectivity.

    • Usage: ping [host]

    • Example: ping google.com (tests connectivity to google.com).

  2. ifconfig / ip

    • Purpose: Display or configure network interfaces.

    • Usage: ifconfig, ip [options] [address]

    • Example: ifconfig (displays network interfaces and their details). ip a (shows detailed information about all network interfaces).

  3. netstat / ss

    • Purpose: Display network connections, routing tables, and interface statistics.

    • Usage: netstat [options], ss [options]

    • Options:

      • -t: TCP connections.

      • -u: UDP connections.

    • Example: ss -tuln (shows all listening TCP and UDP ports).

  4. curl

    • Purpose: Transfer data from or to a server.

    • Usage: curl [options] [URL]

    • Example: curl -I http://example.com (fetches HTTP headers from example.com).

  5. wget

Package Management

  1. apt-get / apt

    • Purpose: Manage packages on Debian-based systems (like Ubuntu).

    • Usage: apt-get [options] [command], apt [options] [command]

    • Example: apt-get install nginx (installs the nginx package).

  2. yum / dnf

    • Purpose: Manage packages on Red Hat-based systems (like CentOS or Fedora).

    • Usage: yum [options] [command], dnf [options] [command]

    • Example: yum install nginx (installs the nginx package).

Archive and Compression

  1. tar

    • Purpose: Archive files (often used with compression).

    • Usage: tar [options] [archive-file] [files]

    • Options:

      • -c: Create a new archive.

      • -x: Extract an archive.

      • -v: Verbose output.

      • -f: Specify archive file.

    • Example: tar -cvf archive.tar directory/ (creates a tarball of directory/).

  2. gzip / gunzip

    • Purpose: Compress/decompress files.

    • Usage: gzip [file], gunzip [file.gz]

    • Example: gzip file.txt (compresses file.txt to file.txt.gz).

  3. zip / unzip

    • Purpose: Compress/decompress files.

    • Usage: zip [options] [zip-file] [files], unzip [zip-file]

    • Example: zip archive.zip file1.txt file2.txt (creates a zip archive containing file1.txt and file2.txt).

Scripting and Automation

  1. bash

    • Purpose: Run shell scripts.

    • Usage: bash [script-file]

    • Example: bash script.sh (executes the script.sh file).

  2. cron / crontab

    • Purpose: Schedule tasks to run at specific times.

    • Usage:

      • crontab -e: Edit the cron table for the current user.
    • Example: crontab -e (opens the crontab file for editing).

Logs and Troubleshooting

  1. grep

    • Purpose: Search for patterns within files.

    • Usage: grep [options] [pattern] [file]

    • Options:

      • -i: Ignore case.

      • -r: Recursively search directories.

    • Example: grep -i error /var/log/syslog (searches for "error" in /var/log/syslog).

  2. awk

    • Purpose: Pattern scanning and processing.

    • Usage: awk [options] '{pattern}' [file]

    • Example: awk '{print $1}' file.txt (prints the first column of file.txt).

  3. sed

    • Purpose: Stream editor for filtering and transforming text.

    • Usage: sed [options] 'script' [file]

    • Example: sed 's/old/new/g' file.txt (replaces "old" with "new" in file.txt).

Others

  1. history

    • Purpose: View command history.

    • Usage: history

    • Example: history (lists previously executed commands).

  2. alias

    • Purpose: Create shortcuts for commands.

    • Usage: alias [name]='[command]'

    • Example: alias ll='ls -la' (creates a shortcut ll for ls -la).

These commands and tools will give you a solid foundation in Linux systems, helping you manage and automate tasks effectively in a DevOps environment.

0
Subscribe to my newsletter

Read articles from Mohi uddin directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Mohi uddin
Mohi uddin