sort files by size in linux

How to Sort Files by Size in Linux

Often you may need to sort files by size in Linux. System administrators commonly use it to find out the largest files on their system and cleanup big files to free up disk space. There are many ways to sort files in Linux. We will simply use ls command along with different options to sort files in different ways.


How to Sort Files by Size in Linux

Here is how to sort files by size using ls command. Typically, we use ls command with -la option to display long listing and including that of hidden files.

$ ls -la
drwxr-xr-x 10 ubuntu ubuntu  4096 Sep  6 03:15 .
drwxr-xr-x  3 root   root    4096 Jan 14  2020 ..
-rw-------  1 ubuntu ubuntu 41622 Sep  6 03:15 .bash_history
-rw-r--r--  1 ubuntu ubuntu   220 Apr  4  2018 .bash_logout
-rw-r--r--  1 ubuntu ubuntu  3771 Apr  4  2018 .bashrc
drwx------  4 ubuntu ubuntu  4096 Feb  7  2020 .cache
drwxrwxr-x  3 ubuntu ubuntu  4096 Aug 12  2020 .dropbox-dist
-rw-rw-r--  1 ubuntu ubuntu    74 Jan 14  2020 .gitconfig
drwx------  3 ubuntu ubuntu  4096 Jan 14  2020 .gnupg
-rw-------  1 ubuntu ubuntu    28 Aug 12 06:35 .lesshst
drwx------  4 ubuntu ubuntu  4096 Jan 14  2020 .local
-rw-------  1 root   root   10607 Aug 28 09:23 .mysql_history
-rw-r--r--  1 ubuntu ubuntu   807 Apr  4  2018 .profile
-rw-------  1 ubuntu ubuntu    59 Jan 14  2020 .python_history
drwxrwxr-x  3 ubuntu ubuntu  4096 Feb  7  2020 .qws
-rw-rw-r--  1 ubuntu ubuntu    75 Feb  7  2020 .selected_editor
drwx------  2 ubuntu ubuntu  4096 Jan 14  2020 .ssh
-rw-r--r--  1 ubuntu ubuntu     0 Jan 14  2020 .sudo_as_admin_successful
drwxr-xr-x  2 root   root    4096 Apr 26 03:46 .vim
-rw-------  1 root   root   28722 Sep  6 03:15 .viminfo
-rw-rw-r--  1 ubuntu ubuntu   217 Sep  8  2020 .wget-hsts

-rwxr-xr-x  1 ubuntu ubuntu   278 Sep 11  2020 gpush.sh
-rw-rw-r--  1 ubuntu ubuntu 21736 Feb  8  2020 logo-transparent.png
-rwxr-xr-x  1 ubuntu ubuntu  1323 Sep 14  2020 mysql_backup.sh
-rw-rw-r--  1 ubuntu ubuntu    40 Aug  9 07:11 new_data.txt
-rwxr-xr-x  1 ubuntu ubuntu    64 Jul  2  2015 postit.sh
-rwxr-xr-x  1 root   root     262 Mar 14 04:02 remove-old-snaps
-rw-rw-r--  1 ubuntu ubuntu   110 Sep  8  2020 t
-rw-rw-r--  1 ubuntu ubuntu   476 Jul  4  2015 tweet.py
-rw-rw-r--  1 ubuntu ubuntu 39116 Feb 29  2016 tweet_list

Now if you want to sort the output by descending order of file size, that is, from biggest to smallest, you need to use -S option.

$ ls -laS

-rw-------  1 ubuntu ubuntu 41622 Sep  6 03:15 .bash_history
-rw-rw-r--  1 ubuntu ubuntu 39116 Feb 29  2016 tweet_list
-rw-------  1 root   root   28722 Sep  6 03:15 .viminfo
-rw-rw-r--  1 ubuntu ubuntu 26834 Aug 28 10:02 new-logo.png
-rw-rw-r--  1 ubuntu ubuntu 21736 Feb  8  2020 logo-transparent.png
-rw-------  1 root   root   10607 Aug 28 09:23 .mysql_history
drwxr-xr-x 10 ubuntu ubuntu  4096 Sep  6 03:15 .
drwxr-xr-x  3 root   root    4096 Jan 14  2020 ..
drwx------  4 ubuntu ubuntu  4096 Feb  7  2020 .cache

drwx------  3 ubuntu ubuntu  4096 Jan 14  2020 .gnupg
drwx------  4 ubuntu ubuntu  4096 Jan 14  2020 .local
drwxrwxr-x  3 ubuntu ubuntu  4096 Feb  7  2020 .qws
drwx------  2 ubuntu ubuntu  4096 Jan 14  2020 .ssh
drwxr-xr-x  2 root   root    4096 Apr 26 03:46 .vim
-rw-r--r--  1 ubuntu ubuntu  3771 Apr  4  2018 .bashrc

