Saturday, December 13, 2025

Basic Linux Commands for DevOps

Basic Linux Commands - DevOps Essentials

Basic Linux Commands for DevOps

Published: December 2025 | Topic: Essential Linux Commands for DevOps Engineers

Mastering Linux commands is fundamental for DevOps work. Whether you're deploying applications, troubleshooting servers, or automating tasks, these commands form the foundation of your daily workflow. This guide covers essential commands categorized by their use case.

Why Command Line Proficiency Matters for DevOps

While GUI tools exist, the command line offers precision, speed, and automation capabilities that are essential for DevOps. Most servers run headless (without GUI), and automation scripts rely on command-line tools. Understanding these commands allows you to:

  • Quickly diagnose and troubleshoot server issues
  • Automate deployment and maintenance tasks
  • Efficiently manage multiple servers simultaneously
  • Work with container technologies like Docker and orchestration tools like Kubernetes
  • Parse and analyze log files and system data

⚠️ Important: Understanding Command Structure

Linux commands typically follow this pattern:

command [options] [arguments]

Options (flags) modify command behavior (usually start with - or --)
Arguments specify what the command operates on (files, directories, etc.)
Man pages are your best friend: Use man command for detailed documentation

Navigation Commands

File Operations Commands

Creating, Copying, Moving, and Removing Files

These commands are fundamental for managing files and directories on Linux systems.

Command Description Common Options DevOps Example
touch Create empty file or update timestamp -a (access time), -m (mod time) touch /tmp/deploy.lock
cp Copy files and directories -r (recursive), -v (verbose), -i (interactive) cp -r app/ backup/app-backup
mv Move or rename files -v (verbose), -i (interactive) mv app.conf app.conf.bak
rm Remove files/directories -r (recursive), -f (force), -v (verbose) rm -rf /tmp/old-logs/
mkdir Create directories -p (create parent dirs), -v (verbose) mkdir -p /opt/myapp/{logs,config,cache}
rmdir Remove empty directories -p (remove parent dirs) rmdir empty-directory

⚠️ Danger Zone: The rm -rf Command

rm -rf is powerful and dangerous. It recursively forces deletion without confirmation. Always double-check your path before using it:

# ❌ DANGEROUS - Could delete everything if run as root!
$ rm -rf /

# ✅ Safer approach - Use pwd to confirm location first
$ pwd
/tmp/old-backups
$ rm -rf *

File Viewing and Editing Commands

Examining and Modifying File Contents

DevOps engineers constantly need to view logs, check configurations, and edit files.

cat - Concatenate Files

Display file contents, combine files, or create new files.

$ cat config.yml
$ cat file1 file2 > combined.txt
$ cat > newfile.txt
# Type content, press Ctrl+D to save

less / more - Page Through Files

View large files one screen at a time.

$ less /var/log/syslog
# Navigation: Space=next page, b=prev page, q=quit
$ grep "error" app.log | less
# Pipe output to less for easier reading

head / tail - View File Beginnings or Ends

Perfect for checking logs and large files.

$ head -20 app.log
# First 20 lines
$ tail -f /var/log/nginx/access.log
# Follow (watch) log file in real-time
$ tail -100 app.log
# Last 100 lines (useful for recent errors)

nano / vim - Text Editors

Edit configuration files and scripts.

$ nano /etc/nginx/nginx.conf
# Simple editor (Ctrl+O save, Ctrl+X exit)
$ vim /etc/hosts
# Powerful editor (i=insert, Esc, :wq=save & quit)

DevOps Tip: Learn at least basic vim for editing files on remote servers.

File Permissions and Ownership

Managing Access Control

Linux uses a permission system to control file access. Understanding this is crucial for security and troubleshooting "Permission denied" errors.

$ ls -l
# Sample output:
-rwxr-xr-- 1 user group 2048 Dec 1 10:00 script.sh
# │ │ │ │ │ │ │ │
# │ │ │ │ │ │ │ └── Filename
# │ │ │ │ │ │ └── Size in bytes
# │ │ │ │ │ └── Group owner
# │ │ │ │ └── User owner
# │ │ │ └── Number of links
# │ │ └── Other users permissions (r-- = read only)
# │ └── Group permissions (r-x = read & execute)
# └── User permissions (rwx = read, write, execute)

chmod - Change Mode

Change file permissions using symbolic or numeric notation.

$ chmod +x script.sh
# Add execute permission for all users
$ chmod 644 config.conf
# rw-r--r-- (owner: read/write, others: read)
$ chmod u=rwx,g=rx,o= script.sh
# Symbolic notation: user=rwx, group=rx, others=none

chown - Change Owner

Change file owner and group.

$ chown user:group file.txt
$ chown www-data:www-data /var/www/html
# Common web server ownership
$ chown -R nginx:nginx /etc/nginx
# -R: recursive (apply to all files/dirs)

