Putorius
Linux Quick Tips

Find Files & Directories Taking Up the Most Disk Space in Linux

A full file system can spell disaster for a Linux system. Sometimes even if you know the disk is full, it is still hard to find the offending directory. In this Linux quick tip we will show you how find large files and directories.

Find Directories Using Most Disk Space

The du command allows you to check disk usage from the command line. Used alone it will recursively go through each child directory and show it's size. However, if you pair it with a few other commands you can narrow down your search. In the example below we use the du -h command to list the directories in human readable fromat. Then we pipe the du output to sort. This allows us to do a human readable numeric sort in reverse order -hr. This will place the largest directories at the top of the list.

[mcherisi@putorius Desktop]$ du -h | sort -hr | head -10
9.4G	.
4.9G	./Music
4.5G	./ISO_Files
3.6G	./Music/Pink Floyd
1.1G	./Music/AC-DC
362M	./Music/Pink Floyd/2001 - Echoes - The Best of Pink Floyd
344M	./Music/Pink Floyd/1995 - Pulse
252M	./Music/10,000 Maniacs
242M	./Music/Pink Floyd/1988 - Delicate Sound Of Thunder
208M	./Music/Pink Floyd/1973 - Dark Side Of The Moon

A good way to narrow down the search is to limit the du command to only the top directories (max directory depth). This allows you to discover which top level directory is taking up the most space. Then you can change directory into it and do another run. Here we tell the du command to only go one directory deep with the -d 1 option.

[mcherisi@putorius Desktop]$ du -d 1 | sort -n -r
9832936	.
5133352	./Music
4688216	./ISO_Files
11336	./Videos
20	./Pictures
8	./Documents

Now we know that the Music directory is storing the most data. We can cd into that directory and run du again to see which child directory is using the most space.

[mcherisi@putorius Music]$ du -d 1 | sort -n -r
5133352	.
3726032	./Pink Floyd
1149744	./AC-DC
257572	./10,000 Maniacs

This method is especially helpful when you have large amounts of directories.

Find Largest Files & Directories

By default, the du command only shows directories. With the -a (all) option it will show you files in addition to directories.

[mcherisi@putorius Desktop]$ du -ah | sort -hr | head -5
9.4G	.
4.9G	./Music
4.5G	./ISO_Files
3.6G	./Music/Pink Floyd
2.6G	./ISO_Files/kali-linux-2019.4-amd64.iso

As you can see, it is now reporting files as well as directories.

Find Largest Files Only

The du command is not the right tool for the job if we are seeking only files. For this task we must turn to the find command. In the example below we are using find . -type f. This instructs find to look in the current directory for files only. We then pass the output -exec to run against the du -a command.

[mcherisi@putorius Desktop]$ find -type f -exec du -ah {} + | sort -hr | head -5
2.6G	./ISO_Files/kali-linux-2019.4-amd64.iso
1.9G	./ISO_Files/linuxmint-19.3-cinnamon-64bit.iso
55M	./Music/Pink Floyd/1970 - Atom Heart Mother/01 - Atom Heart Mother.mp3
54M	./Music/Pink Floyd/1971 - Meddle/06 - Echoes.mp3
48M	./Music/Pink Floyd/Star Profile-audio documentary/Star Profile-01-Audio Documentary.mp3

Conclusion

Now you know how to find the largest files and folders if you ever run into a full disk. To learn more about the commands we used here follow the links in the resources section below.

Resources

Exit mobile version