Skip to content

Linux & shell: Clean up & disk

Linux & shell commands for clean up & disk (tags: #cleanup #disk #backup).

Index
5 items

Linux: Check disk usage per filesystem

Safe

Size, used and available space per mount, with filesystem type.

df -hT
What each part does
  1. dfdisk free: size, used, available per mounted filesystem
  2. -hhuman-readable sizes (G, M)
  3. -Tshow the filesystem type (ext4, xfs, tmpfs...)
Example
Filesystem     Type  Size  Used Avail Use% Mounted on
/dev/nvme0n1p1 ext4   50G   49G  1.0G  98% /

My note Also run df -i: a disk can be 'full' on inodes with space left.

Also known as: df -h · disk full · no space left on device

Linux: Find the biggest directories

Safe

Top 20 directories by size under a path, staying on one filesystem (-x).

sudo du -xh --max-depth=1 <path> 2>/dev/null | sort -rh | head -20
What each part does
  1. sudo dudisk usage of directories
  2. -xstay on one filesystem (skip other mounts)
  3. -hhuman-readable sizes
  4. --max-depth=1 <path>one total per direct subdirectory of path
  5. 2>/dev/nullhide permission errors
  6. | sort -rhsort human sizes, largest first
  7. | head -20top 20
Example
$ sudo du -xh --max-depth=1 /var 2>/dev/null | sort -rh | head -20

Also known as: du · what is using disk · disk full

Linux: Find deleted files still holding disk space

Safe

Lists open files whose link count is 0: deleted but still held open by a process, so space isn't freed.

sudo lsof -nP +L1
What each part does
  1. sudo lsoflist open files (as root, to see every process)
  2. -nPno DNS or port-name lookups: faster
  3. +L1only files with link count < 1: deleted but still open; restart the process to free the space

My note Restart (or truncate via /proc/<pid>/fd/<n>) the holding process to free the space.

Also known as: df full but du small · deleted files open · lsof +L1

Linux: Vacuum old journal logs

Caution

Delete archived journal files older than N days to reclaim disk.

sudo journalctl --vacuum-time=<days>d
What each part does
  1. sudo journalctlmanage the journal as root
  2. --vacuum-time=<days>ddelete archived journal files older than N days (or --vacuum-size=500M)

My note Check size first: journalctl --disk-usage

Also known as: journal too big · /var/log/journal

Linux: Delete old rotated logs

Destructive

Remove compressed rotated logs older than N days. Run without -delete first to preview.

sudo find /var/log -type f -name "*.gz" -mtime +<days> -delete
What each part does
  1. sudo find /var/logsearch under /var/log as root
  2. -type ffiles only
  3. -name "*.gz"compressed rotated logs
  4. -mtime +<days>last modified more than N days ago
  5. -deletedelete matches (run without it first to see the list)

Also known as: clean /var/log · no space left on device

My DevOps Playbook by Angelo John Calleja

New runbook

GitHub not connected

Saving is handled by the site: the GitHub token is stored in Cloudflare, not in your browser. Enter the editor password (the EDITOR_PASSWORD secret) to unlock saving in this browser.

Saving commits to gelogrammer/My_DevOps_Playbook. Create a fine-grained token with access to only this repo andContents: Read and write, then paste it here. It is stored only in this browser.

src/content/docs/…