-rw-r--r--  1 ubuntu ubuntu   807 Apr  4  2018 .profile
-rw-rw-r--  1 ubuntu ubuntu   476 Jul  4  2015 tweet.py
-rwxr-xr-x  1 ubuntu ubuntu   278 Sep 11  2020 gpush.sh
-rwxr-xr-x  1 root   root     262 Mar 14 04:02 remove-old-snaps
-rw-r--r--  1 ubuntu ubuntu   220 Apr  4  2018 .bash_logout
-rw-rw-r--  1 ubuntu ubuntu   217 Sep  8  2020 .wget-hsts
-rw-rw-r--  1 ubuntu ubuntu   110 Sep  8  2020 t
-rw-rw-r--  1 ubuntu ubuntu    75 Feb  7  2020 .selected_editor
-rw-rw-r--  1 ubuntu ubuntu    74 Jan 14  2020 .gitconfig
-rwxr-xr-x  1 ubuntu ubuntu    64 Jul  2  2015 postit.sh
-rw-------  1 ubuntu ubuntu    59 Jan 14  2020 .python_history
-rw-rw-r--  1 ubuntu ubuntu    40 Aug  9 07:11 new_data.txt
-rw-------  1 ubuntu ubuntu    28 Aug 12 06:35 .lesshst
-rw-r--r--  1 ubuntu ubuntu     0 Jan 14  2020 .sudo_as_admin_successful

As you can see, the biggest files are listed at the top and the smallest ones are at the bottom. However, the file sizes are mentioned in bytes, which can be confusing. So you can use -h option to display file sizes in human readable form.

$ ls -laSh

-rw-------  1 ubuntu ubuntu  41K Sep  6 03:15 .bash_history
-rw-rw-r--  1 ubuntu ubuntu  39K Feb 29  2016 tweet_list
-rw-------  1 root   root    29K Sep  6 03:15 .viminfo
-rw-rw-r--  1 ubuntu ubuntu  27K Aug 28 10:02 new-logo.png
-rw-rw-r--  1 ubuntu ubuntu  22K Feb  8  2020 logo-transparent.png
-rw-------  1 root   root    11K Aug 28 09:23 .mysql_history
drwxr-xr-x 10 ubuntu ubuntu 4.0K Sep  6 03:15 .
drwxr-xr-x  3 root   root   4.0K Jan 14  2020 ..
drwx------  4 ubuntu ubuntu 4.0K Feb  7  2020 .cache

drwx------  3 ubuntu ubuntu 4.0K Jan 14  2020 .gnupg
drwx------  4 ubuntu ubuntu 4.0K Jan 14  2020 .local
drwxrwxr-x  3 ubuntu ubuntu 4.0K Feb  7  2020 .qws
drwx------  2 ubuntu ubuntu 4.0K Jan 14  2020 .ssh
drwxr-xr-x  2 root   root   4.0K Apr 26 03:46 .vim

-rw-r--r--  1 ubuntu ubuntu 3.7K Apr  4  2018 .bashrc

-rw-r--r--  1 ubuntu ubuntu  807 Apr  4  2018 .profile
-rw-rw-r--  1 ubuntu ubuntu  476 Jul  4  2015 tweet.py
-rwxr-xr-x  1 ubuntu ubuntu  278 Sep 11  2020 gpush.sh
-rwxr-xr-x  1 root   root    262 Mar 14 04:02 remove-old-snaps
-rw-r--r--  1 ubuntu ubuntu  220 Apr  4  2018 .bash_logout
-rw-rw-r--  1 ubuntu ubuntu  217 Sep  8  2020 .wget-hsts
-rw-rw-r--  1 ubuntu ubuntu  110 Sep  8  2020 t
-rw-rw-r--  1 ubuntu ubuntu   75 Feb  7  2020 .selected_editor
-rw-rw-r--  1 ubuntu ubuntu   74 Jan 14  2020 .gitconfig
-rwxr-xr-x  1 ubuntu ubuntu   64 Jul  2  2015 postit.sh
-rw-------  1 ubuntu ubuntu   59 Jan 14  2020 .python_history
-rw-rw-r--  1 ubuntu ubuntu   40 Aug  9 07:11 new_data.txt
-rw-------  1 ubuntu ubuntu   28 Aug 12 06:35 .lesshst
-rw-r--r--  1 ubuntu ubuntu    0 Jan 14  2020 .sudo_as_admin_successful

