Find the Largest Top 10 Files and Directories On a Linux

LinuxFile SystemUnix

Sometimes, it becomes important to find which files or directories are ingesting up, all of your disk area on a Linux. Similarly, we should be able to discover a particular directory location on file system such as /tmp/ or /var/ or /domestic/. This article will help you to use Unix and Linux commands for finding the most important or biggest files or directories on the file systems.

Although, there is no shortcut command which is available to discover the largest documents/directories on a Linux/UNIX/BSD file system but there is a possibility which we will be showcasing you about.

By aggregating the following three commands (the use of pipes) can help you easily discover a list of largest documents on a Linux machine.

  • du command : It estimates file space usage
  • sort command : Sort lines of text files or given input data
  • head command : Output the first part of files i.e. to display first 10 largest file
  • find command : It Searches file on Linux machine

Use the following command to find the largest Top 10 files and directories on a Linux system –

$ sudo du -a /var | sort -n -r | head -n 10

The sample output should be like this –

1128132      /var
779176       /var/cache
629292       /var/cache/apt
541020       /var/cache/apt/archives
327212       /var/lib
172180       /var/lib/apt
172024       /var/lib/apt/lists
130084       /var/cache/apt-xapian-index
130080       /var/cache/apt-xapian-index/index.1
87556        /var/lib/dpkg

To see human readable output, use the following command –

$ du -hsx * | sort -rh | head -10

The sample output should be like this –

4.4G   Desktop
3.8G   Downloads
149M   en-GB
146M   Apache_OpenOffice_4.1.1_Linux_x86-64_install-deb_en-GB.tar.gz
95M   scala-2.11.4.deb
20M   gawk-4.1.1
4.5M   linux-dash
3.9M   yii-1.1.13.e9e4a0.tar.gz.1
3.9M   yii-1.1.13.e9e4a0.tar.gz

The above command can be better understood with the following explanations –

  • du command -h option : Display sizes in human readable format (e.g., 1K, 234M, 2G).
  • du command -s option : It shows only a total for each argument (summary).
  • du command -x option : Skip directories on different file systems.
  • sort command -r option : Reverse the result of comparisons.
  • sort command -h option : It compares human readable numbers. This is GNU sort specific option only.
  • head command -10 OR -n 10 option : It shows the first 10 lines.

The above command will work for GNU/sort which is installed on a Linux, Other Unix like operating systems uses the following command –

$find /path/to/dir/ -printf '%s %p\n'| sort -nr | head -10
$find . -printf '%s %p\n'| sort -nr | head -10

The sample output should be like this –

185016320 ./Desktop/gdb-7.9.tar
153300495 ./Downloads/apache-storm-1.0.0.tar.gz
152847886 ./Apache_OpenOffice_4.1.1_Linux_x86-64_install-deb_en-GB.tar.gz
98756608 ./scala-2.11.4.deb
96477184 ./.cache/chromium/Default/Cache/data_3
88088576 ./.cache/google-chrome/Default/Cache/data_3
66586000 ./Downloads/Apache24.zip
61919701 ./Downloads/apache-storm-1.0.0/external/flux/flux-examples-1.0.0.jar
55678503 ./Downloads/apache-storm-1.0.0/examples/storm-starter/storm-starter-topologies-1.0.0.jar

To skip directories and only display files, use the following command

$ find /path/to/search/ -type f -printf '%s %p\n'| sort -nr | head -10

or

$ find /path/to/search/ -type f -iname "*.mp4" -printf '%s %p\n'| sort -nr | head -10

Hunt Down Disk Space Hogs with Ducks

Use the following bash shell commands as shown below

$ alias ducks='du -cks * | sort -rn | head'

Use the following command to get top 10 files/dirs eating your disk space-

$ ducks

The sample output should be like this –

5994400   total
4559508   Desktop
673712    Downloads
151596    en-GB
149268    Apache_OpenOffice_4.1.1_Linux_x86-64_install-deb_en-GB.tar.gz
96444     scala-2.11.4.deb
20024     gawk-4.1.1
4544      linux-dash
3952      yii-1.1.13.e9e4a0.tar.gz.1

Congratulations! Now, you know “How to find The Largest Top 10 Files and Directories On a Linux”. We’ll learn more about these types of commands in our next Linux post. Keep reading!

raja
Published on 20-Jan-2020 14:15:31
Advertisements