Linux Command Cheatsheet
Linux is the foundation of server development and operations. This cheatsheet organizes essential Linux commands by scenario — from file operations to system administration, each with practical examples. Whether you're new to Linux or need a quick reference, this guide has you covered.
Quick Reference
| Task | Command |
| Print working directory | pwd |
| List files | ls -la |
| Change directory | cd /path |
| View processes | ps aux / top |
| Search files | find / -name "file" |
| Search content | grep "text" file |
| Change permissions | chmod 755 file |
| Check disk usage | df -h / du -sh |
| Install software | apt install pkg |
| Check service status | systemctl status |
File Operations
### File & Directory Management
``bash
# Current path
pwd
# List directory contents ls -la # Detailed (with hidden files) ls -lh # Human-readable sizes ls -ltr # Sort by time, newest last
# Change directory cd /tmp # Go to absolute path cd ~ # Go home cd - # Go to previous directory
# Create & delete
mkdir -p a/b/c # Recursively create directories
touch file.txt # Create empty file
rm -rf dir/ # Recursively force delete (⚠️ dangerous)
cp -r src dst # Recursively copy
mv src dst # Move or rename
`
### View File Content
`bash
cat file.txt # Display all
less file.txt # Paginated view (Space=next, q=quit)
head -n 20 file # First 20 lines
tail -f log.log # Follow log in real-time
wc -l file # Count lines
`
> Tip: rm -rf / will destroy your entire system. Always double-check paths with rm.
Process Management
`bash
# View processes
ps aux # All processes
ps aux | grep nginx # Filter
top # Real-time monitor (q to quit, 1 for CPU cores)
# Manage processes
kill PID # Terminate by PID
kill -9 PID # Force kill (SIGKILL)
pkill -f "name" # Terminate by process name
nohup command & # Run in background, survive terminal close
jobs # List background jobs
fg %1 # Bring job to foreground
`
Networking
`bash
# Diagnostics
ping -c 4 8.8.8.8 # Connectivity test
curl -I https://example.com # HTTP headers
wget -O output.zip https://... # Download file
traceroute host # Route tracing
# Ports & connections ss -tulpn # Listening ports (replaces netstat) ss -tulpn | grep :80 # Check port 80
# Remote access
ssh user@host -p 22 # SSH login
scp file user@host:/path/ # File transfer
rsync -av src/ dst/ # Incremental sync
`
Search & Find
`bash
# Search files
find /home -name "*.log" # By filename
find /home -type f -size +10M # By type and size
# Search content grep "error" logfile # Basic search grep -r "TODO" src/ # Recursive directory search grep -i "warning" log # Case-insensitive grep -C 3 "error" log # Show 3 lines context
# Locate commands
which nginx # Command path
whereis nginx # Binary + source + man path
`
Permissions
`bash
# Change permissions
chmod 755 script.sh # rwx r-x r-x
chmod +x script.sh # Add executable
chmod -R 644 dir/ # Recursive file permissions
# Change owner chown user:group file chown -R user:group dir/
# View permissions
ls -la # First column: -rwxr-xr-x
`
> Permission bits: r=4, w=2, x=1 → rwx=7, r-x=5, r--=4, ---=0
Disk Management
`bash
# Check disk
df -h # Disk space (human-readable)
du -sh * # Directory sizes in current dir
du -sh /var/log/ # Specific directory total
# Mount management
mount # View mounts
mount /dev/sdb1 /mnt # Mount device
umount /mnt # Unmount
`
Package Management
`bash
# Debian/Ubuntu (apt)
apt update # Update package index
apt install nginx # Install package
apt remove nginx # Remove package
apt search "keyword" # Search packages
# RHEL/CentOS (yum/dnf)
yum install nginx # Install
dnf install nginx # Fedora (newer)
`
> Tip: apt and yum have different commands but similar concepts. Not sure your distro? Run cat /etc/os-release.
System Administration
`bash
# System info
uname -a # Kernel version
hostnamectl # Hostname + OS info
uptime # Uptime + load average
whoami # Current user
# Service management (systemd) systemctl status nginx # Check service status systemctl start nginx # Start service systemctl stop nginx # Stop service systemctl restart nginx # Restart service systemctl enable nginx # Enable on boot
# Logs journalctl -u nginx # View service logs journalctl -u nginx -n 50 # Last 50 lines journalctl -u nginx -f # Follow in real-time
# System load
free -h # Memory usage
uptime # Load averages (1m/5m/15m)
htop # Interactive monitor (if installed)
`
Pro Tips
`bash
# Pipe & redirection
command | grep "xxx" # Pipe to filter
command > file.log # Redirect output (overwrite)
command >> file.log # Redirect output (append)
command 2>&1 # Redirect stderr too
# Command history history # View history !! # Repeat last command !$ # Last argument of last command ctrl+r # Search history (press repeatedly to cycle)
# Shortcuts
ctrl+c # Force quit
ctrl+d # Exit shell or EOF
ctrl+z # Suspend current task
`
Troubleshooting
| Symptom | Cause | Fix |
command not found | Not installed or not in PATH | which command to check, use full path /usr/bin/command | |
Permission denied | No execute permission | chmod +x file or prefix with sudo | |
| Disk full | / partition at 100% | df -h to locate, du -sh /* to find big dirs | |
| Port in use | Another process is listening | ss -tulpn \ | grep :port to find PID, kill PID |
| Process won't die | Zombie process | Try kill, then kill -9`, then reboot if needed |
File(13)
| Command | Level | ||
|---|---|---|---|
pwdPrint working directory | Basic | pwd | |
lsList directory contents | Basic | ls -la | |
cdChange working directory | Basic | cd /home/user/project | |
mkdirCreate directories | Basic | mkdir -p a/b/c | |
touchCreate empty file or update file timestamps | Basic | touch file.txt | |
cpCopy files or directories | Basic | cp -r src/ dst/ | |
mvMove or rename files | Basic | mv old.txt new.txt | |
rmRemove files or directories rm -rf / will delete your entire system! Always double-check paths | Basic | rm -rf dir/ | |
catDisplay file contents | Basic | cat file.txt | |
lessView file contents page by page | Basic | less file.txt | |
headDisplay first lines of a file | Basic | head -n 20 file.txt | |
tailDisplay last lines of a file | Basic | tail -f log.log | |
wcCount lines, words, and bytes in a file | Basic | wc -l file.txt |
Process(6)
| Command | Level | ||
|---|---|---|---|
psDisplay running processes | Basic | ps aux | |
topReal-time view of system processes and resource usage | Intermediate | top | |
killTerminate a process by PID Use kill -9 (SIGKILL) as a last resort, prefer kill (SIGTERM) first | Intermediate | kill -9 1234 | |
nohupRun command immune to hangups (survives terminal close) | Intermediate | nohup long-running-script.sh & | |
pkillKill processes by name | Intermediate | pkill -f "node server" | |
htopInteractive process viewer (enhanced top) | Intermediate | htop |
Network(8)
| Command | Level | ||
|---|---|---|---|
pingTest network connectivity | Basic | ping -c 4 8.8.8.8 | |
curlSend HTTP requests or download files | Intermediate | curl -I https://example.com | |
wgetDownload files from the web | Basic | wget -O output.zip https://example.com/file.zip | |
ssSocket statistics (modern netstat replacement) | Intermediate | ss -tulpn | |
sshRemote login to SSH server | Intermediate | ssh user@hostname -p 22 | |
scpSecurely copy files over SSH | Intermediate | scp file.txt user@host:/tmp/ | |
rsyncFast incremental file transfer tool | Intermediate | rsync -av src/ user@host:/dst/ | |
tracerouteTrace the route packets take to a host | Intermediate | traceroute example.com |
Search(4)
| Command | Level | ||
|---|---|---|---|
findSearch for files by criteria Can be slow on large scopes, use -maxdepth to limit depth | Intermediate | find /home -name "*.log" | |
grepSearch for text patterns in files | Intermediate | grep -r "TODO" src/ | |
whichShow the full path of a command | Basic | which nginx | |
whereisLocate binary, source, and man page for a command | Basic | whereis nginx |
Permission(2)
| Command | Level | ||
|---|---|---|---|
chmodChange file or directory permissions | Intermediate | chmod 755 script.sh | |
chownChange file or directory owner | Intermediate | chown user:group file.txt |
Disk(4)
| Command | Level | ||
|---|---|---|---|
dfReport file system disk space usage | Basic | df -h | |
duEstimate file/directory disk usage | Basic | du -sh /var/log/ | |
mountMount a filesystem or device | Intermediate | mount /dev/sdb1 /mnt | |
umountUnmount a mounted filesystem | Intermediate | umount /mnt |
Package(3)
| Command | Level | ||
|---|---|---|---|
aptDebian/Ubuntu package manager | Intermediate | apt install nginx | |
yumRHEL/CentOS package manager | Intermediate | yum install nginx | |
dnfFedora/RHEL next-gen package manager | Intermediate | dnf install nginx |
System(6)
| Command | Level | ||
|---|---|---|---|
unameDisplay system kernel information | Basic | uname -a | |
uptimeShow system uptime and load averages | Basic | uptime | |
systemctlsystemd service manager | Intermediate | systemctl status nginx | |
journalctlView systemd logs | Intermediate | journalctl -u nginx -n 50 -f | |
freeDisplay system memory usage | Basic | free -h | |
hostnamectlView or set hostname and system info | Basic | hostnamectl |
Shell(1)
| Command | Level | ||
|---|---|---|---|
historyView command history | Basic | history | grep "git" |