Bash Cheat Sheet

Quick reference for Bash shell commands, scripting syntax, file operations, text processing, and common one-liners. Essential for every developer.

File & Directory Operations Text Processing Pipes & Redirection Variables & Scripting Process Management Networking & System

File & Directory Operations

ls -la List all files with details (including hidden)
cd /path/to/dir Change directory
pwd Print working directory
mkdir -p dir/subdir Create nested directories
cp -r src/ dest/ Copy directory recursively
mv old new Move or rename file/directory
rm -rf dir/ Remove directory and contents (careful!)
touch file.txt Create empty file or update timestamp
ln -s target link Create symbolic link
find . -name "*.js" Find files by name pattern

Text Processing

cat file.txt Display file contents
head -n 20 file First 20 lines
tail -f logfile Follow log file in real-time
grep -r "pattern" . Search recursively in files
grep -i "text" file Case-insensitive search
sed "s/old/new/g" file Find and replace in file
awk '{print $1}' file Print first column
sort file | uniq -c Sort and count unique lines
wc -l file Count lines in file
cut -d"," -f1,3 file Extract columns 1 and 3 (comma-delimited)

Pipes & Redirection

cmd1 | cmd2 Pipe output of cmd1 to cmd2
cmd > file Redirect stdout to file (overwrite)
cmd >> file Append stdout to file
cmd 2>&1 Redirect stderr to stdout
cmd &> /dev/null Discard all output
cmd < file Read stdin from file
cmd1 && cmd2 Run cmd2 only if cmd1 succeeds
cmd1 || cmd2 Run cmd2 only if cmd1 fails
$(command) Command substitution
cmd1 | tee file | cmd2 Split output to file and pipe

Variables & Scripting

VAR="value" Set variable (no spaces around =)
echo "$VAR" Use variable (double quotes preserve spaces)
export VAR Make variable available to child processes
${VAR:-default} Use default if VAR is unset
$1, $2, $@, $# Script arguments, all args, arg count
$? Exit code of last command
$$ Current process ID
if [ -f file ]; then ... fi Conditional (file exists)
for i in 1 2 3; do ... done For loop
while read line; do ... done < file Read file line by line

Process Management

ps aux List all running processes
ps aux | grep name Find process by name
kill PID / kill -9 PID Terminate / force kill process
cmd & Run command in background
jobs / fg / bg List/foreground/background jobs
nohup cmd & Run command immune to hangups
top / htop Monitor system processes
Ctrl+C / Ctrl+Z Interrupt / suspend foreground process
xargs -I{} cmd {} Build commands from stdin
time cmd Measure command execution time

Networking & System

curl -s https://url HTTP request (silent)
curl -X POST -d "data" url POST request with data
wget url Download file from URL
ssh user@host Connect to remote host
scp file user@host:/path Copy file to remote host
df -h Disk space usage (human-readable)
du -sh dir/ Directory size
chmod 755 file Set file permissions
chown user:group file Change file ownership
tar -czf archive.tar.gz dir/ Create compressed archive

More Cheat Sheets