Unix – Find Largest Files / Free Space

I was looking for places to free up some space on our server – I recalled accidentally having a 12GB tar archive at one point of our custom files.

Check free space

$ df -k
# list all free space
/dev/xvda             15117312  12306456   2196456  85% /
devtmpfs                773972       112    773860   1% /dev
none                    774176         0    774176   0% /dev/shm
none                    774176        56    774120   1% /var/run

Find largest files in a given directory

Use the du command to find the size of a given directory.

Use du -h to get human friendly results

$ du -h
8.3G    .

List all files individually

To list all files individually, use the -a flag

$ du -a -h
16K     ./images_6.jpeg
112K    ./18175_proof.jpg
64K     ./100_3829...jpg
284K    ./19913_final.eps
104K    ./super_heroes.jpg
36K     ./siteyanyo.jpg
8.3G    .

List all files and sort by file size

To sort by file size, pipe the output into the bash sort command. The gotcha is that you can’t use -h as sort thinks 1000K is bigger than 8G.

Use the -B flag to specify block size

From du –help:
SIZE may be (or may be an integer optionally followed by) one of following:
kB 1000, K 1024, MB 1000*1000, M 1024*1024, and so on for G, T, P, E, Z, Y.

$ du -aB M | sort -n | tail -n 4
34M     ./Helvetica.pdf
46M     ./Harleking_TT1.psd
47M     ./Logo_1.psd
8469M   .

Note I used tail to get the last 3 lines.

You can also pass the -r flag to sort to reverse the order and get the first 4 lines via head – if that’s more intuitive as the “top” files.

$ du -aB M | sort -nr | head -n 4
8469M   .
47M     ./Logo_1.psd
46M     ./Harleking_TT1.psd
34M     ./Helvetica.pdf

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s