List the Size of Each File and Directory and Sort by Size in Linux


Listing the size of each file and directory is good because it helps a user sort them according to the size. Sorting files according to size is necessary to free up space, make them more organized, etc. You can organize the Linux file system through the command line and graphical methods.

It helps you to quickly locate the files and directories as per their contents available. So in this long guide, we will explain various ways to easily List each directory and sort them according to the size in Linux.

List the Size of Each File and Directory and Sort by Size in Linux

You can easily list the files in Linux as per the size, name, modification time, etc. Here we have to sort each file and directory according to their size. You can sort files and directories by size using the command line and GUI methods. Let us discuss both methods and see how to sort files according to their size.

The Command Line Approach

Mainly files are sorted in Linux using the ls command. Using the du and find command, you can also sort files according to size.

The ls Command

The ls command is a handy tool In Linux and Unix systems to list directory contents. If no operand or option is used with this command, it displays the contents of the current directory on the screen by default.

If none of the options like --sort, -l is used with the ls command, it sorts the entries alphabetically by default. Now we will see how to sort them by file size in Linux using various ls command options. First, we will list all the files in the directory and the hidden files associated with them.

:~$ ls -la
total 180 
drwxr-x---    20 prateek prateek 4096 Mar 3 20:42 
drwxr-xr-x     3 root root 4096 Dec 24 14:50 
-rw            1 prateek prateek 18171 Mar 6 18:03 .bash_history 
-rw-r--r--     1 prateek prateek 220 Dec 24 14:50 .bash_logout 
-rw-r--r--     1 prateek prateek 3771 Dec 24 14:50 .bashrc 
drwx          15 prateek prateek 4096 Feb 12 14:21 
drwx          18 prateek prateek 4096 Feb 12 14:21 
drwxr-xr-x     2 prateek prateek 4096 Dec 24 14:56 
drwxr-xr-x     4 prateek prateek 4096 Feb 18 23:38 
drwxr-xr-x     2 prateek prateek 4096 Feb 20 13:09 
-rw-rw-r--     2 prateek prateek 724 Feb 27 21:25 example.txt 
-rwxrwxr-x     1 prateek prateek 88 Mar 3 18:00 file.sh 
drwx           2 prateek prateek 4096 Feb 13 18:28

In the above command, we have used −

  • -l − Long listing of all the files.

  • -a − Includes hidden files.

You can exclude the hidden files (.) using the -A option in the above command instead of -a.

:~$ ls -lA
total 172 
-rw          1 prateek prateek 18171 Mar 6 18:03 .bash_history 
-rw-r--r     1 prateek prateek 220 Dec 24 14:50 .bash_logout 
-rw-r--r     1 prateek prateek 3771 Dec 24 14:50 .bashrc 
drwx        15 prateek prateek 4096 Feb 12 14:21 
drwx        18 prateek prateek 4096 Feb 12 14:21 
drwxr-xr-x   2 prateek prateek 4096 Dec 24 14:56 
drwxr-xr-x   4 prateek prateek 4096 Feb 18 23:38 
drwxr-xr-x   2 prateek prateek 4096 Feb 20 13:09 
-rw-rw-r     2 prateek prateek 724 Feb 27 21:25 example.txt 
-rwxrwxr-x   1 prateek prateek 88 Mar 3 18:00 
drwx         2 prateek prateek 4096 Feb 13 18:28

In all the above files, you can see their details such as size, permissions, etc., but all these are not sorted according to their size.

You can list them according to their size by using the -s flag with the ls command as follows −

:~$ Ls -laS
total 180 
-rw         1 prateek prateek 18171 Mar 6 18:03 .bash_history 
-rw-r--r    1 tcpdump tcpdump 11517 Feb 25 23:35 prateek.pcap 
-rw         1 prateek prateek 10422 Feb 12 13:46 .viminfo 
drwxr-x--- 20 prateek prateek 4096 Mar 3 20:42 
drwxr-xr-x  3 root root 4096 Dec 24 14:50 
drwx       15 prateek prateek 4096 Feb 12 14:21 
drwx       18 prateek prateek 4096 Feb 12 14:21 
drwxr-xr-x  2 prateek prateek 4096 Dec 24 14:56 
drwxr-xr-x  4 prateek prateek 4096 Feb 18 23:38 
drwxr-xr-x  2 prateek prateek 4096 Feb 20 13:09

As you can see from the above output, it lists all the files from largest to smallest (descending order). You can sort these files from smallest to largest (ascending order) using the -r flag. Here -r indicates reverse order.

