Bash / Shell Cheat Sheet

Essential terminal commands, file operations, text processing, scripting, and keyboard shortcuts for everyday development.

File Operations

CommandDescription
touch fileCreate empty file or update timestamp
mkdir dirCreate a directory
mkdir -p a/b/cCreate nested directories
cp src destCopy file
cp -r src/ dest/Copy directory recursively
mv src destMove or rename file/directory
rm fileRemove file
rm -rf dirRemove directory and contents (force)
ln -s target linkCreate symbolic link
cat fileDisplay file contents
less fileView file with scrolling
head -n 20 fileShow first 20 lines
tail -n 20 fileShow last 20 lines
tail -f fileFollow file in real-time (logs)
wc -l fileCount lines in file
du -sh dirShow directory size (human-readable)
df -hShow disk usage (human-readable)
file filenameDetect file type

Text Processing

CommandDescription
grep "pattern" fileSearch for pattern in file
grep -r "pattern" dirRecursive search in directory
grep -i "pattern" fileCase-insensitive search
grep -n "pattern" fileShow line numbers
grep -c "pattern" fileCount matching lines
grep -v "pattern" fileInvert match (exclude pattern)
sed 's/old/new/g' fileReplace text (all occurrences)
sed -i 's/old/new/g' fileReplace in-place
awk '{print $1}' filePrint first column
awk -F, '{print $2}' filePrint 2nd column (comma delimiter)
sort fileSort lines alphabetically
sort -n fileSort numerically
sort -r fileSort in reverse
uniqRemove adjacent duplicate lines
sort | uniq -cCount occurrences of each line
cut -d',' -f1,3 fileExtract fields 1 and 3 (comma-separated)
tr 'a-z' 'A-Z'Translate lowercase to uppercase
diff file1 file2Compare two files

Permissions

CommandDescription
chmod 755 fileSet permissions (rwxr-xr-x)
chmod +x fileAdd execute permission
chmod -R 644 dirSet permissions recursively
chown user:group fileChange file owner and group
chown -R user dirChange 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

CommandDescription
ps auxList all running processes
ps aux | grep nameFind a process by name
topInteractive process viewer
htopEnhanced interactive process viewer
kill PIDTerminate a process by PID
kill -9 PIDForce kill a process
killall nameKill all processes by name
command &Run command in background
jobsList background jobs
fg %1Bring job 1 to foreground
bg %1Resume job 1 in background
nohup cmd &Run command that survives terminal close
lsof -i :8080Find process using port 8080

Networking

CommandDescription
curl urlFetch URL content
curl -o file urlDownload file
curl -X POST -d 'data' urlSend POST request with data
curl -H "Key: Value" urlSend request with custom header
wget urlDownload file
wget -r urlDownload recursively
ping hostTest network connectivity
ssh user@hostConnect to remote host
scp file user@host:pathCopy file to remote host
scp user@host:file .Copy file from remote host
rsync -avz src/ dest/Sync files (efficient, incremental)
netstat -tulnpShow listening ports
ss -tulnpShow listening ports (modern)
ifconfig / ip addrShow network interfaces

Variables & Environment

SyntaxDescription
NAME="value"Set a variable (no spaces around =)
echo $NAMEUse a variable
echo "${NAME}_suffix"Variable with surrounding text
export NAME="value"Set environment variable (available to child processes)
unset NAMERemove a variable
envList all environment variables
$HOMEHome directory path
$PATHCommand search paths
$USERCurrent username
$PWDCurrent 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

TestDescription
-f fileFile exists and is a regular file
-d dirDirectory exists
-e pathPath exists (file or directory)
-r fileFile is readable
-w fileFile is writable
-x fileFile 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 $bNumbers are equal
$a -lt $ba less than b
$a -gt $ba greater than b

Redirection & Pipes

SyntaxDescription
cmd > fileRedirect stdout to file (overwrite)
cmd >> fileAppend stdout to file
cmd 2> fileRedirect stderr to file
cmd &> fileRedirect both stdout and stderr
cmd 2>&1Redirect stderr to stdout
cmd < fileUse file as stdin
cmd1 | cmd2Pipe stdout of cmd1 to stdin of cmd2
cmd1 | tee file | cmd2Pipe and save output to file simultaneously
cmd1 && cmd2Run cmd2 only if cmd1 succeeds
cmd1 || cmd2Run cmd2 only if cmd1 fails
cmd1 ; cmd2Run both commands sequentially
$(command)Command substitution (use output as value)

Keyboard Shortcuts

ShortcutDescription
Ctrl + CKill current process
Ctrl + ZSuspend current process (resume with fg)
Ctrl + DExit shell / send EOF
Ctrl + LClear screen
Ctrl + RReverse search command history
Ctrl + AMove cursor to beginning of line
Ctrl + EMove cursor to end of line
Ctrl + WDelete word before cursor
Ctrl + UDelete from cursor to beginning of line
Ctrl + KDelete from cursor to end of line
TabAuto-complete command or filename
!!Repeat last command
!$Last argument of previous command
historyShow command history