📖 Guide
Linux Commands Cheat Sheet — Complete Reference
Every essential Linux command, from file management to networking to shell scripting. Searchable and organized.
204 commands across 12 categories
File & DirectoryFile Viewing & EditingFile PermissionsText ProcessingArchives & CompressionProcess ManagementNetworkingDisk & StorageUser ManagementSystem InformationShell Scripting BasicsPackage Management
File & Directory
| Command | Description |
|---|---|
ls | List directory contents |
ls -la | List all files with details (including hidden) |
ls -lh | List with human-readable file sizes |
ls -lt | List sorted by modification time (newest first) |
cd <dir>e.g. cd /var/log | Change directory |
cd ~ | Go to home directory |
cd - | Go to previous directory |
pwd | Print current working directory |
mkdir <dir> | Create a directory |
mkdir -p <path>e.g. mkdir -p src/components/ui | Create nested directories |
rm <file> | Remove a file |
rm -r <dir> | Remove directory recursively |
rm -rf <dir> | Force remove without confirmation ⚠️ |
cp <src> <dest> | Copy file |
cp -r <src> <dest> | Copy directory recursively |
mv <src> <dest> | Move or rename file/directory |
touch <file> | Create empty file or update timestamp |
ln -s <target> <link> | Create symbolic link |
find <dir> -name '*.js'e.g. find . -name '*.log' | Find files by name pattern |
find <dir> -type f -mtime -7 | Find files modified in last 7 days |
find . -size +100M | Find files larger than 100MB |
locate <pattern> | Fast file search using index database |
tree | Show directory tree structure |
tree -L 2 | Show tree with depth limit |
stat <file> | Show detailed file information |
File Viewing & Editing
| Command | Description |
|---|---|
cat <file> | Display file contents |
cat -n <file> | Display with line numbers |
less <file> | View file with pagination (scrollable) |
head <file> | Show first 10 lines |
head -n 20 <file> | Show first 20 lines |
tail <file> | Show last 10 lines |
tail -n 50 <file> | Show last 50 lines |
tail -f <file> | Follow file in real-time (great for logs) |
wc <file> | Count lines, words, and bytes |
wc -l <file> | Count lines only |
diff <file1> <file2> | Compare two files line by line |
diff -u <file1> <file2> | Unified diff format |
nano <file> | Open file in nano editor |
tee <file>e.g. echo 'hi' | tee output.txt | Read stdin, write to stdout AND file |
File Permissions
| Command | Description |
|---|---|
chmod 755 <file> | Set permissions (owner: rwx, group/other: rx) |
chmod +x <file> | Make file executable |
chmod -R 644 <dir> | Set permissions recursively |
chmod u+w,g-w <file> | Add owner write, remove group write |
chown <user>:<group> <file> | Change file owner and group |
chown -R <user> <dir> | Change owner recursively |
chgrp <group> <file> | Change group ownership |
umask 022 | Set default permission mask for new files |
Text Processing
| Command | Description |
|---|---|
grep <pattern> <file>e.g. grep 'error' app.log | Search for pattern in file |
grep -r <pattern> <dir> | Recursive search in directory |
grep -i <pattern> <file> | Case-insensitive search |
grep -n <pattern> <file> | Show line numbers with matches |
grep -c <pattern> <file> | Count matching lines |
grep -v <pattern> <file> | Invert match (show non-matching lines) |
grep -E 'pat1|pat2' <file> | Extended regex (OR pattern) |
grep -l <pattern> *.js | List filenames with matches |
sed 's/old/new/' <file> | Replace first occurrence per line |
sed 's/old/new/g' <file> | Replace all occurrences |
sed -i 's/old/new/g' <file> | In-place replacement (modify file) |
sed -n '5,10p' <file> | Print lines 5 through 10 |
sed '/pattern/d' <file> | Delete lines matching pattern |
awk '{print $1}' <file>e.g. ls -l | awk '{print $5, $9}' | Print first column |
awk -F: '{print $1}' /etc/passwd | Use custom field separator |
awk '{sum+=$1} END {print sum}' | Sum values in first column |
sort <file> | Sort lines alphabetically |
sort -n <file> | Sort numerically |
sort -r <file> | Sort in reverse order |
sort -u <file> | Sort and remove duplicates |
uniq | Remove adjacent duplicate lines |
uniq -c | Count occurrences of each line |
cut -d: -f1 <file> | Cut field 1 using : delimiter |
cut -c1-10 <file> | Cut characters 1-10 from each line |
tr 'a-z' 'A-Z'e.g. echo 'hello' | tr 'a-z' 'A-Z' | Translate lowercase to uppercase |
tr -d '\n' | Delete newline characters |
xargs <cmd>e.g. find . -name '*.tmp' | xargs rm | Build commands from stdin |
xargs -I {} <cmd> {}e.g. cat urls.txt | xargs -I {} curl {} | Use placeholder for each input |
Archives & Compression
| Command | Description |
|---|---|
tar -czf archive.tar.gz <dir> | Create gzipped tar archive |
tar -xzf archive.tar.gz | Extract gzipped tar archive |
tar -xzf archive.tar.gz -C <dir> | Extract to specific directory |
tar -tf archive.tar.gz | List archive contents |
tar -cjf archive.tar.bz2 <dir> | Create bzip2 compressed archive |
gzip <file> | Compress file (replaces original) |
gunzip <file>.gz | Decompress gzip file |
zip -r archive.zip <dir> | Create zip archive |
unzip archive.zip | Extract zip archive |
unzip -l archive.zip | List zip contents |
Process Management
| Command | Description |
|---|---|
ps aux | List all running processes |
ps aux | grep <name> | Find process by name |
top | Interactive process viewer |
htop | Enhanced interactive process viewer |
kill <pid> | Terminate process by PID (SIGTERM) |
kill -9 <pid> | Force kill process (SIGKILL) |
killall <name> | Kill all processes by name |
pkill -f <pattern> | Kill processes matching pattern |
bg | Resume suspended job in background |
fg | Bring background job to foreground |
jobs | List background/suspended jobs |
<cmd> &e.g. sleep 100 & | Run command in background |
nohup <cmd> & | Run command immune to hangups |
nice -n 10 <cmd> | Run with lower priority |
renice -n 5 -p <pid> | Change priority of running process |
pgrep <name> | Find process ID by name |
lsof -i :8080 | Show process using port 8080 |
Networking
| Command | Description |
|---|---|
curl <url>e.g. curl https://api.example.com | Make HTTP request |
curl -o file <url> | Download file |
curl -X POST -d '{...}' <url> | POST request with data |
curl -H 'Authorization: Bearer token' <url> | Request with custom header |
curl -s <url> | jq . | Fetch JSON and pretty-print |
wget <url> | Download file |
wget -r <url> | Recursive download (mirror site) |
ssh user@host | Connect to remote machine |
ssh -i key.pem user@host | SSH with private key |
ssh -L 8080:localhost:80 user@host | Local port forwarding |
scp <file> user@host:<path> | Copy file to remote machine |
scp user@host:<file> <local> | Copy file from remote machine |
rsync -avz <src> <dest> | Sync files (fast, incremental) |
rsync -avz --delete <src> <dest> | Sync and delete extra files at dest |
ping <host> | Test connectivity |
ping -c 4 <host> | Ping 4 times then stop |
ss -tuln | Show listening TCP/UDP ports |
ss -tp | Show TCP connections with process info |
dig <domain>e.g. dig google.com | DNS lookup |
dig +short <domain> | DNS lookup (IP only) |
nslookup <domain> | Query DNS servers |
ip addr | Show network interfaces and IPs |
ip route | Show routing table |
traceroute <host> | Trace packet route to host |
netcat -l -p 8080 | Listen on port 8080 |
Disk & Storage
| Command | Description |
|---|---|
df -h | Show disk space usage (human-readable) |
du -sh <dir> | Show directory size |
du -sh * | sort -rh | head | Top 10 largest items in current dir |
du -ah <dir> | sort -rh | head -20 | Top 20 largest files in directory tree |
lsblk | List block devices (disks, partitions) |
mount | Show mounted filesystems |
mount <device> <dir> | Mount a filesystem |
umount <dir> | Unmount a filesystem |
fdisk -l | List disk partitions |
ncdu <dir> | Interactive disk usage analyzer |
User Management
| Command | Description |
|---|---|
whoami | Show current username |
id | Show user ID, group ID, and groups |
who | Show logged-in users |
w | Show logged-in users and their activity |
useradd <user> | Create a new user |
useradd -m -s /bin/bash <user> | Create user with home dir and bash shell |
usermod -aG <group> <user>e.g. usermod -aG docker daniel | Add user to group |
userdel -r <user> | Delete user and home directory |
passwd <user> | Change user password |
groups <user> | Show user's groups |
su - <user> | Switch to another user |
sudo <cmd> | Run command as root |
sudo -u <user> <cmd> | Run command as another user |
visudo | Safely edit sudoers file |
System Information
| Command | Description |
|---|---|
uname -a | Show all system information |
hostname | Show or set system hostname |
uptime | Show system uptime and load average |
free -h | Show memory usage (human-readable) |
lscpu | Show CPU information |
lsb_release -a | Show Linux distribution info |
cat /etc/os-release | Show OS version details |
dmesg | tail | Show recent kernel messages |
date | Show current date and time |
timedatectl | Show/set time zone and NTP status |
env | Show all environment variables |
printenv <VAR> | Show specific environment variable |
which <cmd> | Show full path of a command |
type <cmd> | Show how a command would be interpreted |
Shell Scripting Basics
| Command | Description |
|---|---|
#!/bin/bash | Shebang — first line of a bash script |
VAR="value" | Set a variable (no spaces around =) |
$VAR or ${VAR} | Reference a variable |
$(command)e.g. DATE=$(date +%Y-%m-%d) | Command substitution |
export VAR=value | Set environment variable for child processes |
if [ condition ]; then ... fi | If statement |
[ -f file ] | Test if file exists |
[ -d dir ] | Test if directory exists |
[ -z "$VAR" ] | Test if variable is empty |
[ "$a" = "$b" ] | String equality test |
[ $a -eq $b ] | Numeric equality test |
for i in 1 2 3; do ... done | For loop |
for f in *.txt; do ... done | Loop over files |
while [ condition ]; do ... done | While loop |
case $VAR in pat1) ... ;; esac | Case/switch statement |
function name() { ... } | Define a function |
$1, $2, $@, $# | Script arguments: first, second, all, count |
$? | Exit code of last command (0 = success) |
set -e | Exit script on any error |
set -x | Print each command before executing (debug) |
cmd1 && cmd2 | Run cmd2 only if cmd1 succeeds |
cmd1 || cmd2 | Run cmd2 only if cmd1 fails |
cmd1 | cmd2 | Pipe stdout of cmd1 to stdin of cmd2 |
cmd > file | Redirect stdout to file (overwrite) |
cmd >> file | Redirect stdout to file (append) |
cmd 2>&1 | Redirect stderr to stdout |
cmd &> file | Redirect both stdout and stderr to file |
Package Management
| Command | Description |
|---|---|
apt update | Update package index (Debian/Ubuntu) |
apt upgrade | Upgrade all packages |
apt install <pkg>e.g. apt install nginx | Install a package |
apt remove <pkg> | Remove a package |
apt search <query> | Search for packages |
apt list --installed | List installed packages |
dnf install <pkg> | Install package (Fedora/RHEL) |
dnf update | Update all packages (Fedora/RHEL) |
pacman -S <pkg> | Install package (Arch) |
pacman -Syu | Full system upgrade (Arch) |
snap install <pkg> | Install snap package |
snap list | List installed snaps |
📖 Free, searchable command reference. Bookmark this page for quick access.