How To Find Largest And Smallest Directories And Files In Linux

Find Largest And Smallest Directories And Files In Linux

As you a System administrator, you must know which directories and files are eating up more space and which are the smallest. This will give you an idea to get rid of unnecessary directories and files which are no longer necessary. Alternatively, you could backup or archive them into a external drives to free up some space. In this brief tutorial, we will discussing how to find largest and smallest directories and files in Linux and Unix-like systems.

Find Largest And Smallest Directories And Files in Linux

First let us see how to find largest directories and files in our Linux system.

1. Find Largest Directories And Files in Linux / Unix

As far as I know, there is no single command to find the largest directories and files. However, we can use some combination of commands to achieve this task.

To find out the top largest ten directories and files in the current working directory, just run:

sudo du -a | sort -n -r | head -n 10

Sample output:

324793508 .
175393112 ./Soft_Backup
114384256 ./Soft_Backup/VHD's
76665704 ./Personal
58237276 ./Soft_Backup/OS Images
48053708 ./Soft_Backup/OS Images/Linux
32940784 ./Personal/Tutors
28189472 ./Downloads
26067640 ./Personal/Study
23741692 ./VirtualBox VMs

sksk_001

Where,

  • du : Disk usage command that estimates file space usage
  • -a : Displays all directories and files
  • sort : Sort lines of text files
  • -n : Compare according to string numerical value
  • -r : Reverse the result of comparisons
  • head : Output the first part of files
  • -n 10 : Print the first 10

To view the above result in human-readable format (in Kb, MB, GB etc), just add the parameter “h” as shown below.

sudo du -ah | sort -n -r | head -n 10

As you see in the above results, we have listed all files and directories and its sub-directories in the current working directory.

To display the largest directories and files of a particular directory, for example /var, run:

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

Sample output: 

9317388 /var
5159660 /var/cache
5129476 /var/cache/pacman
5129472 /var/cache/pacman/pkg
2304368 /var/lib
1979572 /var/lib/docker
1976628 /var/lib/docker/devicemapper
1976532 /var/lib/docker/devicemapper/devicemapper
1974172 /var/lib/docker/devicemapper/devicemapper/data
1853276 /var/log

sksk_003

To display the above results in human-readable format, add “-h” parameter:

sudo /var du -ah | sort -n -r | head -n 10

Let us find out the largest files in the current working directory and its sub-directories:

sudo find -printf '%s %p\n'| sort -nr | head -10

Sample output:

14621343744 ./Soft_Backup/VHD's/openSUSE 42.1 Leap Desktop/openSUSE 42.1 Leap Desktop.vdi
13767896472 ./Downloads/3.0.6/eos-amd64-amd64/en/eos-eos3.0-amd64-amd64.161109-081728.en.img.xz.part
12878610432 ./VirtualBox VMs/Ubuntu 16.04 LTS Desktop/Ubuntu 16.04 LTS Desktop.vdi
12834570240 ./Soft_Backup/VHD's/Ubuntu 16.04 LTS Desktop/Ubuntu 16.04 LTS Desktop.vdi
9360637952 ./VirtualBox VMs/Fedora 24 Desktop/Fedora 24 Desktop.vdi
7785676800 ./Soft_Backup/VHD's/Ubuntu 14.04 LTS desktop/Ubuntu 14.04 LTS desktop.vdi
7523532800 ./Soft_Backup/VHD's/Fedora 23 desktop/Fedora 23 desktop.vdi
7223640064 ./Soft_Backup/VHD's/FreeBSD 10.3 MATE desktop/FreeBSD 10.3 MATE desktop.vdi
6510608384 ./Soft_Backup/VHD's/Windows 7/Windows 7.vdi
6323896320 ./Soft_Backup/VHD's/Fedora 23 desktop/.goutputstream-UT19IY

sksk_004

Also, you can skip the directories and display only the files by adding “-type f” flag in the above command:

sudo find -type f -printf '%s %p\n'| sort -nr | head -10

Sample output:

14621343744 ./Soft_Backup/VHD's/openSUSE 42.1 Leap Desktop/openSUSE 42.1 Leap Desktop.vdi
13767896472 ./Downloads/3.0.6/eos-amd64-amd64/en/eos-eos3.0-amd64-amd64.161109-081728.en.img.xz.part
12878610432 ./VirtualBox VMs/Ubuntu 16.04 LTS Desktop/Ubuntu 16.04 LTS Desktop.vdi
12834570240 ./Soft_Backup/VHD's/Ubuntu 16.04 LTS Desktop/Ubuntu 16.04 LTS Desktop.vdi
9360637952 ./VirtualBox VMs/Fedora 24 Desktop/Fedora 24 Desktop.vdi
7785676800 ./Soft_Backup/VHD's/Ubuntu 14.04 LTS desktop/Ubuntu 14.04 LTS desktop.vdi
7523532800 ./Soft_Backup/VHD's/Fedora 23 desktop/Fedora 23 desktop.vdi
7223640064 ./Soft_Backup/VHD's/FreeBSD 10.3 MATE desktop/FreeBSD 10.3 MATE desktop.vdi
6510608384 ./Soft_Backup/VHD's/Windows 7/Windows 7.vdi
6323896320 ./Soft_Backup/VHD's/Fedora 23 desktop/.goutputstream-UT19IY

sksk_005

To find out the largest files in a specific directory (Ex. /var) and its sub-directories just mention the path of the directory as shown below:

sudo find /var -printf '%s %p\n'| sort -nr | head -10

You have now basic idea about how to find the largest files and directories. How about smallest files and directories? That’s also easy to find out.

2. Find Smallest Directories And Files in Linux / Unix

To view the top ten smallest directories in the current working directory, run:

du -S . | sort -n | head -10

Sample output:

4 ./.adobe
4 ./.adobe/Flash_Player
4 ./.adobe/Flash_Player/AssetCache
4 ./.alkasir/log
4 ./.alkasir/torpt
4 ./.alkasir/torpt/ptc
4 ./.bundle/cache
4 ./.bundle/cache/compact_index
4 ./.cache/chromium/Default
4 ./.cache/deepin

sksk_006

To view the smallest directories in a specific location, for example /var, run:

du -S /var | sort -n | head -10

To view the top ten smallest files only in the current working directory, run:

ls -lSr | head -10

Sample output:

total 116
-rw-r--r-- 1 sk users 62 Nov 5 12:15 Inter.txt
drwxr-xr-x 2 sk users 4096 May 9 2016 Wallpapers
drwxr-xr-x 5 sk users 4096 Nov 25 17:24 VirtualBox VMs
drwxr-xr-x 2 sk users 4096 May 11 2016 Videos
drwxr-xr-x 6 sk users 4096 Oct 24 17:32 usbkill
drwxr-xr-x 2 sk users 4096 May 11 2016 Templates
drwxr-xr-x 5 sk users 4096 May 4 2016 Soft_Backup
drwxr-xr-x 2 sk users 4096 Oct 25 12:32 snap
drwxr-xr-x 2 sk users 4096 May 11 2016 Public

sksk_007

Likewise, to view the top ten smallest files only in a specific directory, for example /var, just run:

ls -lSr /var | head -10

You know now how to find the largest and smallest directories and files in your Linux and Unix operating systems. If you find this guide useful, please share it on your social networks and support us.

Cheers!

Source and References:

Thanks for stopping by!

How can I benefit from this blog:

Have a Good day!!

You may also like...