All References

Bash Cheatsheet

Essential Bash shell commands, keyboard shortcuts, variables, conditionals, loops, functions, pipes, and redirection. Applies to bash 4+ and zsh.

Navigation

Command Description
pwdPrint current directory
cd <dir>Change directory
cd ~Go to home directory
cd -Go to previous directory
lsList directory contents
ls -laList all files including hidden, long format
pushd <dir>Push directory onto stack and cd to it
popdPop directory from stack and return to it

File Operations

Command Description
cp src dstCopy file
cp -r src dstCopy directory recursively
mv src dstMove or rename file/directory
rm <file>Delete file
rm -rf <dir>Delete directory and contents (use with care)
mkdir <dir>Create directory
mkdir -p a/b/cCreate nested directories
touch <file>Create empty file or update timestamp
ln -s target linkCreate symbolic link
find . -name "*.log"Find files matching pattern
chmod 755 <file>Set file permissions
chown user:group <file>Change file owner and group

Viewing Files

Command Description
cat <file>Print file contents
less <file>Paginated viewer (q to quit, / to search)
head -n 20 <file>Show first 20 lines
tail -n 20 <file>Show last 20 lines
tail -f <file>Follow file as it grows (useful for logs)
grep "pattern" <file>Search file for pattern
grep -r "pattern" .Recursive search in current directory
wc -l <file>Count lines in file
sort <file>Sort lines alphabetically
uniqRemove duplicate adjacent lines (use after sort)

Redirection & Pipes

Syntax Description
cmd > fileRedirect stdout to file (overwrite)
cmd >> fileRedirect stdout to file (append)
cmd < fileFeed file as stdin to cmd
cmd1 | cmd2Pipe stdout of cmd1 to stdin of cmd2
cmd 2> err.logRedirect stderr to file
cmd &> fileRedirect both stdout and stderr to file
cmd 2>/dev/nullSuppress stderr
cmd1 && cmd2Run cmd2 only if cmd1 succeeds
cmd1 || cmd2Run cmd2 only if cmd1 fails
cmd1 ; cmd2Run both regardless of exit codes

Variables

Syntax Description
VAR=valueAssign (no spaces around =)
$VAR or ${VAR}Expand variable
${VAR:-default}Use default if VAR is unset or empty
${VAR:?error}Exit with error if VAR is unset
readonly VAR=valueMake variable read-only
export VARMake variable available to child processes
unset VARRemove variable
${#VAR}Length of variable value
$0, $1, $2…Script name, positional args
$@All arguments as separate words
$#Number of arguments
$?Exit code of last command
$$PID of current shell

Conditionals

# if / elif / else
if [[ "$VAR" == "hello" ]]; then
  echo "greeting"
elif [[ "$VAR" == "bye" ]]; then
  echo "farewell"
else
  echo "other"
fi

# File tests
if [[ -f "$FILE" ]]; then echo "is a file"; fi
if [[ -d "$DIR" ]]; then echo "is a directory"; fi
if [[ -z "$STR" ]]; then echo "empty string"; fi
if [[ -n "$STR" ]]; then echo "non-empty string"; fi
if [[ -e "$PATH" ]]; then echo "exists"; fi
Test Flag Meaning
-fIs a regular file
-dIs a directory
-eExists (file or dir)
-zString is empty
-nString is non-empty
-r / -w / -xFile is readable / writable / executable
-eq / -ne / -lt / -gtNumeric: equal / not equal / less / greater

Loops

# for loop over values
for item in a b c; do
  echo "$item"
done

# for loop over files
for f in *.txt; do
  echo "$f"
done

# C-style for loop
for ((i=0; i<5; i++)); do
  echo "$i"
done

# while loop
while [[ $count -lt 10 ]]; do
  ((count++))
done

# until loop
until [[ $count -ge 10 ]]; do
  ((count++))
done

Functions

# Define
greet() {
  local name="$1"       # local variable
  echo "Hello, $name!"
  return 0              # exit code
}

# Call
greet "World"
result=$(greet "World") # capture output

Keyboard Shortcuts

Shortcut Action
Ctrl+CInterrupt (kill) current process
Ctrl+ZSuspend current process (resume with fg)
Ctrl+DSend EOF / exit shell
Ctrl+LClear screen
Ctrl+RSearch command history (reverse search)
!!Re-run last command (e.g. sudo !!)
Ctrl+AMove cursor to start of line
Ctrl+EMove cursor to end of line
Ctrl+WDelete word to the left
TabAutocomplete command or path

Process Management

Command Description
ps auxList all running processes
kill <pid>Send SIGTERM to process
kill -9 <pid>Force kill (SIGKILL)
pkill <name>Kill processes by name
jobsList background jobs
bg %1Resume job 1 in background
fg %1Bring job 1 to foreground
nohup cmd &Run cmd immune to hangups, in background

Frequently Asked Questions

How do I check if a file exists in bash?

Use the -f flag inside a conditional: if [[ -f "/path/to/file" ]]; then echo "exists"; fi. To check for a directory, use -d. To check that a path exists regardless of type, use -e. Always quote the path in case it contains spaces: [[ -f "$MYPATH" ]].

What is the difference between single and double quotes in bash?

Double quotes ("...") allow variable expansion and command substitution inside them — $VAR and $(cmd) are expanded. Single quotes ('...') treat everything literally — no expansion at all. Use double quotes when you need variable values in a string. Use single quotes when you want the exact literal string, such as regex patterns or awk scripts that contain $.

How do I loop over files in bash?

Use a glob in a for loop: for f in *.txt; do echo "$f"; done. Always quote "$f" to handle filenames with spaces. To loop recursively over all files, you can use find . -name "*.txt" -exec echo \; or enable globstar with shopt -s globstar and then use for f in **/*.txt; do ....