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:
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:
$ 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 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.
# 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.
# 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.
# 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
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 -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.
# 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.
# 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.
# -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:
$ 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.
Inside top: P (sort by CPU), M (sort by memory), q (quit)
free
Display memory usage statistics.
Shows total, used, free, shared, buffer/cache memory
df
Report disk space usage.
-h: human readable format (MB, GB)
du
Estimate file space usage.
-s: summary, -h: human readable
uptime
Show how long system has been running.
Also shows load averages: 1, 5, 15 minute
w / who
Show who is logged in and what they're doing.
Process Management Commands
Controlling Running Programs
Manage processes, start/stop services, and troubleshoot runaway processes.
ps - Process Status
Display information about running processes.
# 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).
# 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.
# 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 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
$ 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).
# 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.
# 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).
$ 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
sed 's/.*\.//' | sort | uniq -c | sort -nr
Finds all files, extracts extensions, counts occurrences
Top 10 Memory Using Processes
Monitor Multiple Logs
grep -E "(error|500|404)"
Find Largest Files
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:
- Log Investigation: Find all unique IP addresses that accessed your server today from the nginx access log.
- Disk Cleanup: Find all files larger than 100MB in /var and report their total size.
- Process Management: Write a command that kills all zombie processes.
- Backup Script: Create a one-liner that backs up the /etc directory, compresses it, and names it with today's date.
- Monitoring: Create a command that shows the 5 most CPU-intensive processes every 5 seconds.
- Security Check: Find all files in /etc with world-writeable permissions.
- 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 commandorcommand --helpare 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:
- Shell scripting: Combine commands into reusable scripts with variables, conditionals, and loops.
- Regular expressions: Master pattern matching for advanced grep, sed, and awk usage.
- Advanced text processing: Learn about
sort,uniq,paste,join, andcolumn. - Process substitution: Learn about
<(command)syntax for advanced command chaining. - Performance monitoring: Dive deeper into
vmstat,iostat,sar, anddstat. - Container-specific commands: Master
docker,docker-compose, andkubectlcommands.
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