chgrp - Change Group

Change only the group ownership.

$ chgrp developers app/ $ chgrp -R adm /var/log

Text Processing Commands

Powerful Tools for Log Analysis and Data Extraction

These commands are essential for parsing logs, extracting information, and transforming text data.

grep - Global Regular Expression Print

Search for patterns in text. One of the most used DevOps commands.

$ grep "error" /var/log/syslog
$ grep -r "database" /etc/
# -r: recursive search
$ grep -i "warning" app.log
# -i: case-insensitive
$ grep -v "DEBUG" app.log
# -v: invert match (show lines NOT containing)
$ tail -f app.log | grep "500"
# Monitor log for HTTP 500 errors

awk - Pattern Scanning and Processing

Powerful text processing language. Great for extracting columns from structured text.

$ ps aux | awk '{print $1, $4}'
# Print user and CPU% columns
$ awk -F: '{print $1}' /etc/passwd
# -F: set field separator to colon
$ df -h | awk '/\/$/ {print "Root usage: " $5}'
# Show root partition usage percentage

sed - Stream Editor

Filter and transform text. Excellent for find/replace operations.

$ sed 's/old/new/g' file.txt
# Replace all 'old' with 'new'
$ sed -i 's/127.0.0.1/localhost/g' config.conf
# -i: edit file in-place
$ sed '/^#/d' config.conf
# Delete all comment lines (starting with #)
$ sed -n '10,20p' app.log
# Print only lines 10-20

cut - Remove Sections

Extract columns or fields from lines of text.

$ cut -d: -f1 /etc/passwd
# -d: delimiter, -f: field (get usernames)
$ echo "one,two,three" | cut -d, -f2
# Output: two

Real-World Example: Log Analysis Pipeline

Combining these commands creates powerful analysis pipelines:

# Find all 500 errors in nginx log, count by IP address
$ grep " 500 " /var/log/nginx/access.log | \
    awk '{print $1}' | \
    sort | uniq -c | sort -nr

# Output shows IP addresses with most 500 errors
# 45 192.168.1.100
# 12 192.168.1.101
# 3 192.168.1.102

System Monitoring Commands

Checking System Health and Resources

Monitoring system performance is critical for maintaining healthy servers and applications.

top / htop

Interactive process viewer showing CPU, memory, and process info.

top
htop

Inside top: P (sort by CPU), M (sort by memory), q (quit)

free

Display memory usage statistics.

free -h

Shows total, used, free, shared, buffer/cache memory

df

Report disk space usage.

df -h

-h: human readable format (MB, GB)

du

Estimate file space usage.

du -sh /var/log

-s: summary, -h: human readable

uptime

Show how long system has been running.

uptime

Also shows load averages: 1, 5, 15 minute

w / who

Show who is logged in and what they're doing.

w
who

Process Management Commands

Controlling Running Programs

Manage processes, start/stop services, and troubleshoot runaway processes.

ps - Process Status

Display information about running processes.

$ ps aux
# Show all processes for all users
$ ps -ef | grep nginx
# Find nginx processes
$ ps -p 1234 -o pid,ppid,cmd,%cpu,%mem
# Show specific process with custom format

kill - Terminate Processes

Send signals to processes (default: SIGTERM for graceful shutdown).

$ kill 1234
# Gracefully terminate process 1234
$ kill -9 1234
# Force kill (SIGKILL) - use as last resort
$ killall nginx
# Kill all processes named nginx
$ pkill -f "python app.py"
# Kill processes by full command

jobs / fg / bg - Job Control

Manage foreground and background jobs.

$ long-running-command &
# Run command in background
$ jobs
# List background jobs
$ fg %1
# Bring job 1 to foreground
$ Ctrl+Z
# Suspend foreground job
$ bg
# Resume suspended job in background

systemctl - Systemd Control

Manage services on modern Linux systems.

$ systemctl status nginx
$ systemctl start nginx
$ systemctl stop nginx
$ systemctl restart nginx
$ systemctl enable nginx
# Start on boot
$ systemctl reload nginx
# Reload config without stopping

Network Commands

Diagnosing Network Issues

Essential for troubleshooting connectivity, checking ports, and monitoring network traffic.

Command Purpose Common Usage
ping Test network connectivity ping -c 4 google.com
curl Transfer data from URLs curl -I http://example.com
wget Download files from web wget https://example.com/file.tar.gz
netstat / ss Network statistics ss -tulpn (show listening ports)
nslookup / dig DNS lookup dig example.com
traceroute / mtr Trace network path traceroute google.com
ifconfig / ip Network interface config ip addr show
nc (netcat) Network swiss army knife nc -zv host 80 (test port)

Checking Open Ports and Connections

# Show all listening TCP ports with process names
$ ss -tulpn

