| Command | What it does |
|---|
ls -lah | list files with sizes, permissions and hidden files |
cd DIR / cd - / cd ~ | change folder / go back / go home |
pwd | print the current folder |
cat FILE / less FILE | print a file / page through it (q quits, /word searches) |
tail -f FILE | follow a log as it grows |
grep -rn "text" DIR | search text in files, with line numbers |
find DIR -name "*.log" | find files by name |
cp -r SRC DST / mv SRC DST / rm -r DIR | copy / move or rename / delete |
mkdir -p a/b/c | create folders, including parents |
chmod +x script.sh / chown user:group FILE | change permissions / owner |
sudo COMMAND | run as root |
ps aux | grep NAME | find a process |
top / htop | live CPU and memory per process |
kill PID / kill -9 PID | stop a process / force it |
df -h / du -sh * | free disk space / size of each item here |
free -h | memory use |
systemctl status NAME | is a service running? |
journalctl -u NAME -f | follow a service's logs |
ss -tulpn | which ports are listening, and by which process |
ip a | IP addresses of each interface |
curl -I URL | HTTP status and headers |
ssh user@host | log in to a remote server |
tar -czf out.tgz DIR / tar -xzf in.tgz | create / extract a compressed archive |
history | grep WORD | find a command you ran before |
man COMMAND / COMMAND --help | read the manual |
| Command | What it does |
|---|
ls -l | long listing: permissions, owner, size, date |
ls -la | include hidden files (names starting with .) |
ls -lt / ls -lS | sort by time / by size |
ls -R | list folders recursively |
tree -L 2 | folder tree, two levels deep |
cd / / cd .. / cd - | root / parent / previous folder |
touch FILE | create an empty file or update its time |
mkdir -p DIR | create a folder and missing parents |
cp FILE DIR/ | copy a file into a folder |
cp -a SRC DST | copy keeping permissions, owners and times |
mv OLD NEW | rename or move |
rm FILE / rm -r DIR / rm -rf DIR | delete a file / a folder / without asking (careful) |
rmdir DIR | delete an empty folder |
ln -s TARGET LINK | create a symbolic link |
stat FILE | size, permissions, times, inode |
file FILE | what kind of file it is |
realpath FILE | full absolute path |
basename PATH / dirname PATH | last part / folder part of a path |
| Command | What it does |
|---|
cat FILE | print the whole file |
cat -n FILE | with line numbers |
less FILE | page through; G end, g start, /text search, n next |
head -n 20 FILE | first 20 lines |
tail -n 50 FILE | last 50 lines |
tail -f FILE | follow new lines (logs) |
tail -F FILE | follow even if the file is rotated |
wc -l FILE | count lines (-w words, -c bytes) |
diff -u A B | compare two files |
nl FILE | number the lines |
tac FILE | print in reverse order |
| Command | What it does |
|---|
grep "text" FILE | lines containing text |
grep -i / -v / -c / -n / -w | ignore case / invert / count / line numbers / whole word |
grep -rn "text" DIR | search recursively |
grep -E "err|warn" FILE | extended regex, either word |
grep -A 3 -B 3 "text" FILE | 3 lines after and before each match |
grep -l "text" *.conf | only names of files that match |
find DIR -name "*.log" | by name (-iname ignores case) |
find DIR -type f -size +100M | files over 100 MB |
find DIR -mtime +7 | modified more than 7 days ago |
find DIR -name "*.tmp" -delete | find and delete |
find DIR -type f -exec chmod 644 {} \; | run a command on each match |
which CMD / type CMD | where a command comes from |
locate NAME | fast search in a file index (updatedb refreshes it) |
| Command | What it does |
|---|
ls -l | shows -rwxr-xr--: type, then user, group, others |
chmod 755 FILE | rwx for owner, r-x for group and others |
chmod 644 FILE | rw- for owner, r-- for the rest |
chmod 600 key.pem | private file: only the owner can read and write |
chmod u+x FILE / chmod g-w FILE / chmod o-r FILE | add or remove one permission |
chmod -R 750 DIR | recursively |
chown user FILE / chown user:group FILE | change owner / owner and group |
chown -R app:app /srv/app | recursively |
umask | default permissions for new files |
sudo -u USER CMD | run as another user |
sudo -i | root shell |
Numbers: read 4, write 2, execute 1. 7 = rwx, 6 = rw-, 5 = r-x, 4 = r--.
| Command | What it does |
|---|
whoami / id | current user / user, groups and IDs |
sudo useradd -m -s /bin/bash NAME | create a user with a home folder |
sudo passwd NAME | set a password |
sudo usermod -aG docker NAME | add a user to a group (log in again to apply) |
sudo userdel -r NAME | delete a user and home folder |
groups NAME | a user's groups |
getent passwd NAME | a user's account line |
su - NAME | switch user |
last / who / w | recent logins / who is logged in / and what they run |
sudo visudo | safely edit sudo rules |
| Command | What it does |
|---|
ps aux | every process with CPU, memory and command |
ps -ef --forest | process tree |
pgrep -a NAME | find processes by name |
top | live view; P sort by CPU, M by memory, k kill, q quit |
htop | friendlier top |
kill PID | ask a process to stop (SIGTERM) |
kill -9 PID | force stop (SIGKILL), last resort |
pkill NAME / killall NAME | stop by name |
CMD & / jobs / fg / bg | run in background / list / foreground / background |
Ctrl+Z | suspend the running command |
nohup CMD & | keep running after logout |
nice -n 10 CMD / renice 5 -p PID | lower priority |
lsof -p PID | files a process has open |
lsof -i :8080 | which process uses port 8080 |
strace -p PID | system calls of a running process |
| Command | What it does |
|---|
uname -a | kernel and architecture |
cat /etc/os-release | distribution and version |
hostnamectl | hostname, OS, kernel |
uptime | running time and load average (1, 5, 15 min) |
nproc / lscpu | number of CPUs / CPU details |
free -h | memory; "available" is what apps can use |
df -h | disk space per filesystem |
df -i | inodes (full inodes also stop writes) |
du -sh DIR | size of a folder |
du -h --max-depth=1 / | sort -h | what fills the disk |
vmstat 1 | CPU, memory, swap, I/O every second |
iostat -xz 1 | disk utilisation and latency |
mpstat -P ALL 1 | CPU per core |
sar -n DEV 1 | network throughput |
dmesg -T | tail | kernel messages (OOM kills, disk errors) |
lsblk | disks and partitions |
lsmod | loaded kernel modules |
| Command | What it does |
|---|
systemctl status NAME | state, recent logs, PID |
sudo systemctl start / stop / restart / reload NAME | control a service |
sudo systemctl enable --now NAME | start now and at boot |
sudo systemctl disable NAME | do not start at boot |
systemctl is-active NAME / is-enabled NAME | quick checks for scripts |
systemctl list-units --type=service --state=failed | failed services |
systemctl list-timers | scheduled timers |
systemctl cat NAME | show the unit file |
sudo systemctl daemon-reload | after editing unit files |
journalctl -u NAME | a service's logs |
journalctl -u NAME -f | follow them |
journalctl -u NAME --since "1 hour ago" | recent only |
journalctl -p err -b | errors since boot |
journalctl --disk-usage | journal size |
/var/log/ | classic log files (syslog, auth.log, nginx/) |
| Command | What it does |
|---|
ip a / ip -br a | addresses (brief) |
ip r | routing table, default gateway |
ss -tulpn | listening TCP/UDP ports with process |
ss -tan state established | open connections |
ping -c 4 HOST | is it reachable? |
traceroute HOST / mtr HOST | path and where it slows down |
dig NAME / dig +short NAME | DNS lookup |
nslookup NAME | DNS lookup (simple) |
cat /etc/resolv.conf | DNS servers in use |
curl -I URL | headers and status only |
curl -sS -o /dev/null -w "%{http_code}" URL | just the status code |
curl -v URL | full request and response, TLS details |
wget URL | download a file |
nc -zv HOST 5432 | is a TCP port open? |
ssh -i key.pem user@host | log in with a key |
ssh -L 8080:localhost:80 user@host | forward a local port through SSH |
scp FILE user@host:/path/ | copy over SSH |
rsync -avz SRC/ user@host:/DST/ | sync folders efficiently |
sudo ufw status / sudo iptables -L -n | firewall rules |
cat /etc/hosts | local name overrides |
| Command | What it does |
|---|
tar -czf out.tar.gz DIR | create a gzip archive |
tar -xzf in.tar.gz -C /dest | extract into a folder |
tar -tzf in.tar.gz | list contents |
tar -cJf out.tar.xz DIR | smaller, slower (xz) |
gzip FILE / gunzip FILE.gz | compress / decompress one file |
zip -r out.zip DIR / unzip in.zip | zip archives |
zcat FILE.gz | less | read a compressed file |
sha256sum FILE / sha256sum -c FILE.sha256 | checksum / verify |
| Ubuntu / Debian | RHEL / Fedora / Amazon Linux | What it does |
|---|
sudo apt update | sudo dnf check-update | refresh package lists |
sudo apt install -y PKG | sudo dnf install -y PKG | install |
sudo apt upgrade | sudo dnf upgrade | upgrade everything |
sudo apt remove PKG | sudo dnf remove PKG | remove |
apt search WORD | dnf search WORD | search |
apt show PKG | dnf info PKG | details |
dpkg -l | rpm -qa | list installed |
dpkg -L PKG | rpm -ql PKG | files a package installed |
| Command | What it does |
|---|
lsblk -f | disks, partitions, filesystems, mount points |
sudo fdisk -l | partition tables |
sudo mkfs.ext4 /dev/xvdf | format a disk (destroys data) |
sudo mount /dev/xvdf /data | mount it |
sudo umount /data | unmount |
cat /etc/fstab | mounts at boot (use UUIDs from blkid) |
sudo growpart /dev/xvda 1 && sudo resize2fs /dev/xvda1 | grow a partition and filesystem after enlarging a cloud disk |
findmnt | mount tree |
Text processing
| Command | What it does |
|---|
cut -d: -f1 /etc/passwd | pick a field |
sort / sort -n / sort -rh | sort text / numbers / human sizes, reversed |
uniq -c | count repeated lines (sort first) |
sort | uniq -c | sort -rn | head | top values of anything |
tr 'a-z' 'A-Z' / tr -d '\r' | translate / delete characters |
awk '{print $1}' FILE | first column (space separated) |
awk -F, '{s+=$3} END {print s}' f.csv | sum a CSV column |
sed -n '10,20p' FILE | print lines 10–20 |
sed -i 's/old/new/g' FILE | replace in place |
xargs -I{} CMD {} | run a command per input line |
tee FILE | save and show output |
jq '.items[].name' f.json | read JSON |
column -t | align columns |
| Syntax | What it does |
|---|
CMD > FILE / >> FILE | output to a file / append |
CMD 2> err.log | errors to a file |
CMD > all.log 2>&1 | output and errors together |
CMD > /dev/null 2>&1 | discard everything |
CMD < FILE | file as input |
A | B | output of A into B |
A && B / A || B | B only if A succeeded / failed |
CMD1 ; CMD2 | one after the other |
$(CMD) | insert a command's output |
| Command | What it does |
|---|
crontab -l / crontab -e | list / edit your cron jobs |
*/5 * * * * CMD | every 5 minutes (minute hour day month weekday) |
0 2 * * * CMD >> /var/log/job.log 2>&1 | daily at 02:00, with a log |
systemctl list-timers | systemd timers |
at 18:00 | run once at a time |
| Keys | What they do |
|---|
Tab / Tab Tab | complete / show options |
Ctrl+C | stop the running command |
Ctrl+L | clear the screen |
Ctrl+R | search command history |
Ctrl+A / Ctrl+E | start / end of the line |
Ctrl+U / Ctrl+K | delete before / after the cursor |
Ctrl+D | log out / end of input |
!! / sudo !! | repeat the last command / with sudo |
!$ | last argument of the previous command |
| Symptom | Run |
|---|
| Server slow | uptime, top, vmstat 1, iostat -xz 1, free -h |
| Disk full | df -h, df -i, du -h --max-depth=1 / | sort -h, journalctl --vacuum-size=500M |
| Service down | systemctl status NAME, journalctl -u NAME -n 100, ss -tulpn |
| Cannot connect | ping, dig, nc -zv HOST PORT, curl -v URL, firewall rules |
| Process killed | dmesg -T | grep -i kill (out of memory) |
| Permission denied | ls -l, id, namei -l /path/to/file |
Get every new cheat sheet by email
We're adding cheat sheets for Terraform, AWS, GitLab CI/CD and more.
One email a week at most. Unsubscribe with one click.