Linux Command Cheatsheet: Disk usage and filesize cheatsheet


There are two main commands when it comes to checking disk space and filesize (/directory size): df and du.

Here is a quick cheatsheet with the various useful commands to check for disk space, disk usage, etc on linux (/Mac OS) machines:

The commands:

df - report file system disk space usage (show total hard drive disk usage)

Introduction to df

The df command is for checking the total size (/space remaining) for your file systems/drives.

Show overview of all drives (/volumes) on the machine - df

When df is used without any options, it'll give a nice overview of the volumes on the machine.


Sample output:

Filesystem     1K-blocks    Used Available Use% Mounted on
/dev/vda1       41266360 5666600  33486296  15% /
devtmpfs          930740       0    930740   0% /dev
tmpfs             941376       0    941376   0% /dev/shm
tmpfs             941376  100244    841132  11% /run
tmpfs             941376       0    941376   0% /sys/fs/cgroup
tmpfs             188276       0    188276   0% /run/user/0

Changing df into a more human readable format

But the above isn't so easy to read, so add a -h option to show it in human readable output:

df -h

Filesystem      Size  Used Avail Use% Mounted on
/dev/vda1        40G  5.5G   32G  15% /
devtmpfs        909M     0  909M   0% /dev
tmpfs           920M     0  920M   0% /dev/shm
tmpfs           920M   98M  822M  11% /run
tmpfs           920M     0  920M   0% /sys/fs/cgroup
tmpfs           184M     0  184M   0% /run/user/0

Only show a certain filesystem

If you want to see info about only a certain filesystem, then you can do it like this:

df -h /dev/vda1

Filesystem      Size  Used Avail Use% Mounted on
/dev/vda1        40G  5.5G   32G  15% /

Show the output in megabytes

To show the output with the sizes in megabytes, use the -m option. For example:

df -m /dev/vda1

Filesystem     1M-blocks  Used Available Use% Mounted on
/dev/vda1          40300  5534     32702  15% /

To see the type of of filesystem, use -T. For example:

df -m -T /dev/vda1

Filesystem     Type 1M-blocks  Used Available Use% Mounted on
/dev/vda1      ext4     40300  5534     32702  15% /

du - show file space usage (file sizes, total directory sizes, etc)

Introduction to du

I personally find myself using du quite often, mostly to see the total size of directories.

Basic usage of du

If you run du with no options it'll show each file/directory within the current working directory, along with its size (in bytes):


8       Documents/GitHub/BlogEtc/migrations
56      Documents/GitHub/BlogEtc/tests
16      Documents/GitHub/BlogEtc/.git/objects/59
8       Documents/GitHub/BlogEtc/.git/objects/92
16      Documents/GitHub/BlogEtc/.git/objects/0c
16      Documents/GitHub/BlogEtc/.git/objects/3e
8       Documents/GitHub/BlogEtc/.git/objects/50
16      Documents/GitHub/BlogEtc/.git/objects/57
32      Documents/GitHub/BlogEtc/.git/objects/3b
8       Documents/GitHub/BlogEtc/.git/objects/6f
8       Documents/GitHub/BlogEtc/.git/objects/03

Changing it into a human readable format

The above is good for sorting, but if you want to read it with your own eyes you will probably want to convert the bytes into a human readable format. Use the -h option to get the human readable version:

du -h

4.0K    Documents/GitHub/BlogEtc/migrations
 28K    Documents/GitHub/BlogEtc/tests
8.0K    Documents/GitHub/BlogEtc/.git/objects/59
4.0K    Documents/GitHub/BlogEtc/.git/objects/92
4.0K    Documents/GitHub/BlogEtc/.git/objects/50
8.0K    Documents/GitHub/BlogEtc/.git/objects/57
 16K    Documents/GitHub/BlogEtc/.git/objects/3b
4.0K    Documents/GitHub/BlogEtc/.git/objects/9b
 12K    Documents/GitHub/BlogEtc/.git/objects/04
4.0K    Documents/GitHub/BlogEtc/.git/objects/56

du for a specific directory

Normally you will want to enter a directory - just append it to the command like du -h /home/your-dir/.

du /your-directory/

Show all files (not just directories)

Use the following option (-a) to show all files, not just directories

du -a /your-directory/

Limit du to a certain number of subdirectories

You can do this with the max depth option. For example to show a max of 1 sub level (subdirectory):

du -d 1 /your-dir/

Obviously you should change the 1 to whatever you need!

Give a summary (total size of directory) - this is the most used option

This will give a total size of a directory - and it is a commonly used option for du.

du -s /var

538036	/var

Give a summary of each directory within a directory - second most used option

But sometimes you don't want a summary of just the one directory - you want to know how large each directory is within a directory. For example, to show how much space each website is using, if they are all in /home.

To do this, just add a * to the end, for example:

du -s /var/*

4	/var/adm
82736	/var/cache
4	/var/crash
20	/var/db
8	/var/empty
4	/var/games
4	/var/gopher
12	/var/kerberos
315512	/var/lib

Show the biggest files (using the sort command with du)

You can pipe the output of du to sort to sort it.

This won't work with the -h (human readable du output).

du -a /var/ | sort -n -r

10625624        /var/
6291456 /var//vm
3828208 /var//db
3299200 /var//db/dyld
2286312 /var//db/dyld/dyld_shared_cache_x86_64h
2097152 /var//vm/swapfile1

Show the biggest directories and sort by largest

du -s /var/* | sort -n -r

6291456 /var/vm
4956224 /var/db
1021944 /var/folders
92928   /var/log
4488    /var/networkd

Systems that these commands will work on

All of these commands for showing disk usage and remaining space will work on almost all *nix based operating systems, including Cent OS, Ubuntu, CoreOS, Debian, Mac OS etc. If you are using bash in a terminal, you can probably count on having du and df...!

Comments Linux Command Cheatsheet: Disk usage and filesize cheatsheet