How to Sorting Files by Size in Linux

Niraj Menon Feb 02, 2024
  1. Finding the Largest and Smallest Files in a Directory on Linux
  2. Finding the Size of All Directories in Linux
  3. Finding Files Larger Than a Specific Size in Linux
  4. Viewing the Disk Usage in Linux
How to Sorting Files by Size in Linux

Sometimes, you’d like to do some system deep-cleaning by finding unnecessarily large files and deleting them or removing files smaller than a predetermined size, such as logs. Linux offers various utilities that can help us find such files when used in conjunction.

This tutorial will go into how to use find for everyday uses, such as finding the files in a folder based on their size.

Finding the Largest and Smallest Files in a Directory on Linux

To find the largest files in a given folder, we can use the du and sort commands.

user@linux:~$ ls -lh
-rw-r--r-- 1 user user 8.0M Jan 1 00:00 a
-rw-r--r-- 1 user user 4.0M Jan 1 00:00 b
-rw-r--r-- 1 user user 2.0M Jan 1 00:00 c
-rw-r--r-- 1 user user 1.0M Jan 1 00:00 d
user@linux:~$ du -h * | sort -h
1.0M    d
2.0M    c
4.0M    b
8.0M    a

This will print out the files in order of increasing sizes, so the largest file in the directory will be at the end of the program output, while the smallest one will be at the beginning.

Note the use of the -h flag - this tells the commands that the sizes are given in the human-readable form.

Finding the Size of All Directories in Linux

The following command finds and sorts the directories in ascending order of size in your home directory.

user@linux:~$ sudo find /var/ -maxdepth 1 -type d -exec du -sh {} \; | sort -h
4.0K    /var/local
4.0K    /var/mail
4.0K    /var/opt
56K     /var/spool
60K     /var/tmp
92K     /var/snap
7.3M    /var/backups
4.3G    /var/log
4.4G    /var/cache
17G     /var/lib
25G     /var/

Finding Files Larger Than a Specific Size in Linux

If you know a minimum or maximum size for the files you search for, you can use the find command to list such files.

Suppose you would like to find all files larger than 200 MB (200M). We can do this with the following command, which additionally prints out the size of each discovered file. We use sudo to descend into all root-owned directories.

Keep in mind that the output for your run may yield different files.

user@linux:/var$ sudo find . -type f -size +200M -exec ls -lh {} \;
-rw------- 2 root root 363M Jan 1 00:00 ./lib/snapd/snaps/qt513_24.snap
-rw------- 2 root root 363M Jan 1 00:00 ./lib/snapd/cache/cf177ca655544816bb73b6d8e89c83753b96548f105acd563c1bf1b7d0d046bd3e99a96db5bfe912f8a446a8e9d5b6c5

Viewing the Disk Usage in Linux

The Linux command df allows us to see the overall disk usage on each partition of your filesystem, which helps narrow down partitions where space is overutilized. We run the following to find disk usage.

Once again, this can be quite different based on your disk configuration. The entries with snap are called loopback disks, which are virtual disks to which disk images are mounted, as required by the Snap utility on Ubuntu.

user@linux:~$ df -h
Filesystem      Size  Used Avail Use% Mounted on
udev            7.8G     0  7.8G   0% /dev
tmpfs           1.6G  2.1M  1.6G   1% /run
/dev/nvme0n1p6  200G   45G  146G  24% /
tmpfs           7.8G  397M  7.4G   5% /dev/shm
tmpfs           5.0M  4.0K  5.0M   1% /run/lock
/dev/nvme0n1p2   96M   36M   61M  37% /boot/efi
/dev/loop1       56M   56M     0 100% /snap/core18/2253
/dev/loop0      165M  165M     0 100% /snap/gnome-3-28-1804/161
/dev/loop2      512K  512K     0 100% /snap/gifex/3
/dev/loop3       66M   66M     0 100% /snap/gtk-common-themes/1519
/dev/loop4      128K  128K     0 100% /snap/bare/5
/dev/loop6      363M  363M     0 100% /snap/qt513/24
/dev/loop7      100M  100M     0 100% /snap/core/11993
/dev/sda7       1.1T  384G  677G  37% /home
tmpfs           1.6G  1.9M  1.6G   1% /run/user/1000
/dev/loop8       56M   56M     0 100% /snap/core18/2284

Related Article - Linux File