:~$ ls -laSr
total 180 
-rW-r--r-- 1 prateek prateek 0 Dec 24 14:57 .sudo_as_admin_successful 
-rW-rW-r-- 1 prateek prateek 16 Mar 4 19:14 myfile.txt 
-rW        1 prateek prateek 20 Feb 13 21:11 .lesshst 
-rW-rW-r   1 prateek prateek 34 Feb 28 20:34 Linux.txt 
-rWXrWXr-x 1 prateek prateek 54 Feb 25 14:55 source.sh -
rW-rW-r    1 prateek prateek 66 Jan 27 19:03 .selected editor 
-rW-rW-r   1 prateek prateek 76 Feb 28 20:11 Linuxl.txt 
-rWXrWXr-x 1 prateek prateek 88 Mar 3 18:00 
-rW-r--r   1 prateek prateek 220 Dec 24 14:50 .bash_logout 
-rW-rW-r   2 prateek prateek 724 Feb 27 21:25 tutorials.txt

With the above command, you can also list subdirectories recursively using the -R flag.

:~$ ls -laSrR
46F505F43 
-rw  1 prateek prateek 311803E9D 10884 Dec 26 21:01 OBB2OCB4AAAA42A943D95DBAED3CE2
-rw  1 prateek prateek 0676BB5A1 10888 Dec 26 21:01 A68B2B01F4C5FD751C8FAAACCC3B17
-rw  1 prateek prateek E8F6A55609 10888 Dec 26 21:01 40328257F373FC856156C6F048B053
-rw  1 prateek prateek 53D066F16 10892 Dec 26 21:01 B675C5F5FFACF874E705146520BEB2
-rw  1 prateek prateek 135B5554CD 10893 Feb 2 22:29 EA34D1B92FA40A8D3F43ABF2B47213
-rw  1 prateek prateek 1F53F3458E 10893 Dec 26 21:01 C59AA89A6D8D512BA32E39B847E2C3
-rw  1 prateek prateek 20217CC51 10893 Dec 26 21:01 57419DD3200B2512D029506439BE37

You can display the file sizes in these outputs in a human-readable format using the -h option.

:~$ ls -laSrRh
46F505F43 
-rw  1 prateek prateek 311803E9D 11K Dec 26 21:01 OBB2OCB4AAAA42A943D95DBAED3CE2
-rw  1 prateek prateek 0676BB5A1 11K Dec 26 21:01 A68B2B01F4C5FD751C8FAAACCC3B17
-rw  1 prateek prateek E8F6A55609 11K Dec 26 21:01 40328257F373FC856156C6F048B053
-rw  1 prateek prateek 53D066F16 11K Dec 26 21:01 B675C5F5FFACF874E705146520BEB2
-rw  1 prateek prateek 135B5554CD 11K Feb 2 22:29 EA34D1B92FA40A8D3F43ABF2B47213
-rw  1 prateek prateek 1F53F3458E 11K Dec 26 21:01 C59AA89A6D8D512BA32E39B847E2C3
-rw  1 prateek prateek 20217CC51 11K Dec 26 21:01 57419DD3200B2512D029506439BE37

In the above output, you can see that the unit size of the file is printed in Kilobytes (K). By default, it prints the file size in kilobytes only. You can also display the file size in a specific unit, like gigabytes, megabytes, etc. For this, use the following command −

:~$ ls -laSrRh --block-size=M
total 1M 
-rW-r--r--  1 prateek prateek OM Dec 24 14:57 .sudo_as_admin_successful 
-rW-rw--r-- 1 prateek prateek 1M Mar 4 19:14 myfile.txt -
-rw----     1 prateek prateek 1M Feb 13 21:11 .lesshst 
-rw-rw-r--- 1 prateek prateek 1M Feb 28 20:34 Linux.txt 
-rwxrwxr-x  1 prateek prateek 1M Feb 25 14:55 soruc.sh 
-rw-rw-r--- 1 prateek prateek 1M Jan 27 19:03 .selected_editor 
-rw-rw-r--- 1 prateek prateek 1M Feb 28 20:11 Linuxl.txt 

In the above command, we have displayed the file size in megabytes(M). You can also exclude the directories when sorting all the files by their size using the below command −

:~$ ls -laSrRh | grep -v '^d'
16K -rw  FE23745F2C8 prateek prateek Feb 25 14:46 CB77C383B7CB98DBBF1B72834482
16K -rw  A173A37ADFFD prateek prateek Feb 7 13:59 CB570E0E6B203DA159A3299C1CBC
20K -rw  70EFB03EECE prateek prateek Feb 2 21:39 CB4E5366CE0DA21FE2642B99C61B
76K -rw  ED96AC4CAEB prateek prateek Feb 28 20:02 CB262BB6FA06C62CE4CE9464940D
52K -rw  Q9B16E211B07 prateek prateek Feb 27 21:26 CB259E3298B967ABA5782E6B1D38
12K -rw  135B5554CD prateek prateek Dec 26 21:01 CB1080EDCC82BFOFB11A6023E173 

