Sidharth R
  • Home
  • Posts
  • Journal
  • Home
  • Posts
  • Journal
  • Search
  1. Snippets
  2. /
  3. Linux

Linux snippets

Shell commands for everyday Linux administration that each do one job. Copy one straight from this page — the note under each says what it changes and where it differs on macOS.

Disk

Find the largest files in a directory

find . -type f -exec du -h {} + | sort -rh | head -n 20

Run it before deleting anything. du reports space on disk, so sparse files look smaller than ls says.

Find the largest files on the root filesystem

sudo find / -xdev -type f -exec du -h {} + | sort -rh | head -n 20

-xdev keeps it off /proc and network mounts. It reads every inode, so give it minutes.

Show the size of each directory one level down

du -h -d 1 . | sort -rh

Totals per folder rather than per file — the quicker way to pick which tree to dig into.

Networking

See which process is listening on a port

sudo ss -tulpn

Without sudo the process column is empty for other users' sockets, which reads as nothing listening.

See what is holding one port

sudo ss -tlpn 'sport = :8080'

The answer to "address already in use". A socket on 127.0.0.1 is reachable only from this host.

See what is listening on a port, on macOS too

sudo lsof -nP -iTCP:8080 -sTCP:LISTEN

macOS has no ss. lsof is the equivalent there and works on Linux as well.

Services

Follow a systemd service's logs live

journalctl -u nginx -f

Ctrl+C stops it. A service that writes its own log file will not appear here.

Follow a service's logs from the last 100 lines

journalctl -u nginx -f -n 100

Enough history to see what happened just before you attached.

Show a service's errors from the last hour

journalctl -u nginx -p err --since "1 hour ago"

-p err drops the info and debug lines; --since takes plain English like "10 min ago".

List the services that failed to start

systemctl --failed

The first command to run on a box that came up wrong.


  • Home
  • Posts
  • Journal
  • Labs
  • Snippets
  • Quotes
  • Links worth your time
  • About
  • Contact
  • Style guide
  • RSS

© 2026 Sidharth R. · Licensed CC BY-NC-SA 4.0