# Check if port 443 is open on remote server
$ nc -zv example.com 443

# Monitor network connections in real-time
$ watch -n 1 'netstat -an | grep ESTABLISHED | wc -l'

# Download file with curl and follow redirects
$ curl -L -o output.tar.gz https://example.com/download

Compression and Archiving

Working with Compressed Files

Essential for creating backups, distributing files, and managing disk space.

tar - Tape Archive

Create and extract archive files (.tar, .tar.gz, .tar.bz2).

$ tar -czvf backup.tar.gz /path/to/directory
# c=create, z=gzip, v=verbose, f=file
$ tar -xzvf backup.tar.gz
# x=extract
$ tar -tzf backup.tar.gz
# t=list contents

gzip / gunzip

Compress/decompress .gz files.

$ gzip largefile.log
# Creates largefile.log.gz
$ gunzip largefile.log.gz
# Extracts to largefile.log
$ zcat compressed.log.gz | grep "error"
# Search compressed files without extracting

zip / unzip

Create and extract .zip archives (common on Windows).

$ zip -r archive.zip directory/
$ unzip archive.zip
$ unzip -l archive.zip
# List contents without extracting

Powerful Command Combinations

The Real Power: Combining Commands with Pipes

The pipe operator (|) connects commands, passing output of one as input to another. This allows complex data processing pipelines.

Counting Files by Type

$ find . -type f | \
sed 's/.*\.//' | sort | uniq -c | sort -nr

Finds all files, extracts extensions, counts occurrences

Top 10 Memory Using Processes

$ ps aux --sort=-%mem | head -11

Monitor Multiple Logs

$ tail -f /var/log/nginx/*.log | \
grep -E "(error|500|404)"

Find Largest Files

$ find / -type f -size +100M 2>/dev/null | \
xargs du -h | sort -rh | head -20

Essential Keyboard Shortcuts

Master these keyboard shortcuts to work faster in the terminal:

Shortcut Action Description
Ctrl+C Interrupt/Kill Stop current command
Ctrl+D EOF/Exit End of file or exit shell
Ctrl+Z Suspend Pause foreground job
Ctrl+R Reverse Search Search command history
Ctrl+A Beginning of Line Move cursor to line start
Ctrl+E End of Line Move cursor to line end
Ctrl+U Cut to Beginning Delete from cursor to start
Ctrl+K Cut to End Delete from cursor to end
Ctrl+Y Paste Paste previously cut text
Tab Auto-complete Complete commands/filenames
↑ / ↓ History Navigation Scroll through command history

Practice Exercises for DevOps Scenarios

Try these real-world exercises to build your skills:

  1. Log Investigation: Find all unique IP addresses that accessed your server today from the nginx access log.
  2. Disk Cleanup: Find all files larger than 100MB in /var and report their total size.
  3. Process Management: Write a command that kills all zombie processes.
  4. Backup Script: Create a one-liner that backs up the /etc directory, compresses it, and names it with today's date.
  5. Monitoring: Create a command that shows the 5 most CPU-intensive processes every 5 seconds.
  6. Security Check: Find all files in /etc with world-writeable permissions.
  7. Network Test: Check if port 443 is open on 5 different servers and report results.

Key Takeaways for DevOps Engineers

  • Master the basics first: Navigation, file operations, and text processing commands form 80% of daily work.
  • Learn to chain commands: Pipes (|) turn simple commands into powerful data processing pipelines.
  • Use man pages: man command or command --help are always available reference.
  • Practice safe commands: Always verify paths before using destructive commands like rm -rf.
  • Build muscle memory: Regular practice makes command usage second nature.
  • Document your aliases: Create aliases for frequently used complex commands.
  • Understand exit codes: Commands return 0 on success, non-zero on failure - crucial for scripting.

Start with mastering 20 essential commands, then gradually expand your toolkit as you encounter new scenarios in your DevOps work.

Next Steps in Your Learning Journey

Once you're comfortable with these basic commands, explore:

  1. Shell scripting: Combine commands into reusable scripts with variables, conditionals, and loops.
  2. Regular expressions: Master pattern matching for advanced grep, sed, and awk usage.
  3. Advanced text processing: Learn about sort, uniq, paste, join, and column.
  4. Process substitution: Learn about <(command) syntax for advanced command chaining.
  5. Performance monitoring: Dive deeper into vmstat, iostat, sar, and dstat.
  6. Container-specific commands: Master docker, docker-compose, and kubectl commands.

Remember: Proficiency comes from practice. Try to use the command line for tasks you'd normally do with GUI tools. Over time, you'll build speed and efficiency that makes you more productive in your DevOps role.

No comments:

Post a Comment

Linux Security & Permissions for DevOps

Linux Security & Permissions - DevOps Security Guide Linux Security & Permissions ...