DEV Community

Apaksh
Apaksh

Posted on

Linux Command Line: The Complete Cheat Sheet

If you work in tech, you will use the Linux command line. It's not optional. Whether you're SSH-ing into a server, debugging a Docker container, or just trying to find that one log file buried three directories deep, these commands are the bedrock of your workflow. I've distilled 100+ essential commands into this single reference. Print it, tape it to your monitor, and watch your Stack Overflow searches drop by half.

Navigation & Directory Management

pwd                        # Print working directory
cd ~                       # Go to home directory
cd -                       # Go to previous directory
cd ../..                   # Go up two levels
ls                         # List files
ls -la                     # List all files (including hidden) with details
ls -lh                     # Human-readable file sizes
ls -lt                     # Sort by modification time (newest first)
tree                       # Visual directory tree
tree -L 2                  # Tree limited to 2 levels deep
pushd /var/log             # Push directory onto stack & cd into it
popd                       # Pop directory off stack & return
Enter fullscreen mode Exit fullscreen mode

File & Directory Operations

touch file.txt             # Create empty file / update timestamp
mkdir -p a/b/c             # Create nested directories
cp file.txt backup.txt     # Copy file
cp -r dir/ newdir/         # Copy directory recursively
mv file.txt /tmp/          # Move file
mv old.txt new.txt         # Rename file
rm file.txt                # Delete file
rm -rf dir/                # Force-delete directory recursively
ln -s /path/to/file link   # Create symbolic link
find . -name "*.log"       # Find files by name
find . -mtime -7           # Files modified in last 7 days
find . -size +100M         # Files larger than 100MB
find . -name "*.tmp" -delete  # Find and delete
locate filename            # Fast file search (uses DB)
Enter fullscreen mode Exit fullscreen mode

Viewing & Editing Files

cat file.txt               # Print file to stdout
cat -n file.txt            # Print with line numbers
less file.txt              # Paginate file (q to quit)
head -n 20 file.txt        # First 20 lines
tail -n 50 file.txt        # Last 50 lines
tail -f /var/log/syslog    # Follow file in real-time
wc -l file.txt             # Count lines
diff -u file1 file2        # Unified diff (patch-friendly)
Enter fullscreen mode Exit fullscreen mode

Text Search & Processing

# grep
grep "pattern" file.txt         # Search for pattern
grep -i "pattern" file.txt      # Case-insensitive
grep -r "pattern" ./dir/        # Recursive search
grep -n "pattern" file.txt      # Show line numbers
grep -v "pattern" file.txt      # Invert match (exclude)
grep -c "pattern" file.txt      # Count matching lines
grep -A 3 "pattern" file.txt    # 3 lines after match
grep -E "pat1|pat2" file.txt    # Extended regex (OR)

# sed (Stream Editor)
sed 's/old/new/' file.txt       # Replace first occurrence per line
sed 's/old/new/g' file.txt      # Replace all occurrences
sed -i 's/old/new/g' file.txt   # Edit file in-place
sed -n '5,10p' file.txt         # Print lines 5-10
sed '/pattern/d' file.txt       # Delete lines matching pattern

# awk
awk '{print $1}' file.txt       # Print first column
awk -F: '{print $1}' /etc/passwd  # Use : as delimiter
awk '{sum+=$1} END{print sum}'  # Sum first column

# Other text tools
cut -d: -f1 /etc/passwd         # Cut first field using : delimiter
sort file.txt                   # Sort alphabetically
sort -n numbers.txt             # Sort numerically
sort -u file.txt                # Sort and remove duplicates
uniq -c file.txt                # Count duplicate lines
tr 'a-z' 'A-Z' < file.txt      # Translate lowercase to uppercase
Enter fullscreen mode Exit fullscreen mode

File Permissions & Ownership

chmod 755 file             # rwxr-xr-x
chmod 644 file             # rw-r--r--
chmod +x script.sh         # Add execute permission
chmod -R 755 dir/          # Apply recursively
chown user:group file      # Change owner and group
chown -R user:group dir/   # Recursive ownership change
umask 022                  # Default mask (files 644, dirs 755)
Enter fullscreen mode Exit fullscreen mode

Permission Reference

  • 7 (rwx) -- Read + Write + Execute
  • 6 (rw-) -- Read + Write
  • 5 (r-x) -- Read + Execute
  • 4 (r--) -- Read only
  • 0 (---) -- No permissions

Process Management

ps aux                     # All running processes
ps aux | grep nginx        # Find process by name
pgrep nginx                # Get PID by process name
top                        # Interactive process viewer
htop                       # Better interactive viewer
kill PID                   # Send SIGTERM (graceful stop)
kill -9 PID                # Send SIGKILL (force stop)
killall nginx              # Kill all processes named nginx
bg                         # Resume job in background
fg                         # Bring background job to foreground
jobs                       # List background jobs
nohup command &            # Run immune to hangups
Enter fullscreen mode Exit fullscreen mode

