Bash / Shell Cheat Sheet
Essential terminal commands, file operations, text processing, scripting, and keyboard shortcuts for everyday development.
File Operations
| Command | Description |
|---|---|
touch file | Create empty file or update timestamp |
mkdir dir | Create a directory |
mkdir -p a/b/c | Create nested directories |
cp src dest | Copy file |
cp -r src/ dest/ | Copy directory recursively |
mv src dest | Move or rename file/directory |
rm file | Remove file |
rm -rf dir | Remove directory and contents (force) |
ln -s target link | Create symbolic link |
cat file | Display file contents |
less file | View file with scrolling |
head -n 20 file | Show first 20 lines |
tail -n 20 file | Show last 20 lines |
tail -f file | Follow file in real-time (logs) |
wc -l file | Count lines in file |
du -sh dir | Show directory size (human-readable) |
df -h | Show disk usage (human-readable) |
file filename | Detect file type |
Text Processing
| Command | Description |
|---|---|
grep "pattern" file | 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 |
grep -c "pattern" file | Count matching lines |
grep -v "pattern" file | Invert match (exclude pattern) |
sed 's/old/new/g' file | Replace text (all occurrences) |
sed -i 's/old/new/g' file | Replace in-place |
awk '{print $1}' file | Print first column |
awk -F, '{print $2}' file | Print 2nd column (comma delimiter) |
sort file | Sort lines alphabetically |
sort -n file | Sort numerically |
sort -r file | Sort in reverse |
uniq | Remove adjacent duplicate lines |
sort | uniq -c | Count occurrences of each line |
cut -d',' -f1,3 file | Extract fields 1 and 3 (comma-separated) |
tr 'a-z' 'A-Z' | Translate lowercase to uppercase |
diff file1 file2 | Compare two files |
Search & Find
| Command | Description |
|---|---|
find . -name "*.js" | Find files by name pattern |
find . -type d -name "src" | Find directories by name |
find . -type f -mtime -7 | Files modified in last 7 days |
find . -size +100M | Files larger than 100MB |
find . -name "*.log" -delete | Find and delete files |
find . -exec cmd {} \; | Execute command on each result |
which command | Show full path of a command |
whereis command | Locate binary, source, and man page |
locate filename | Fast file search (uses database) |
Permissions
| Command | Description |
|---|---|
chmod 755 file | Set permissions (rwxr-xr-x) |
chmod +x file | Add execute permission |
chmod -R 644 dir | Set permissions recursively |
chown user:group file | Change file owner and group |
chown -R user dir | Change owner recursively |
# Permission numbers
# 4 = read (r) 2 = write (w) 1 = execute (x)
# 7 = rwx 6 = rw- 5 = r-x 4 = r-- 0 = ---
# Common patterns:
755 # owner: rwx, group: r-x, others: r-x (directories, scripts)
644 # owner: rw-, group: r--, others: r-- (files)
700 # owner: rwx, group: ---, others: --- (private)
600 # owner: rw-, group: ---, others: --- (SSH keys)
Process Management
| Command | Description |
|---|---|
ps aux | List all running processes |
ps aux | grep name | Find a process by name |
top | Interactive process viewer |
htop | Enhanced interactive process viewer |
kill PID | Terminate a process by PID |
kill -9 PID | Force kill a process |
killall name | Kill all processes by name |
command & | Run command in background |
jobs | List background jobs |
fg %1 | Bring job 1 to foreground |
bg %1 | Resume job 1 in background |
nohup cmd & | Run command that survives terminal close |
lsof -i :8080 | Find process using port 8080 |
Networking
| Command | Description |
|---|---|
curl url | Fetch URL content |
curl -o file url | Download file |
curl -X POST -d 'data' url | Send POST request with data |
curl -H "Key: Value" url | Send request with custom header |
wget url | Download file |
wget -r url | Download recursively |
ping host | Test network connectivity |
ssh user@host | Connect to remote host |
scp file user@host:path | Copy file to remote host |
scp user@host:file . | Copy file from remote host |
rsync -avz src/ dest/ | Sync files (efficient, incremental) |
netstat -tulnp | Show listening ports |
ss -tulnp | Show listening ports (modern) |
ifconfig / ip addr | Show network interfaces |
Variables & Environment
| Syntax | Description |
|---|---|
NAME="value" | Set a variable (no spaces around =) |
echo $NAME | Use a variable |
echo "${NAME}_suffix" | Variable with surrounding text |
export NAME="value" | Set environment variable (available to child processes) |
unset NAME | Remove a variable |
env | List all environment variables |
$HOME | Home directory path |
$PATH | Command search paths |
$USER | Current username |
$PWD | Current directory |
$? | Exit code of last command (0 = success) |
$$ | Current process ID |
$! | PID of last background process |
$# | Number of arguments |
$@ | All arguments (as separate words) |
Scripting Basics
#!/bin/bash
# Variables
NAME="World"
echo "Hello, $NAME!"
# If/else
if [ "$1" = "hello" ]; then
echo "Hi there!"
elif [ -z "$1" ]; then
echo "No argument"
else
echo "Unknown: $1"
fi
# For loop
for i in 1 2 3 4 5; do
echo "Number: $i"
done
# For loop (C-style)
for ((i=0; i<10; i++)); do
echo "$i"
done
# While loop
while [ $count -lt 5 ]; do
echo "$count"
((count++))
done
# Functions
greet() {
echo "Hello, $1!"
}
greet "Alice"
Test Conditions
| Test | Description |
|---|---|
-f file | File exists and is a regular file |
-d dir | Directory exists |
-e path | Path exists (file or directory) |
-r file | File is readable |
-w file | File is writable |
-x file | File is executable |
-z "$var" | Variable is empty |
-n "$var" | Variable is not empty |
"$a" = "$b" | Strings are equal |
"$a" != "$b" | Strings are not equal |
$a -eq $b | Numbers are equal |
$a -lt $b | a less than b |
$a -gt $b | a greater than b |
Redirection & Pipes
| Syntax | Description |
|---|---|
cmd > file | Redirect stdout to file (overwrite) |
cmd >> file | Append stdout to file |
cmd 2> file | Redirect stderr to file |
cmd &> file | Redirect both stdout and stderr |
cmd 2>&1 | Redirect stderr to stdout |
cmd < file | Use file as stdin |
cmd1 | cmd2 | Pipe stdout of cmd1 to stdin of cmd2 |
cmd1 | tee file | cmd2 | Pipe and save output to file simultaneously |
cmd1 && cmd2 | Run cmd2 only if cmd1 succeeds |
cmd1 || cmd2 | Run cmd2 only if cmd1 fails |
cmd1 ; cmd2 | Run both commands sequentially |
$(command) | Command substitution (use output as value) |
Keyboard Shortcuts
| Shortcut | Description |
|---|---|
Ctrl + C | Kill current process |
Ctrl + Z | Suspend current process (resume with fg) |
Ctrl + D | Exit shell / send EOF |
Ctrl + L | Clear screen |
Ctrl + R | Reverse search command history |
Ctrl + A | Move cursor to beginning of line |
Ctrl + E | Move cursor to end of line |
Ctrl + W | Delete word before cursor |
Ctrl + U | Delete from cursor to beginning of line |
Ctrl + K | Delete from cursor to end of line |
Tab | Auto-complete command or filename |
!! | Repeat last command |
!$ | Last argument of previous command |
history | Show command history |