For every Linux system admin, storage is a big issue. It’s especially true when you’re running a server machine where storage can become a big factor. It’s necessary to manage the directories so that enough storage space is available whenever needed. Everyday users can also easily manage their directory for ensuring the best performance and available disk space. Let’s take a look how to find out the largest directories and files under Linux.
The command tool
We’ll be using 2 tools today – “du” and “find”. “du” will show you the biggest directories under your system whereas “find” will show the largest files available on the disk.
Finding the largest directory
At first, let’s start with directory. Run this command for finding out the largest directory under “/home” partition.
du -a /home | sort -n -r | head -n 10
Let’s explain the details of the command:
- du – Estimates the space usage of directory.
- a – Display all the files & folders
- sort – Sort text file lines
- -n – Compare according to the numeric value of string
- -r – Reverse the comparison(s) result
- head – Output the first file part
- -n (under “head” part) – Print N number of lines on the screen.
For searching and finding out the largest directory in the entire, run this command:
du -a | sort -n -r | head -n 5
The size of the files/folders will be shown in bytes. If you want human readable form like KB, MB or GB etc. then run this command line:
du -hs * | sort -rh | head -10
Here, “-h” argument will tell to show human readable form. You can also add the argument in the previous commands.
Finding out the top file sizes
If you’re interested only the top file sizes of your system, then use this command:
find -type f -exec du -Sh {} + | sort -rh | head -n 5
If you’re in need of searching the largest files in a specific directory, then follow this structure:
find ~/Downloads/ -type f -exec du -Sh {} + | sort -rh | head -n 5 OR find ~/Downloads/ -type f -printf "%s %p\n" | sort -rn | head -n 5
Voila! The largest file(s)/directory(s) are discovered! Make changes accordingly to suit your needs. If your system is clogged with piles of images or small files, consider compressing them into a single archive.