Signal Reference

  • SIGTERM (15) -- Graceful shutdown
  • SIGKILL (9) -- Force kill (can't catch)
  • SIGHUP (1) -- Reload config
  • SIGINT (2) -- Ctrl+C

Disk & Filesystem

df -h                      # Disk space usage (human-readable)
du -sh dir/                # Size of directory
du -sh * | sort -h         # All items sorted by size
lsblk                      # List block devices as tree
mount                      # Show all mounted filesystems
stat file.txt              # Detailed file metadata
file mystery.bin           # Identify file type
Enter fullscreen mode Exit fullscreen mode

Networking

# Connectivity
ping -c 4 google.com       # Ping exactly 4 times
traceroute google.com      # Trace network path
curl https://example.com   # Fetch URL content
curl -I https://example.com  # Fetch headers only
curl -o file.zip URL       # Download to file
wget URL                   # Download file

# Network info
ip addr                    # Show IP addresses (modern)
ip route                   # Show routing table
ss -tuln                   # Show listening ports (modern)
ss -tulnp                  # Include process names

# DNS
nslookup domain.com        # DNS lookup
dig domain.com             # Detailed DNS lookup

# SSH & Remote
ssh user@host              # Connect to host
ssh -p 2222 user@host      # Connect on custom port
scp file.txt user@host:/path  # Copy file to remote
rsync -avz src/ user@host:dst/  # Sync files (fast, resumable)
Enter fullscreen mode Exit fullscreen mode

Users & Groups

whoami                     # Current username
id                         # User ID, group ID, groups
useradd -m -s /bin/bash username   # Create user with home dir
usermod -aG docker username        # Add user to group
passwd username                    # Change user password
groupadd developers        # Create group
sudo command               # Run as root
sudo -i                    # Interactive root shell
Enter fullscreen mode Exit fullscreen mode

Package Management

apt (Debian/Ubuntu)

apt update                 # Refresh package index
apt upgrade                # Upgrade all packages
apt install nginx          # Install package
apt remove nginx           # Remove package
apt search keyword         # Search for package
Enter fullscreen mode Exit fullscreen mode

yum / dnf (RHEL/CentOS/Fedora)

dnf update                 # Update all packages
dnf install nginx          # Install package
dnf remove nginx           # Remove package
Enter fullscreen mode Exit fullscreen mode

Compression & Archives

tar -czvf archive.tar.gz dir/     # Create gzip-compressed tar
tar -xzvf archive.tar.gz          # Extract gzip tar
tar -tf archive.tar               # List contents without extracting
tar -xzvf archive.tar.gz -C /tmp/ # Extract to specific directory
zip -r archive.zip dir/           # Zip directory
unzip archive.zip                 # Extract zip
Enter fullscreen mode Exit fullscreen mode

Redirection & Pipes

command > file.txt         # Redirect stdout (overwrite)
command >> file.txt        # Redirect stdout (append)
command 2> error.log       # Redirect stderr
command 2>&1               # Merge stderr into stdout
command &> file.txt        # Redirect both stdout + stderr
command1 | command2        # Pipe stdout to next command
tee file.txt               # Write to file AND stdout

# Useful pipe combos
ps aux | grep nginx | grep -v grep
find . -name "*.log" | xargs rm -f
Enter fullscreen mode Exit fullscreen mode

Scheduling & Automation (Cron)

crontab -e                 # Edit crontab
crontab -l                 # List crontab

# Cron syntax: minute hour day month weekday command
0 2 * * *    /backup.sh         # Daily at 2:00 AM
*/15 * * * * /check.sh          # Every 15 minutes
0 9 * * 1-5  /standup.sh        # Weekdays at 9 AM
Enter fullscreen mode Exit fullscreen mode

Terminal Shortcuts

  • Ctrl+C -- Kill current process
  • Ctrl+Z -- Suspend current process
  • Ctrl+D -- Exit shell / EOF
  • Ctrl+L -- Clear screen
  • Ctrl+R -- Search command history (reverse)
  • !! -- Repeat last command
  • sudo !! -- Re-run last command with sudo

SSH Key Management

ssh-keygen -t ed25519 -C "email@example.com"   # Generate key (recommended)
ssh-copy-id user@host                           # Copy public key to remote
eval "$(ssh-agent -s)"                          # Start agent
ssh-add ~/.ssh/id_ed25519                       # Add key to agent
Enter fullscreen mode Exit fullscreen mode

System Services (systemd)

systemctl start nginx      # Start service
systemctl stop nginx       # Stop service
systemctl restart nginx    # Restart service
systemctl enable nginx     # Start on boot
systemctl status nginx     # Show service status
journalctl -u nginx -f     # Follow service logs
Enter fullscreen mode Exit fullscreen mode

Common Gotchas

  • rm -rf has no recycle bin -- it's permanent. Always double-check before running.
  • > overwrites, >> appends -- easy to lose data with the wrong one.
  • Spaces in filenames need quoting -- rm -rf My Documents removes "My" AND "Documents".
  • chmod -R 777 is almost always wrong -- massive security risk.
  • kill -9 should be a last resort -- data may not be flushed. Try kill -15 (SIGTERM) first.

Pro Tips

  • sudo !! -- Run last command as sudo
  • touch file{1..5}.txt -- Create multiple files at once
  • cmd1 && cmd2 -- Run cmd2 only if cmd1 succeeds
  • python3 -m http.server 8080 -- Quick HTTP server in current directory
  • watch -n 2 df -h -- Watch a command run every 2 seconds

If you found this useful, share it with a colleague who needs it. Subscribe for more developer resources every week.


Want the full resource?

Linux CLI Cheat Sheet — $6.99 on Gumroad

Get the complete, downloadable version. Perfect for bookmarking, printing, or sharing with your team.

Get it now on Gumroad →


If you found this useful, drop a ❤️ and follow for more developer resources every week.

Top comments (0)