Running out of disk space? Here’s how to find large files in Ubuntu (and other Linux distributions) without any fuss using the du and ncdu commands.
The du Command
The du (Disk Usage) command does what it says it does – tells you about the disk usage of files in a given directory.
du Command Syntax
du OPTIONS PATH
Note that: – OPTIONS is an optional space-separated list of options from the du command manual – OPTIONS will allow you to specify things like maximum and minimum file sizes to include, what unit to measure file sizes in – PATH should be the path of the folder you wish to view disk usage of
du has a lot of options – rather than reproduce them here, you can find the full user manual for the du command by running:
man du
I’ll cover the most common usage in the examples below.
du Examples
Often, it’s easiest to find files or folders over a certain size rather than showing all files. Most system files are small – as are most documents (text, spreadsheets only containing words, numbers). To find the real space eaters, like wayward cache files, logs that have grown too large, or old movie downloads, you can find files over 10MB with the below command:
The following will show files in the root filesystem (/) with a size over 10M and list their details:
find / -size +10M -ls
The following will find files between 10M and 100M and print their details.
find / -size +10M -size -100M -ls
The above examples use the -size and -ls options for the du command to define which files are searched and how their details should be displayed.
Note that if you are investigating directories that other users own, you may need to use sudo to run du with administrative/root privileges.
You can use STDIN/STDOUT redirection to tailor your results further:
du -a -h . | sort -r -h | head -20
The above will run du in the current directory (.) with the -a (all files) and -h (human-readable – prints sizes in friendlier units) flags, then pipes the output to the sort command, which reverses (-r) and compares human readable numbers (-h) the list so that it is ordered largest-to-smallest file size. This is then piped to the head command, which truncates the results to the top 20 files.
The ncdu Command (Makes it Easy)
The ncdu (NCurses Disk Usage) command works similarly to du – but with a friendly user interface that allows you to navigate the filesystem and view details on disk usage for each file and directory on the fly. This makes it super useful for tracking down space-hogs.
It’s not installed in Ubuntu by default, but can be by running:
sudo apt install ncdu
ncdu Syntax
Simply run:
ncdu
To launch ncdu.
Again, you can find the full user manual for the ncdu command by running:
man ncdu
ncdu Examples
After initial loading, ncdu looks like this.
Every file and folder has its size listed next to it – so you can immediately see what’s using up space.
Neat!