Linux find largest file in directory recursively using find/du

ADVERTISEMENTS



Linux find largest file in directory recursively using find

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.

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

Open the terminal application Login as root user using the sudo -i command Type du -a /dir/ | sort -n -r | head -n 20 du will estimate file space usage sort will sort out the output of du command 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



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 ## shell alias ## alias ducks='du -cks * | sort -rn | head' ### run it ### ducks

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 NA command combination:

$ sudo find / -type f -printf "%s\t%p

" | sort -n | tail -1

$ find $HOME -type f -printf '%s %p

' | sort -nr | head -10

Sample outputs:

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 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

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