Bash Cheat Sheet

Essential shell commands for navigation, files, processes, pipes, scripting, and the moves you forget at 2am

Navigation
pwd
Print working directory (where am I)
cd <dir>
Change directory
cd
Go to home directory
cd -
Go to previous directory
cd ..
Go up one directory
ls
List directory contents
ls -la
List all files with details (incl. hidden)
ls -lh
List with human-readable file sizes
tree
Show directory tree (may need install)
Files & Directories
touch <file>
Create empty file or update timestamp
mkdir <dir>
Create directory
mkdir -p a/b/c
Create nested dirs, no error if exists
cp src dst
Copy file
cp -r src dst
Copy directory recursively
mv src dst
Move or rename file/directory
rm <file>
Delete file
rm -r <dir>
Delete directory recursively
rm -rf <dir>
Force-delete dir (DANGEROUS, no prompt)
ln -s target link
Create symbolic link
Viewing & Searching Files
cat <file>
Print file contents
less <file>
Page through file (q to quit, / to search)
head -n 20 <file>
First 20 lines
tail -n 20 <file>
Last 20 lines
tail -f <file>
Follow file as it grows (logs)
grep "pattern" <file>
Search for pattern in file
grep -r "pattern" .
Recursive search in current dir
grep -i "pattern"
Case-insensitive search
grep -n "pattern"
Show line numbers in matches
find . -name "*.js"
Find files by name pattern
find . -type f -size +10M
Find files larger than 10MB
find . -mtime -7
Files modified in last 7 days
Permissions & Ownership
chmod 755 <file>
rwxr-xr-x (owner/group/other)
chmod +x <file>
Make file executable
chmod -R 644 <dir>
Recursive chmod
chown user:group <file>
Change file owner and group
sudo <cmd>
Run command as root
sudo -i
Open root shell
whoami
Current user
id
User ID and groups
Pipes, Redirection & Substitution
cmd1 | cmd2
Pipe stdout of cmd1 into cmd2
cmd > file
Redirect stdout to file (overwrite)
cmd >> file
Append stdout to file
cmd 2> file
Redirect stderr to file
cmd > file 2>&1
Redirect stdout AND stderr to file
cmd &> file
Same as above (bash shorthand)
cmd < file
Read stdin from file
cmd | tee file
Pipe AND save to file simultaneously
$(cmd)
Command substitution (use cmd's output)
cmd1 && cmd2
Run cmd2 only if cmd1 succeeds
cmd1 || cmd2
Run cmd2 only if cmd1 fails
Process Management
ps aux
List all running processes
ps aux | grep node
Filter processes by name
top
Live process monitor
htop
Better top (may need install)
kill <pid>
Send SIGTERM to process
kill -9 <pid>
Force-kill (SIGKILL)
killall <name>
Kill all processes matching name
cmd &
Run command in background
jobs
List background jobs in this shell
fg %1
Bring job 1 to foreground
nohup cmd &
Run command immune to hangup, in background
Environment & Variables
echo $PATH
Print value of $PATH
env
List all environment variables
export VAR=value
Set environment variable for session
unset VAR
Remove environment variable
VAR=value cmd
Set var only for this command
source ~/.bashrc
Re-load bash config
which cmd
Find which binary runs for cmd
type cmd
Show how cmd is interpreted (alias, builtin, file)
alias ll='ls -la'
Create alias for the session
Text Processing
wc -l <file>
Count lines in file
wc -w <file>
Count words
sort <file>
Sort lines alphabetically
sort -n
Numeric sort
sort -u
Sort and remove duplicates
uniq
Remove adjacent duplicate lines (often after sort)
cut -d',' -f1 <file>
First column of CSV
awk '{print $2}'
Print second whitespace-separated field
sed 's/foo/bar/g' <file>
Replace all "foo" with "bar"
sed -i.bak 's/foo/bar/g' <file>
In-place edit with .bak backup
tr 'a-z' 'A-Z'
Translate lowercase to uppercase
xargs cmd
Build command line from stdin
Networking
curl <url>
Fetch URL
curl -sS <url>
Silent but show errors
curl -X POST -d '{}' <url>
POST with JSON body
curl -H "Auth: x" <url>
Custom header
curl -o file <url>
Save response to file
wget <url>
Download file
ssh user@host
SSH into a remote host
ssh -p 2222 user@host
SSH on a custom port
scp file user@host:/path
Copy file to remote
rsync -av src/ dst/
Sync directories (efficient)
ping <host>
Test reachability
netstat -tulpn
Show listening ports (Linux)
lsof -i :3000
What process is using port 3000
Archives & Compression
tar -czvf out.tar.gz dir/
Create gzipped tar archive
tar -xzvf in.tar.gz
Extract gzipped tar archive
tar -tzvf in.tar.gz
List contents without extracting
zip -r out.zip dir/
Create zip archive
unzip in.zip
Extract zip archive
gzip <file>
Compress single file (.gz)
gunzip <file>.gz
Decompress .gz file
System Info & Disk
uname -a
Kernel and system info
df -h
Disk usage by filesystem (human-readable)
du -sh <dir>
Total size of directory
du -h --max-depth=1
Size of subdirectories, one level deep
free -h
Memory usage (Linux)
uptime
System uptime and load average
date
Current date and time
cal
Calendar of current month
Scripting Essentials
#!/usr/bin/env bash
Shebang line for portable bash scripts
set -e
Exit on any command failure
set -u
Exit on undefined variable
set -o pipefail
Fail if any command in a pipe fails
set -euo pipefail
All three above (recommended for scripts)
if [ -f file ]; then ...; fi
Conditional, file exists
for x in *; do echo $x; done
For loop over files
while read line; do ...; done < file
Read file line by line
$#
Number of arguments passed to script
$1, $2, $@
First arg, second arg, all args
$?
Exit code of last command (0 = success)
trap "cmd" EXIT
Run cmd on script exit (cleanup)
History & Shortcuts
history
Show command history
!!
Run last command again
!$
Last argument of previous command
!grep
Run last command starting with "grep"
Ctrl+R
Reverse-search through history
Ctrl+A
Move cursor to start of line
Ctrl+E
Move cursor to end of line
Ctrl+U
Delete from cursor to start of line
Ctrl+K
Delete from cursor to end of line
Ctrl+W
Delete word before cursor
Ctrl+L
Clear screen (same as `clear`)
Ctrl+C
Send SIGINT to current foreground process
Ctrl+D
EOF / exit shell if input is empty