Linux Commands Cheat Sheet: 100+ Essential Commands

6 min read
Beginner Linux Commands Terminal Cheat Sheet

Quick Answer: ls lists files. cd /path changes directory. cat file shows content. cp src dest copies. mv src dest moves/renames. rm file deletes. sudo command runs as root. grep pattern file searches text. chmod 755 file sets permissions.

Navigation and Files

# Where am I?
pwd

# List files
ls                         # Basic
ls -la                     # Detailed with hidden files
ls -lh                     # Human-readable sizes
ls -lt                     # Sort by modification time
ls -lS                     # Sort by size

# Change directory
cd /var/log                # Absolute path
cd ..                      # Parent directory
cd ~                       # Home directory
cd -                       # Previous directory

# Create
mkdir mydir                # Create directory
mkdir -p a/b/c             # Create nested directories
touch file.txt             # Create empty file

# Copy
cp file.txt backup.txt     # Copy file
cp -r dir/ newdir/         # Copy directory recursively

# Move / Rename
mv file.txt newname.txt    # Rename
mv file.txt /other/dir/    # Move

# Delete
rm file.txt                # Delete file
rm -r dir/                 # Delete directory
rm -rf dir/                # Force delete (no prompt)

# Find files
find / -name "*.log"                  # By name
find /home -size +100M                # Larger than 100MB
find . -mtime -7                      # Modified in last 7 days
find . -type f -name "*.tmp" -delete  # Find and delete

Viewing and Editing Files

# View file
cat file.txt               # Entire file
less file.txt              # Scrollable (q to quit)
head -20 file.txt          # First 20 lines
tail -20 file.txt          # Last 20 lines
tail -f /var/log/syslog    # Follow (live)

# Edit
nano file.txt              # Simple editor
vim file.txt               # Power editor

# Word/line count
wc -l file.txt             # Line count
wc -w file.txt             # Word count
wc -c file.txt             # Byte count

# File type
file document.pdf

Text Processing

# Search text
grep "error" logfile.txt              # Basic search
grep -i "error" logfile.txt           # Case insensitive
grep -r "TODO" src/                   # Recursive in directory
grep -n "error" logfile.txt           # Show line numbers
grep -c "error" logfile.txt           # Count matches
grep -v "debug" logfile.txt           # Invert (exclude matches)
grep -E "error|warning" logfile.txt   # Regex (multiple patterns)

# Sort
sort file.txt                         # Alphabetical
sort -n file.txt                      # Numerical
sort -r file.txt                      # Reverse
sort -u file.txt                      # Remove duplicates
sort -t: -k3 -n /etc/passwd           # Sort by field

# Unique / Deduplicate
uniq file.txt                         # Remove adjacent duplicates
sort file.txt | uniq -c               # Count occurrences
sort file.txt | uniq -d               # Show only duplicates

# Cut (extract columns)
cut -d: -f1 /etc/passwd               # First field (delimiter :)
cut -d, -f2,3 data.csv                # Fields 2 and 3

# Sed (find and replace)
sed 's/old/new/' file.txt             # Replace first per line
sed 's/old/new/g' file.txt            # Replace all
sed -i 's/old/new/g' file.txt         # In-place edit
sed -n '10,20p' file.txt              # Print lines 10-20

# Awk (column processing)
awk '{print $1}' file.txt             # First column
awk -F: '{print $1, $3}' /etc/passwd  # Custom delimiter
awk '$3 > 100' data.txt               # Filter by condition
awk '{sum+=$1} END {print sum}' nums  # Sum a column

# Pipes (combine commands)
cat access.log | grep "404" | wc -l   # Count 404 errors
ps aux | sort -k4 -rn | head -10      # Top 10 by memory

Permissions

# View permissions
ls -la

# Change permissions
chmod 755 script.sh           # rwxr-xr-x
chmod 644 file.txt            # rw-r--r--
chmod +x script.sh            # Add execute
chmod -w file.txt             # Remove write
chmod -R 755 dir/             # Recursive

# Change owner
chown user:group file.txt
chown -R www-data:www-data /var/www/

# Common permission numbers
# 755 = rwxr-xr-x  (directories, scripts)
# 644 = rw-r--r--  (regular files)
# 600 = rw-------  (private files, SSH keys)
# 700 = rwx------  (private directories)
# 777 = rwxrwxrwx  (NEVER use this)

Process Management

# View processes
ps aux                        # All processes
ps aux | grep nginx           # Find specific process
top                           # Interactive process monitor
htop                          # Better interactive monitor

