Linux CLI Cheatsheet
7 min read
File & Directory Navigation
| Command | Description |
|---|---|
pwd | Print current working directory |
ls -la | List all files (long format, hidden) |
ls -lhS | List sorted by size (human readable) |
ls -lt | List sorted by modification time |
cd - | Go to previous directory |
cd ~ | Go to home directory |
tree -L 2 | Show directory tree (2 levels deep) |
find . -name '*.py' | Find files by name pattern |
find . -type f -mtime -7 | Files modified in last 7 days |
find . -size +100M | Find files larger than 100MB |
locate filename | Fast file search (uses db) |
which / whereis | Find command location |
File Operations
| Command | Description |
|---|---|
cp -r src/ dst/ | Copy directory recursively |
mv old new | Move / rename file |
rm -rf dir/ | Force remove directory (CAUTION) |
mkdir -p a/b/c | Create nested directories |
ln -s target link | Create symbolic link |
touch file.txt | Create empty file / update timestamp |
stat file | Detailed file info (size, perms, times) |
file myfile | Detect file type |
basename /path/to/file.txt | Extract filename from path |
dirname /path/to/file.txt | Extract directory from path |
File Viewing & Editing
| Command | Description |
|---|---|
cat file | Print entire file |
less file | Scrollable file viewer (q to quit) |
head -n 20 file | First 20 lines |
tail -n 20 file | Last 20 lines |
tail -f logfile | Follow log in real-time |
wc -l file | Count lines in file |
diff file1 file2 | Compare two files |
sort file | uniq -c | Sort and count unique lines |
cut -d',' -f1,3 file.csv | Extract columns 1 and 3 |
column -t -s',' file.csv | Display CSV as aligned table |
nano / vim / vi | Terminal text editors |
Text Processing (grep / sed / awk)
| Command | Description |
|---|---|
grep -rn 'pattern' . | Recursive search with line numbers |
grep -i 'pattern' file | Case-insensitive search |
grep -v 'exclude' file | Invert match (exclude lines) |
grep -c 'pattern' file | Count matching lines |
grep -E 'a|b' file | Extended regex (OR) |
sed 's/old/new/g' file | Replace all occurrences |
sed -i 's/old/new/g' file | In-place replacement |
sed -n '5,10p' file | Print lines 5–10 |
awk '{print $1, $3}' file | Print columns 1 and 3 |
awk -F',' '{sum+=$2} END{print sum}' | Sum column 2 (CSV) |
awk '/pattern/ {print}' file | Print matching lines |
Permissions & Ownership
| Command | Description |
|---|---|
chmod 755 file | rwxr-xr-x (owner rwx, others rx) |
chmod +x script.sh | Add execute permission |
chmod -R 644 dir/ | Recursive permission change |
chown user:group file | Change owner and group |
chown -R user dir/ | Recursive ownership change |
umask 022 | Set default permissions for new files |
Process Management
| Command | Description |
|---|---|
ps aux | List all running processes |
ps aux | grep name | Find process by name |
top / htop | Interactive process monitor |
kill PID | Terminate process (graceful) |
kill -9 PID | Force kill process |
killall name | Kill all processes by name |
jobs | List background jobs |
bg / fg | Move job to background / foreground |
nohup cmd & | Run command immune to hangups |
cmd & disown | Run and detach from terminal |
lsof -i :8080 | Find process using port 8080 |
pgrep -f pattern | Find PID by pattern |
Networking
| Command | Description |
|---|---|
ip addr / ifconfig | Show network interfaces |
curl -s url | HTTP request (silent mode) |
curl -o file url | Download file from URL |
wget url | Download file |
wget -r -l 2 url | Recursive download (2 levels) |
ss -tlnp / netstat -tlnp | Show listening ports |
ping -c 4 host | Ping host (4 packets) |
traceroute host | Trace network route |
dig domain | DNS lookup |
ssh user@host | SSH into remote server |
scp file user@host:/path | Copy file over SSH |
rsync -avz src/ user@host:/dst/ | Sync directories over SSH |
Disk & Storage
| Command | Description |
|---|---|
df -h | Disk usage (human-readable) |
du -sh dir/ | Directory size |
du -h --max-depth=1 | Size of each subdirectory |
ncdu /path | Interactive disk usage explorer |
mount / umount | Mount / unmount filesystems |
fdisk -l | List disk partitions |
lsblk | List block devices |
Compression & Archives
| Command | Description |
|---|---|
tar czf archive.tar.gz dir/ | Create gzipped tarball |
tar xzf archive.tar.gz | Extract gzipped tarball |
tar xzf archive.tar.gz -C /dst | Extract to specific directory |
tar tf archive.tar.gz | List tarball contents |
zip -r archive.zip dir/ | Create zip archive |
unzip archive.zip | Extract zip archive |
gzip / gunzip | Compress / decompress .gz |
zcat file.gz | View compressed file |
System Info & Monitoring
| Command | Description |
|---|---|
uname -a | System info (kernel, arch) |
cat /etc/os-release | OS distribution info |
uptime | System uptime and load |
free -h | Memory usage |
nproc | Number of CPU cores |
lscpu | CPU architecture info |
dmesg | tail | Kernel messages (recent) |
journalctl -u service -f | Follow systemd service logs |
systemctl status service | Check service status |
systemctl restart service | Restart a service |
Redirection & Pipes
| Command | Description |
|---|---|
cmd > file | Redirect stdout to file (overwrite) |
cmd >> file | Append stdout to file |
cmd 2>&1 | Redirect stderr to stdout |
cmd &> file | Redirect all output to file |
cmd1 | cmd2 | Pipe stdout of cmd1 to cmd2 |
cmd1 | tee file | cmd2 | Pipe + save to file |
cmd | xargs -I{} echo {} | Pass piped input as arguments |
Handy Shortcuts & One-Liners
| Command | Description |
|---|---|
!! | Repeat last command |
!$ | Last argument of previous command |
Ctrl+R | Reverse search command history |
Ctrl+Z → bg | Suspend and background a process |
Ctrl+A / Ctrl+E | Jump to start / end of line |
history | grep cmd | Search command history |
watch -n 2 cmd | Run command every 2 seconds |
time cmd | Measure execution time |
alias ll='ls -la' | Create command alias |
echo $? | Exit code of last command |
date +%Y-%m-%d | Current date (formatted) |
env / printenv | Show environment variables |
export VAR=value | Set environment variable |