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 -r src dst
Copy directory recursively
mv src dst
Move or rename file/directory
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
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
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
sort <file>
Sort lines alphabetically
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
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
!$
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