As you can see, the file sizes are listed Kb, Mb to be human readable.

If you want to sort files in ascending order (smallest to largest) use -r option.

$ ls -laShr

-rw-r--r--  1 ubuntu ubuntu    0 Jan 14  2020 .sudo_as_admin_successful
-rw-------  1 ubuntu ubuntu   28 Aug 12 06:35 .lesshst
-rw-rw-r--  1 ubuntu ubuntu   40 Aug  9 07:11 new_data.txt
-rw-------  1 ubuntu ubuntu   59 Jan 14  2020 .python_history
-rwxr-xr-x  1 ubuntu ubuntu   64 Jul  2  2015 postit.sh
-rw-rw-r--  1 ubuntu ubuntu   74 Jan 14  2020 .gitconfig
-rw-rw-r--  1 ubuntu ubuntu   75 Feb  7  2020 .selected_editor
-rw-rw-r--  1 ubuntu ubuntu  110 Sep  8  2020 t
-rw-rw-r--  1 ubuntu ubuntu  217 Sep  8  2020 .wget-hsts
-rw-r--r--  1 ubuntu ubuntu  220 Apr  4  2018 .bash_logout
-rwxr-xr-x  1 root   root    262 Mar 14 04:02 remove-old-snaps
-rwxr-xr-x  1 ubuntu ubuntu  278 Sep 11  2020 gpush.sh
-rw-rw-r--  1 ubuntu ubuntu  476 Jul  4  2015 tweet.py
-rw-r--r--  1 ubuntu ubuntu  807 Apr  4  2018 .profile

-rw-r--r--  1 ubuntu ubuntu 3.7K Apr  4  2018 .bashrc

drwxr-xr-x  2 root   root   4.0K Apr 26 03:46 .vim
drwx------  2 ubuntu ubuntu 4.0K Jan 14  2020 .ssh
drwxrwxr-x  3 ubuntu ubuntu 4.0K Feb  7  2020 .qws
drwx------  4 ubuntu ubuntu 4.0K Jan 14  2020 .local
drwx------  3 ubuntu ubuntu 4.0K Jan 14  2020 .gnupg
drwxrwxr-x  3 ubuntu ubuntu 4.0K Aug 12  2020 .dropbox-dist
drwx------  4 ubuntu ubuntu 4.0K Feb  7  2020 .cache
drwxr-xr-x  3 root   root   4.0K Jan 14  2020 ..
drwxr-xr-x 10 ubuntu ubuntu 4.0K Sep  6 03:15 .
-rw-------  1 root   root    11K Aug 28 09:23 .mysql_history
-rw-rw-r--  1 ubuntu ubuntu  22K Feb  8  2020 logo-transparent.png
-rw-rw-r--  1 ubuntu ubuntu  27K Aug 28 10:02 new-logo.png
-rw-------  1 root   root    29K Sep  6 03:15 .viminfo
-rw-rw-r--  1 ubuntu ubuntu  39K Feb 29  2016 tweet_list
-rw-------  1 ubuntu ubuntu  41K Sep  6 03:15 .bash_history

If you want to also list files of your subdirectories, recursively, use -R (capital R) option.

Please note, if you only use ls command with options then it will display results for your present working directory. If you want to sort files in another folder /home/ubuntu, you need to mention it after the ls options, as shown below.

$ ls -laShr /home/ubuntu

You can also use du command to list the largest files and folders in Linux. Here is an example to list top 10 files & directories in /home folder.

$ du -a /home | sort -n -r | head -n 10
256452  /home
256448  /home/ubuntu
197312  /home/ubuntu/.dropbox-dist
197300  /home/ubuntu/.dropbox-dist/dropbox-lnx.x86_64-104.4.175
49176   /home/ubuntu/.local
47676   /home/ubuntu/.local/lib
47672   /home/ubuntu/.local/lib/python2.7
47668   /home/ubuntu/.local/lib/python2.7/site-packages
47120   /home/ubuntu/.local/lib/python2.7/site-packages/django
37652   /home/ubuntu/.dropbox-dist/dropbox-lnx.x86_64-104.4.175/python-packages.zip

In the above command, du returns a list of all files & folders with their sizes. This output is passed to sort command, that sorts it in descending order. This output is passed to head command that filters and displays only the top 10 commands.

In this short article, we have looked at how to sort files in descending order.

Also read:

How to Combine Multiple PDF Files in Single File
Python Script to Run SQL Query
Su vs Sudo in Linux
How to Parse Command Line Arguments in Bash
How to Install Varnish on CentOS 7 with NGINX

Leave a Reply

Your email address will not be published. Required fields are marked *