# Kill processes
kill PID                      # Graceful stop
kill -9 PID                   # Force kill
killall nginx                 # Kill by name
pkill -f "python app.py"     # Kill by command pattern

# Background/foreground
command &                     # Run in background
jobs                          # List background jobs
fg %1                         # Bring job 1 to foreground
bg %1                         # Resume job 1 in background
nohup command &               # Keep running after logout

# Service management (systemd)
systemctl start nginx
systemctl stop nginx
systemctl restart nginx
systemctl status nginx
systemctl enable nginx        # Start on boot
systemctl disable nginx
systemctl list-units --type=service

Networking

# IP address
ip addr show                  # All interfaces
ip addr show eth0             # Specific interface
hostname -I                   # Quick IP list

# Connectivity
ping google.com
ping -c 4 google.com          # 4 pings only
traceroute google.com
mtr google.com                # Better traceroute

# DNS
dig google.com
nslookup google.com
host google.com

# Ports and connections
ss -tlnp                      # Listening TCP ports
ss -ulnp                      # Listening UDP ports
ss -tunap                     # All connections
netstat -tlnp                 # Older syntax

# Download
curl -O https://example.com/file.zip         # Download file
curl -sI https://example.com                 # Headers only
wget https://example.com/file.zip            # Download
wget -q -O - https://example.com/api         # Output to stdout

# Transfer files
scp file.txt user@server:/path/              # Upload
scp user@server:/path/file.txt ./            # Download
rsync -avz dir/ user@server:/path/           # Sync directory

# Firewall (UFW)
ufw status
ufw allow 22
ufw allow 80/tcp
ufw deny 3306
ufw enable

Disk and Storage

# Disk usage
df -h                         # Filesystem usage
du -sh /var/log               # Directory size
du -sh *                      # Size of each item in current dir
du -h --max-depth=1 /         # Top-level directory sizes

# Find large files
find / -size +500M -type f 2>/dev/null
du -a / 2>/dev/null | sort -rn | head -20

# Mount/unmount
mount /dev/sdb1 /mnt
umount /mnt
lsblk                         # List block devices
fdisk -l                      # List partitions

Users and Groups

# Current user
whoami
id

# User management
useradd -m -s /bin/bash sam    # Create user
passwd sam                     # Set password
userdel -r sam                 # Delete user + home dir
usermod -aG sudo sam           # Add to sudo group

# Switch user
su - sam                       # Switch to user
sudo command                   # Run as root
sudo -u www-data command       # Run as specific user

# Groups
groups                         # Your groups
groups sam                     # User's groups
groupadd developers
usermod -aG developers sam

# Who's logged in
who
w
last                           # Login history

Archives and Compression

# Tar
tar czf archive.tar.gz dir/          # Create gzip archive
tar cjf archive.tar.bz2 dir/         # Create bzip2 archive
tar xzf archive.tar.gz               # Extract gzip
tar xjf archive.tar.bz2               # Extract bzip2
tar xzf archive.tar.gz -C /target/   # Extract to directory
tar tzf archive.tar.gz               # List contents

# Zip
zip -r archive.zip dir/
unzip archive.zip
unzip -l archive.zip                  # List contents

# Gzip
gzip file.txt                         # Compress (replaces file)
gunzip file.txt.gz                    # Decompress
zcat file.txt.gz                      # View without decompressing

System Information

# System
uname -a                      # Full system info
hostnamectl                   # Hostname and OS
cat /etc/os-release           # OS version
uptime                        # How long system has been running
date                          # Current date/time

# Hardware
lscpu                         # CPU info
free -h                       # Memory usage
lspci                         # PCI devices
lsusb                         # USB devices
lsblk                         # Block devices

# Logs
journalctl -xe                # System logs
journalctl -u nginx           # Service-specific logs
journalctl --since "1 hour ago"
dmesg                         # Kernel messages

Useful One-Liners

# Find and replace in multiple files
find . -name "*.py" -exec sed -i 's/old/new/g' {} +

# Count files in directory
find . -type f | wc -l

# List 10 largest files
find / -type f -exec du -h {} + 2>/dev/null | sort -rh | head -10

# Watch a command (refresh every 2s)
watch -n 2 'df -h'

# Run command on multiple files
for f in *.jpg; do convert "$f" "${f%.jpg}.png"; done

# Quick HTTP server
python3 -m http.server 8000

# Generate random password
openssl rand -base64 24

# Check if port is open
nc -zv localhost 80

# Monitor log in real time with filter
tail -f /var/log/nginx/access.log | grep "404"

See Also