All Cheat Sheets
Linux / Shell Cheat Sheet
Essential Linux and Bash/Zsh commands — file operations, permissions, processes, networking, and text processing.
File Operations
List files
ls -laChange dir
cd /path/to/dirCopy
cp -r src/ dest/Move / rename
mv old newRemove
rm -rf dir/Create dir
mkdir -p path/to/dirFind files
find . -name '*.ts' -type fFile size
du -sh dir/Disk usage
df -hText Processing
Search text
grep -rn 'pattern' .Replace text
sed -i 's/old/new/g' fileColumn extract
awk '{print $1}' fileSort lines
sort file.txtUnique lines
sort file.txt | uniqCount lines
wc -l fileHead
head -n 20 fileTail (follow)
tail -f logfilePermissions
Change perms
chmod 755 fileChange owner
chown user:group fileMake executable
chmod +x script.shNumeric modes
chmod 644 file # rw-r--r--
chmod 600 key # rw-------
chmod 755 dir # rwxr-xr-xr=4, w=2, x=1 — digits are owner, group, othersSymbolic modes
chmod u+x,g-w,o-r file
chmod a+r file
chmod go= fileu=user, g=group, o=others, a=all; + add, - remove, = set exactlyRecursive
chmod -R 755 dir/
chown -R www-data:www-data /var/wwwDirs vs files
find . -type d -exec chmod 755 {} +
find . -type f -exec chmod 644 {} +Change group
chgrp developers fileDefault mask
umask # show, e.g. 0022
umask 027 # new files 640, dirs 750Setuid / setgid / sticky
chmod u+s bin # 4xxx
chmod g+s dir # 2xxx
chmod +t /tmp # 1xxxView permissions
ls -l file
stat -c '%a %U:%G %n' filestat -c is GNU; on macOS use stat -f '%Lp %Su:%Sg %N'Process Management
List processes
ps aux | grep nodeKill process
kill -9 <pid>Background
command &Top / htop
htopPort usage
lsof -i :3000Networking
Download
curl -O https://example.com/fileHTTP request
curl -X POST -d '{"a":1}' urlDNS lookup
dig example.comPing
ping -c 4 example.comOpen ports
ss -tulnSSH
ssh user@hostSCP copy
scp file user@host:/pathArchives
Create tar.gz
tar -czf archive.tar.gz dir/Extract tar.gz
tar -xzf archive.tar.gzZip
zip -r archive.zip dir/Unzip
unzip archive.zipBash Scripting
Shebang & strict mode
#!/usr/bin/env bash
set -euo pipefail-e exit on error, -u error on unset vars, -o pipefail fail if any pipe stage failsVariables
name="world" # no spaces around =
echo "Hello, $name"
echo "${name}s"Command substitution
today=$(date +%F)Arguments
$0 script name $1 first arg $# arg count
"$@" all args (quoted) $? last exit codeDefault value
port="${PORT:-3000}"If / else
if [[ -f "$file" ]]; then
echo "exists"
elif [[ -z "$var" ]]; then
echo "empty var"
else
echo "nope"
fiTest operators
-f file -d dir -e exists -z empty -n non-empty
== != (strings) -eq -ne -lt -gt (numbers)For loop
for f in *.log; do
echo "$f"
done
for i in {1..5}; do echo "$i"; doneWhile read loop
while IFS= read -r line; do
echo "$line"
done < file.txtCase
case "$1" in
start) start_app ;;
stop) stop_app ;;
*) echo "usage: $0 start|stop"; exit 1 ;;
esacFunctions
greet() {
local name="$1"
echo "Hi, $name"
}
greet "Ada"Exit codes
command || echo "failed"
command && echo "ok"
exit 10 = success, non-zero = failureArithmetic
count=$((count + 1))Arrays
arr=(a b c)
echo "${arr[0]}" "${#arr[@]}"
for x in "${arr[@]}"; do echo "$x"; doneCleanup on exit
trap 'rm -f "$tmp"' EXITRedirection
cmd > out.log 2>&1 # stdout + stderr to file
cmd 2>/dev/null # discard errors
cmd | tee out.logsystemd & journalctl
Service status
systemctl status nginxStart / stop / restart
sudo systemctl start nginx
sudo systemctl stop nginx
sudo systemctl restart nginxReload config
sudo systemctl reload nginxEnable at boot
sudo systemctl enable --now nginx--now also starts it immediatelyDisable at boot
sudo systemctl disable nginxList failed units
systemctl --failedReload unit files
sudo systemctl daemon-reloadRun after editing a .service fileService logs
journalctl -u nginxFollow logs
journalctl -u nginx -fLogs since time
journalctl -u nginx --since "1 hour ago"Errors this boot
journalctl -b -p errFree tools for this
Keep the references coming
One email a week: new and trending developer tools, fresh comparisons, and what shipped. Unsubscribe in one click.