The du Command

through the du command, you can also sort files in a specified file parameter. To sort directories and files according to their size through the du command, use the following command −

:~$ du -ah | sort -h
4.0K ./snap/root-framewor /910/.local/s are/mime/application/vnd.oasis.open'o cument.graphics-template.xml 
4.0K ./snap/root-framework/910/.1ocal/share/mime/application/vnd.oasis.opendo cument.graphics.xml 
4.0K ./snap/root-framework/910/.1ocal/share/mime/application/vnd.oasts.opendo cument.image.xml 
4.0K ./snap/root-framework/910/.1ocal/share/mime/application/vnd.oasts.opendo cument.presentation-flat-xml.xml 
4.0K ./snap/root-framework/910/.1ocal/share/mime/application/vnd.oasis.opendo cument.presentation-template.xml 
4.0K ./snap/root-framework/910/.1ocal/share/mime/application/vnd.oasis.opendo cument..resentation.xml 

In the above command, -a displays the size of each file along with directories.

Through the du command, you can also sort files and directories by setting their levels. For example, if you want to sort the files only up to the first level, then use the following command −

:~$ du -ah --max-depth=1 | sort -h

Note − If you want to sort the file size in descending order, add the -r flag in the above command.

You can only sort the entire directories, including their files, through the above approach. Thus this method is not valid for individual files. If you only need to sort the directories by their size, then you can use the following 'du' command as follows −

:~$ du -h | sort -h
4.0K ./.cache/evolution/addressbook/trash 
4.0K ./.cache/evolution/calendar/trash 
4.0K ./.cache/evolution/mail/trash 
4.0K ./.cache/evolution/memos/trash 
4.0K ./.cache/evolution/sources/trash 
4.0K ./.cache/evolution/tasks/trash 
4.0K ./.cache/gnome-desktop-thumbnailer/gstreamer-1.0 
4.0K ./.cache/ibus-table 
4.0K . .cache mesa shader cache 08

The find Command

You can list the directories using the find command and the sort command.

:~$ find . -type d -ls | sort -n -r
1186380 4 drwxrwxr-x 2 prateek prateek 4096 Feb 25 14:45 ./snap/firefox/2391/.1ocal/share/glib-2.0/schemas 
1186378 4 drwxr-xr-x 2 prateek prateek 4096 Feb 28 23:32 ./snap/firefox/common/.cache/mozilla/firefox/e35175b1.default/safebrowsing/google4 
1186371 4 drwxrwxr-x 3 prateek prateek 4096 Feb 25 14:45 ./snap/firefox/2391/.1ocal/share/glib-2.0 
1186369 4 drwxrwxr-x 2 prateek prateek 4096 Feb 25 14:45 ./snap/firefox/2391/.1ocal/share/icons 
1186366 4 drwxrwxr-x 4 prateek prateek 4096 Feb 25 14:45 ./snap/firefox/2391/.1ocal/share 
1186359 4 drwx  2 prateek prateek 4096 Dec 26 21:00 ./snap/firefox/2391/.config/pulse 
1186357 4 drwxrwxr-x 2 prateek prateek 4096 Feb 28 20:01 ./snap/f irefox 2391 .config ibus

You can exclude the directories and only sort the files using the below find command −

:~$ find . -type f -ls | sort -n -r

Using GUI Method

The files and directories are sorted alphabetically in the Linux file manager. In this file manager, you can set your preferences according to yourself. You can set the files according to their size by customizing your preferences.

For this, go to the file manager and the directory and folder where you want to sort the files according to their size. After this, click the downward arrow (1) in the top bar.

Figure 1

You can sort the files by decreasing size by clicking the size option (2) shown in the above image.

Conclusion

So this was all about the simple ways you can use to list each directories and sort them as per their size in Linux. Through the ls command in Linux, you can sort files. Using this command, we have seen sorting files according to their size using various options and sorting files in human-readable format, specific size units, excluding hidden files, etc. Apart from this, you can also sort files through the find and du command. We also listed the files according to size using these two commands.

Sorting files with the GUI method is easy; you can do it in a few clicks. You have to customize your preferences by going to the file manager. Thus In this article, we have seen how to sort files and directories by their size using almost all possible methods.

Updated on: 22-Aug-2023

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements