If you concern about disk space, you must know which directories and files are consuming more disk space and which are the smallest. This will give you an idea to get rid of the unnecessary directories and files which are occupied the drive space. Also, you can backup or archive them into a external drives to free up some space. In this brief tutorial, we will be discussing how to find largest and smallest directories and files in Linux and Unix-like operating systems.
Table of Contents
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 and 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 ten largest 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
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
To display the above results in human-readable format, add "-h" parameter:
$ sudo du -ah /var | 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
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
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 and 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
To view the smallest directories in a specific location, for example /var, run:
$ sudo 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
Likewise, to view the top ten smallest files only in a specific directory, for example /var, just run:
$ sudo ls -lSr /var | head -10
Suggested read:
You know now how to find the largest and smallest directories and files in your Linux and Unix operating systems.
Resources:
3 comments
Thanks. Learning something new every day. I always bookmark these pages.
I use ‘du -a -h –max-depth=1 | sort -hr’ to see the largest directories.
sudo /var du -ah | sort -n -r | head -n 10
should be
sudo du -ah /var | sort -n -r | head -n 10
Good catch. Corrected now. Thanks for the heads up.