Linux find largest file in directory recursively using find/du

I have 500GB SSD installed on my Linux server. My web server is running out of the disk space. I need to find a biggest or largest file concerning file size on the disk. How do I find largest file in a directory recursively using the find command?

To find a big file concerning file size on disk is easy task if you know how to use the find, du and other command. The du command used to estimate file space usage on Linux system. The output of du passed on to the sort and head command using shell pipes. Let us see how to find largest file in Linux server using various commands.
Tutorial details
Difficulty level Easy
Root privileges No
Requirements Linux
Est. reading time 3 minutes

Linux find largest file in directory recursively using find

The procedure to find largest files including directories in Linux is as follows:

  1. Open the terminal application
  2. Login as root user using the sudo -i command
  3. Type du -a /dir/ | sort -n -r | head -n 20
  4. du will estimate file space usage
  5. sort will sort out the output of du command
  6. head will only show top 20 largest file in /dir/

Linux find a biggest files in /

Run the command:
$ sudo du -a /dir/ | sort -n -r | head -n 20
OR
$ sudo du -a / 2>/dev/null | sort -n -r | head -n 20

The -a option passed to the du command to write counts for all files, not just directories.

Linux find large files quickly with bash alias

One can hunt down disk space hogs with ducks bash shell alias

## shell alias ##  
alias ducks='du -cks * | sort -rn | head'
### run it ###
ducks

How To Find Largest Top 10 Files and Directories On Linux / UNIX / BSD

Finding largest file recursively on Linux bash shell using find

One can only list files and skip the directories with the find command instead of using the du command, sort command and head command combination:
$ sudo find / -type f -printf "%s\t%p\n" | sort -n | tail -1
$ find $HOME -type f -printf '%s %p\n' | sort -nr | head -10

Here is what I got on my systems:

295599646	/home/vivek/backups/lnxpcs-master.zip
302654548	/home/vivek/backups/books/pdfs/unit443.wmv
313499710	/home/vivek/backups/books/pdfs/magzine.rar
340414464	/home/vivek/.local/share/baloo/index
346359808	/home/vivek/isoimages/VMware-VMvisor-Installer-6.7.0-8169922.x86_64.iso
352256000	/home/vivek/install63.iso
830054400	/home/vivek/linux/linux-4.18.8.tar
1014864333	/home/vivek/backups/corpapp/vsnl_9.5.2_E_21_Linux.tar.gz
1216380038	/home/vivek/backups/books/full.edition.tar.gz
1787822080	/home/vivek/Fedora-Workstation-Live-x86_64-28-1.1.iso

Where, find command options are as follows:

  • $HOME – Directory search for files.
  • -type f – Search for regular files only.
  • -printf '%s %p\n' – Force find to use print format on the scren, interpreting \ escapes and % directives. The %s will print file’s size in bytes. Show file name using %p. This speailized output makes it easy to sort out file names using the sort command.

The -n is for numeric sort and the -r passed to sort will reverse the result of comparisons. The head command is used to control and show the first part of files. In other words, only display the top 10 results from previous commands.

Great! I found the largest files on my disk. What next?

Depend upon file/dir type you can either move or delete the file. For example, you cannot remove or move the Linux kernel or diver directories. To delete unwanted file on Linux use the rm command:
rm -i -v /path/to/file
To get rid of all files and its sub-directories recursively use following command:
rm -rf /path/to/folderName
To move file to a usb pen mounted at /mnt/usb/, run the mv command:
mv /path/to/large/file/ /mnt/usb/

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. For more info see this page or man pages of du and find commands:
man du
man find
man sort
man head
man tail


🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 2 comments so far... add one


CategoryList of Unix and Linux commands
Disk space analyzersdf ncdu pydf
File Managementcat cp mkdir tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
2 comments… add one
  • zt01 Jan 17, 2021 @ 5:27

    find command helped me with my shell script as i needed email to see what is going on in /var/www/html/uploads/ directory.

  • Sammeer Apr 1, 2021 @ 6:54

    Thanks a ton!!! This is what I was looking for and you have provided it t the point, clearly without wasting any time.

    The post is a great fast read!

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum