Skip to content

Unix CLI Cheat Sheet

Portable commands for Linux, macOS, and other Unix-like systems.

CommandDescription
pwdPrint working directory
cd -Go to previous directory
cd ~Go to home directory
ls -laList all files with details
ls -lahHuman-readable sizes
ls -ltSort by modification time
tree -L 2Directory tree, 2 levels deep
CommandDescription
cp -r src destCopy directory recursively
mv old newMove/rename
rm -rf dirRemove directory recursively
mkdir -p a/b/cCreate nested directories
touch fileCreate file or update timestamp
ln -s target linkCreate symbolic link
stat fileFile details
file filenameDetect file type
CommandDescription
cat filePrint entire file
less filePaginated view (q to quit)
head -n 20 fileFirst 20 lines
tail -n 20 fileLast 20 lines
tail -f fileFollow file (live updates)
wc -l fileLine count
diff file1 file2Compare files
colordiffColored diff
CommandDescription
find . -name "*.js"Find by name
find . -type f -mtime -1Files modified in last day
find . -size +100MFiles larger than 100MB
grep -r "pattern" .Recursive search
grep -rn "pattern" .With line numbers
grep -rl "pattern" .List matching files only
grep -i "pattern"Case insensitive
grep -v "pattern"Invert match
rg "pattern"Ripgrep (faster)
rg -t js "pattern"Ripgrep, JS files only
fd "pattern"Faster find alternative
CommandDescription
sort fileSort lines
sort -u fileSort unique
sort -n fileNumeric sort
uniqRemove adjacent duplicates
uniq -cCount occurrences
cut -d',' -f1,3Extract columns 1 and 3
awk '{print $1}'Print first column
awk -F: '{print $1}'Custom delimiter
sed 's/old/new/g'Replace all occurrences
tr 'a-z' 'A-Z'Translate characters
xargsBuild commands from stdin
Terminal window
# Linux
sed -i 's/old/new/g' file
# macOS (requires empty string for backup)
sed -i '' 's/old/new/g' file
CommandDescription
cmd > fileRedirect stdout to file
cmd >> fileAppend stdout to file
cmd 2> fileRedirect stderr
cmd &> fileRedirect both
cmd1 | cmd2Pipe stdout to next command
cmd1 | tee file | cmd2Pipe and save copy
cmd < fileUse file as stdin
cmd <<< "string"Here string
CommandDescription
ps auxAll processes
ps aux | grep nameFind process
pgrep -f patternGet PIDs by pattern
topInteractive process viewer
htopBetter process viewer
kill PIDTerminate process
kill -9 PIDForce kill
pkill -f patternKill by pattern
killall nameKill by name
jobsList background jobs
bgResume job in background
fgBring job to foreground
nohup cmd &Run immune to hangups
cmd &Run in background
Ctrl+ZSuspend current process
Ctrl+CInterrupt process
CommandDescription
df -hDisk free space
du -sh *Directory sizes
du -sh * | sort -hSorted by size
ncduInteractive disk usage
CommandDescription
curl -I urlHeaders only
curl -o file urlDownload to file
curl -X POST -d "data" urlPOST request
curl -H "Header: value" urlCustom header
wget urlDownload file
ping hostTest connectivity
traceroute hostTrace route
netstat -anNetwork connections
lsof -i :8080What’s on port 8080
ss -tulnListening ports (Linux)
dig domainDNS lookup
nslookup domainDNS lookup
host domainDNS lookup
CommandDescription
chmod 755 filerwxr-xr-x
chmod +x fileAdd execute permission
chmod -R 644 dirRecursive
chown user:group fileChange owner
chown -R user dirRecursive
NumberPermission
7rwx (read, write, execute)
6rw- (read, write)
5r-x (read, execute)
4r— (read only)
0--- (none)

Common: 755 (dirs), 644 (files), 600 (private), 777 (all access)

CommandDescription
tar -czvf archive.tar.gz dirCreate gzipped tarball
tar -xzvf archive.tar.gzExtract gzipped tarball
tar -tf archive.tarList contents
zip -r archive.zip dirCreate zip
unzip archive.zipExtract zip
gzip fileCompress (replaces file)
gunzip file.gzDecompress
CommandDescription
envShow environment variables
echo $VARPrint variable
export VAR=valueSet variable
which cmdPath to command
type cmdCommand type/location
alias ll='ls -la'Create alias
source ~/.bashrcReload config
historyCommand history
!!Repeat last command
!$Last argument of previous cmd
Ctrl+RReverse search history
CommandDescription
ssh user@hostConnect
ssh -i key.pem user@hostWith key file
ssh -L 8080:localhost:80 hostLocal port forward
ssh -R 8080:localhost:80 hostRemote port forward
scp file user@host:pathCopy to remote
scp user@host:path fileCopy from remote
rsync -avz src destSync directories
rsync -avz --delete src destSync with delete
Terminal window
# Find and delete
find . -name "*.tmp" -delete
# Find and execute
find . -name "*.js" -exec wc -l {} +
# Count files by extension
find . -type f | sed 's/.*\.//' | sort | uniq -c | sort -rn
# Watch command output (Linux)
watch -n 2 'command'
# Parallel execution
cat urls.txt | xargs -P 4 -I {} curl {}
# JSON pretty print
cat file.json | python -m json.tool
cat file.json | jq '.'
# Get external IP
curl -s ifconfig.me
# Quick HTTP server
python -m http.server 8000
# Base64 encode/decode
echo "text" | base64
echo "dGV4dAo=" | base64 -d
# Generate random string
openssl rand -hex 16
# Monitor file changes
tail -f log.txt | grep --line-buffered "error"
# Process substitution
diff <(sort file1) <(sort file2)
# Loop over files
for f in *.txt; do echo "$f"; done
# Loop over lines
while read -r line; do echo "$line"; done < file.txt