Reducing disk bloat on the command line

20th Nov, 2013 | osx unix

Some handy nix snippets to find which directories are eating into your disk space

Show me it all

First the daddy. Show disk free (df) on all mounts (-h is for (h)uman readable disk sizes)

df -h

Show a breakdown

Show disk usage (du) for a file/directory or set

-h Again human readable

-s Value for each file/directory

-c Show a grant total for all

Rather than specify a file or path I usually cd into the directory and use a wildcard:

cd

du -sch *

To sort by size pipe to, err, sort. With the -h option it groups k, m, g together so not actually in true size order but it's easy enough to see what's what. Alternatively remove the -h and look at big numbers!

du -sh * | sort -n

These commands will miss dot files by default. If you need to see them use

shopt -s dotglob

Show recent file changes

Want to see the preference files updated when you launched an app? Or can't remember what you've been doing recently? Show files changed in the last 10 minutes.

find <path> -type f -cmin -10

To widen the net to longer durations, say files updated in the last 10 days use ctime

find <path> -type f -ctime -10

Time defaults to days but alternative units can be specified:

s: second

m: minute

h: hour

d: day

w: week

Show recent file access

For file access use either amin or atime with similar pattern.

find <path> -type f -atime -10

find <path> -type f -amin -10

Do dangerous stuff

Please use without effect of medication. Now you've found files that are possibly clogging up space (how about old backup/cache/log files?) with care you can delete files. Warning it's recursive. You can/should run the command without the -delete to see what's going to happen first.

# Delete all files in current directly modified more than 100 days ago

find . -type f -mtime +100d -delete