Find the largest files and directories in Linux

Introduction

Sometimes it is necessary to know what file(s) or directories are eating up all your disk space. Further, it may be required to find out it at the particular directory location on filesystem such as /tmp/ or /var/ or /home/.

Also it is very necessary to find the unnecessary junks and free up them from your hard disk.

This tutorial describes how to find the largest files and folders in the Linux using find and du commands.

Find Biggest Files and Directories

To find out top biggest directories under /home partition run :

[unixcop@rhel-pc ~]$  du -a /home | sort -n -r | head -n 5
2435568	/home/qadry
2435568	/home
1530928	/home/qadry/.cache
846976	/home/qadry/.cache/mozilla/firefox/c2ivcm3y.default-default
846976	/home/qadry/.cache/mozilla/firefox
[unixcop@rhel-pc ~]$ 

OR

# du -a / 2>/dev/null | sort -n -r | head -n 5

The above two commands display the biggest 5 directories of /home.

Find Largest Directories

To find out the biggest directories in the current working directory run:

[root@rhel-pc var]# du -hsx * | sort -rh | head -5
3.1G	lib
324M	cache
38M	tmp
28M	log
632K	snap
[root@rhel-pc var]#

The Options of du command :

h : Print sizes in human readable format

x : skip directories on different file systems.

s : show only a total for each argument (summary).

sort : Sort lines of text files

r : Reverse the comparisons.

head : Output the first part of files.

n : number of lines

To find out the biggest directories in a specific directory … for example /var directory , run:

[root@rhel-pc ~]# du -hs /var | sort -rh | head -5
7.0G	/var
[root@rhel-pc ~]# 

And To display the largest folders/files including the sub-directories, run:

[root@rhel-pc var]# du -Sh | sort -rh | head -5
1.1G	./lib/snapd/snaps
553M	./lib/snapd/snap/kde-frameworks-5-core18/32/usr/lib/x86_64-linux-gnu
327M	./lib/snapd/snap/gnome-3-28-1804/161/usr/lib/x86_64-linux-gnu
251M	./lib/snapd/snapshots
240M	./lib/snapd/snap/spotify/46/usr/share/spotify
[root@rhel-pc var]# 

To find out top file sizes only, Run:

[root@rhel-pc var]# find -type f -exec du -Sh {} + | sort -rh | head -n 5
261M	./lib/snapd/snaps/kde-frameworks-5-core18_32.snap
251M	./lib/snapd/snapshots/2_spotify_1.1.55.498.gf9a83c60_46.zip
192M	./lib/rpm/Packages
180M	./lib/snapd/snaps/spotify_46.snap
165M	./lib/snapd/snaps/gnome-3-28-1804_161.snap
[root@rhel-pc var]# 

In the above command, the biggest file sizes only were displayed.

To find the largest files in a specified location, include the path besides the find command as shown below:

[root@rhel-pc ~]# find /home/qadry/ -type f -exec du -Sh {} + | sort -rh | head -n 5
138M	/home/qadry/Downloads/Video/▶ TroubleShootingTools.mp4
102M	/home/qadry/.local/share/TelegramDesktop/tupdates/temp/Telegram
102M	/home/qadry/Downloads/Telegram/Telegram
53M	/home/qadry/xdm-setup-7.2.11.tar.xz
53M	/home/qadry/install.sh
[root@rhel-pc ~]# 

The above command will display the largest file from /home/qadry location.

Find the largest file in a directory and its sub-directories using the find command

Type the following GNU/find command:

## Warning: only works with GNU find ##
# find /path/to/dir/ -printf '%s %p\n'| sort -nr | head -5  #for a specified location
# find . -printf '%s %p\n'| sort -nr | head -5

For example:

A specified location: (e.g. /var)

[root@rhel-pc ]# find /var/ -printf '%s %p\n'| sort -nr | head -5
273375232 /var/lib/snapd/snaps/kde-frameworks-5-core18_32.snap
273375232 /var/lib/snapd/cache/127632cc622891a2cd69da70616530d2a15cccf0a9adb50f7d2dca3c1bfc56c0cc9052c39a8b22cadc619a43fd87048f
262885666 /var/lib/snapd/snapshots/2_spotify_1.1.55.498.gf9a83c60_46.zip
201011200 /var/lib/rpm/Packages
188289024 /var/lib/snapd/snaps/spotify_46.snap

OR

Current Working Directory:

[root@rhel-pc var]# find . -printf '%s %p\n'| sort -nr | head -5
273375232 ./lib/snapd/snaps/kde-frameworks-5-core18_32.snap
273375232 ./lib/snapd/cache/127632cc622891a2cd69da70616530d2a15cccf0a9adb50f7d2dca3c1bfc56c0cc9052c39a8b22cadc619a43fd87048f
262885666 ./lib/snapd/snapshots/2_spotify_1.1.55.498.gf9a83c60_46.zip
201011200 ./lib/rpm/Packages
188289024 ./lib/snapd/snaps/spotify_46.snap

Conclusion

You just learned how to search, find and list largest or biggest directories/files in Linux using the combination of du/find and other commands.

Qadry
unixcop Admin

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest articles

You might also likeRELATED

How to Count Number of Files and Sub-directories inside a Directory

Hi guys, In this article we will show you how to count the number of files and sub-directories in a directory using a tree command. Count number...

Find Top 10 IP Addresses Accessing Your Apache Web Server

In this post, you will learn how to Find Top 10 IP Addresses Accessing Your Apache Web Server- The monitoring access to your web server...

How to Install MongoDB in CentOS 8

Introduction MongoDB is a source-available cross-platform document-oriented database program. Classified as a NoSQL database program, MongoDB uses JSON-like documents with optional schemas. MongoDB is developed by MongoDB Inc. and licensed under the Server Side Public License (SSPL). MongoDB is a...