Linux Cheatsheet
Linux Cheatsheet
A quick-reference guide for essential and advanced Linux commands, covering file operations, process management, networking, permissions, text processing utilities, archiving, and remote file downloads.
File Commands
ls -la # List files in long format including hidden files (.dotfiles)
cd <dir> # Change working directory
pwd # Print current working directory path
mkdir -p <path> # Create a directory and any missing parent directories
rm <file> # Delete a file
rm -rf <dir> # Force delete a directory recursively (dangerous!)
cp <src> <dest> # Copy a file
cp -r <src_dir> <dest_dir> # Copy a directory recursively
mv <src> <dest> # Move or rename a file/directory
touch <file> # Create an empty file or update its modified timestamp
cat <file> # Output entire file contents to terminal
head -n 20 <file> # View the first 20 lines of a file
tail -n 20 <file> # View the last 20 lines of a file
tail -f <file> # Follow and view append logs to a file in real-time
less <file> # Read a file with interactive paginated scrolling
Process Commands
ps aux # View all currently running active processes
ps aux | grep <name> # Find process ID (PID) of a specific process
top # Interactive real-time process monitor
htop # Modern, colorful, interactive process monitor (recommended)
kill <PID> # Terminate a process gracefully by ID (SIGTERM)
kill -9 <PID> # Force terminate a process immediately (SIGKILL)
killall <name> # Terminate all processes matching a specific name
pkill <name> # Terminate processes matching a partial name
bg # Resume a suspended process in the background
fg # Move a background process to the foreground
jobs # List all active background jobs
lsof -i :8080 # List processes listening on a specific port (e.g. 8080)
Networking
ping <host> # Send ICMP echo requests to verify host connectivity
ip addr # View local network interfaces and IP addresses (modern)
ifconfig # View network interfaces and IP configurations (legacy)
ss -tulpn # List active listening TCP/UDP sockets with PIDs (modern)
netstat -tulpn # List active TCP/UDP listening connections (legacy)
traceroute <host> # Trace route hops to a destination host
nslookup <domain> # Lookup IP address of a domain
dig <domain> # Advanced DNS query tool (returns record details)
nc -zv <host> <port> # Scan a port to check if it's open (Netcat)
Permissions
# chmod [octal] <file> (Numeric notation)
chmod 755 script.sh # Owner: rwx, Group: r-x, Others: r-x
chmod 644 config.txt # Owner: rw-, Group: r--, Others: r--
# chmod [symbolic] <file> (Symbolic notation)
chmod +x script.sh # Add execute permission (x) for everyone
chmod u+w config.txt # Add write permission (w) for Owner/User (u)
chmod g-w config.txt # Remove write permission (w) from Group (g)
chmod o-r config.txt # Remove read permission (r) from Others (o)
# Ownership
chown <user> <file> # Change owner of a file
chown <user>:<group> <file> # Change both owner and group of a file
chown -R <user> <dir> # Change owner of a directory recursively
chgrp <group> <file> # Change group ownership of a file
grep (Text Searching)
grep "pattern" file.txt # Search for a pattern inside a file
grep -i "pattern" file.txt # Case-insensitive pattern search
grep -r "pattern" <dir> # Recursively search for a pattern in all files under directory
grep -v "pattern" file.txt # Invert match: show lines NOT containing the pattern
grep -E "regex" file.txt # Search using Extended Regular Expressions (ERE)
grep -n "pattern" file.txt # Show line numbers alongside matches
grep -C 3 "pattern" file.txt # Show 3 lines of context around each match (A=after, B=before)
sed (Stream Editor)
sed 's/old/new/' file.txt # Replace the first occurrence of 'old' with 'new' per line
sed 's/old/new/g' file.txt # Replace ALL occurrences of 'old' with 'new' globally in file
sed -i 's/old/new/g' file.txt # In-place edit: replace and save modifications directly to the file
sed '/pattern/d' file.txt # Delete lines matching a specific pattern
sed -n '5,10p' file.txt # Print only lines 5 through 10 of a file
awk (Text Processing)
awk '{print $1}' file.txt # Print only the first column of every line (whitespace separated)
awk -F',' '{print $2}' f.csv # Print the second column using a custom comma delimiter (-F)
awk '/pattern/ {print $3}' f # If a line matches the pattern, print its third column
awk '$3 > 100 {print $1, $3}' # Conditional: print columns 1 and 3 if column 3 exceeds 100
awk '{sum += $1} END {print sum}' f # Sum up all numbers in the first column and output total
find (File Searching)
find . -name "*.log" # Find files ending in .log in current directory and subdirectories
find /path -iname "config*" # Find files starting with 'config' (case-insensitive)
find . -type d # Find only directories (-type f for files, -type l for symlinks)
find . -size +100M # Find files larger than 100 Megabytes
find . -mtime -7 # Find files modified within the last 7 days
find . -type f -exec chmod 644 {} \; # Execute action: apply chmod 644 to all found files
tar (Archiving)
tar -czvf archive.tar.gz <dir># Compress a directory into a tar.gz archive (c=create, z=gzip, v=verbose, f=file)
tar -xzvf archive.tar.gz # Extract a tar.gz archive (x=extract)
tar -cjvf archive.tar.bz2 <d> # Compress a directory into a high-ratio tar.bz2 archive (j=bzip2)
tar -xjvf archive.tar.bz2 # Extract a tar.bz2 archive
tar -tf archive.tar # List the contents of an archive without extracting it
curl (Data Transfer)
curl <url> # Output website content/response to the terminal
curl -o file.html <url> # Download URL content and save it to a specific file
curl -O <url> # Download file and save it using its remote filename
curl -I <url> # Fetch HTTP response headers only (GET HEAD)
curl -H "Auth: Key" <url> # Send custom HTTP header with request
curl -d "param1=val" <url> # Send POST request data (defaults to application/x-www-form-urlencoded)
curl -i -X POST -H "Content-Type: application/json" -d '{"id":1}' <url> # Send JSON POST request
wget (File Downloader)
wget <url> # Download a file to the current directory
wget -O custom.zip <url> # Download and save a file using a custom local filename
wget -c <url> # Resume a partially downloaded file
wget -b <url> # Download a file in the background (runs in background log)
wget --limit-rate=500k <url> # Limit download speed to 500 Kilobytes per second
wget -r --no-parent <url> # Recursively download a directory/site under a specific URL path