tallge

Reference / Linux commands by task

Linux commands by task

GNU coreutils and util-linux on a current distribution. Flags that differ on BSD or macOS are noted where it matters.

Files and directories

TaskCommand
List with sizes, hidden files, newest lastls -lahtr
Find files by name, case-insensitivefind . -iname "*.log"
Files changed in the last 60 minutesfind /var/log -mmin -60 -type f
Files larger than 100 MBfind / -xdev -type f -size +100M
Copy preserving mode, owner and timescp -a src/ dest/
Sync a directory, showing progressrsync -a --info=progress2 src/ dest/
Symbolic linkln -s /opt/app/current/bin/app /usr/local/bin/app
Details: size, mode, inode, timesstat file
Pack and compress a directorytar -czf backup.tar.gz dir/
List an archive without extractingtar -tzf backup.tar.gz

rsync src/ dest/ copies the contents of src; rsync src dest/ creates dest/src. The trailing slash on the source is the difference.

Text

TaskCommand
Search recursively with line numbersgrep -rn "pattern" .
Only filenames that matchgrep -rl "pattern" .
Extended regex, case-insensitivegrep -Ei "error|fail" app.log
Follow a growing logtail -f /var/log/syslog
Count lineswc -l file
Count identical lines, most frequent firstsort file | uniq -c | sort -rn | head
Column 1 of a space-separated fileawk '{print $1}' access.log
Field 3 of a CSV without quotescut -d, -f3 data.csv
Replace in place, keep a backupsed -i.bak 's/old/new/g' file
Unified diff of two filesdiff -u old.conf new.conf

uniq only collapses adjacent duplicates, which is why it is almost always preceded by sort. On macOS, sed -i needs an explicit suffix argument: sed -i '' 's/a/b/' file.

Processes and services

TaskCommand
All processes with full command linesps aux
Process treeps -ef --forest
Find a process id by namepgrep -a python
Ask a process to stop, then force itkill PID; sleep 5; kill -9 PID
Interactive view sorted by CPUtop -o %CPU
Which process holds a file openlsof /path/to/file
Service status and recent log linessystemctl status app.service
Restart a servicesudo systemctl restart app.service
Follow a service’s logjournalctl -u app.service -f
Errors since bootjournalctl -b -p err
Run detached from the terminalnohup ./job.sh > job.log 2>&1 &

Disks and memory

TaskCommand
Free space per filesystemdf -h
Free inodes (when df shows space but writes fail)df -i
Size of each item in a directorydu -sh -- * | sort -h
Block devices and mount pointslsblk -f
Where a path is mounted fromfindmnt -T /var/lib
Memory, human-readablefree -h
Deleted files still held open (space not freed)lsof +L1

When df and du disagree, a process usually still holds a deleted file, often a rotated log. lsof +L1 lists such files; restarting the process frees the space.

Basic network diagnostics

TaskCommand
Addresses on all interfacesip -br addr
Routing tableip route
Which route a destination would takeip route get 192.0.2.10
Listening TCP and UDP sockets with processessudo ss -tulpn
Is a host reachable (4 packets)ping -c 4 192.0.2.10
Path to a host, hop by hoptracepath 192.0.2.10
DNS lookup, short answerdig +short example.com A
DNS lookup against a specific resolverdig @192.0.2.53 example.com
Is a TCP port opennc -zv 192.0.2.10 443
HTTP status and headerscurl -sI "$URL"
TLS certificate dates and subjectopenssl s_client -connect host:443 -servername host </dev/null 2>/dev/null | openssl x509 -noout -dates -subject

ss replaced netstat, and ip replaced ifconfig and route. The old tools are often not installed on current systems. ICMP may be blocked even when the service works, so a failed ping proves little; nc -zv tests the port you actually care about.

Shell habits that save time

TaskCommand
Re-run the last command with sudosudo !!
Last argument of the previous commandcd !$
Search command historyCtrl+R, then type
Stop a script at the first errorset -euo pipefail
Time a commandtime ./build.sh

The example addresses come from the ranges reserved for documentation (RFC 5737), and example.com is reserved for examples (RFC 2606).

Sources