OSETC TECH

How To List All Files Ordered by Size in Linux

This post will guide you how to list all files sorted by file size using Ls command at the shell prompt in your Linux system. How do I sort all files in a given directory by size using ls command in Linux operating systems.

Ls Command


The Ls Command can be used to list information about the FILEs (the current directory by default). Sort entries alphabetically if none of -cftuvSUX nor –sort is specified.

The Syntax of the ls command is as followss:

ls [OPTION]... [FILE]...

Option:

-S sort by file size

For example, you want to list all files under the / direcotry using ls command, just type the following command:

$ ls -l  /

Outputs:

[devops@localhost ~]$ ls -l /
total 28
lrwxrwxrwx. 1 root root 7 Jun 14 20:28 bin -> usr/bin
dr-xr-xr-x. 5 root root 4096 Jun 14 20:38 boot
drwxr-xr-x. 20 root root 3160 Sep 24 17:20 dev
drwxr-xr-x. 143 root root 8192 Sep 24 17:24 etc
drwxr-xr-x. 3 root root 20 Jun 14 22:09 home
lrwxrwxrwx. 1 root root 7 Jun 14 20:28 lib -> usr/lib
lrwxrwxrwx. 1 root root 9 Jun 14 20:28 lib64 -> usr/lib64
drwxr-xr-x. 2 root root 6 Apr 11 2018 media
drwxr-xr-x. 2 root root 6 Apr 11 2018 mnt
drwxr-xr-x. 3 root root 16 Jun 14 20:32 opt
dr-xr-xr-x. 212 root root 0 Sep 24 17:20 proc
dr-xr-x---. 8 root root 4096 Jul 3 18:59 root
drwxr-xr-x. 41 root root 1280 Sep 24 17:24 run
lrwxrwxrwx. 1 root root 8 Jun 14 20:28 sbin -> usr/sbin
drwxr-xr-x. 2 root root 6 Apr 11 2018 srv
dr-xr-xr-x. 13 root root 0 Sep 24 17:20 sys
drwxrwxrwt. 16 root root 4096 Sep 24 18:32 tmp
drwxr-xr-x. 13 root root 155 Jun 14 20:28 usr
drwxr-xr-x. 21 root root 4096 Jun 14 21:08 var

Sortting All Files by Size Using Ls Command


You can pass the “-S” option to the ls command to sort all files by size, type:

$ ls -lS /

Outputs:

[devops@localhost ~]$ ls -lS /
total 28
drwxr-xr-x. 143 root root 8192 Sep 24 17:24 etc
dr-xr-xr-x. 5 root root 4096 Jun 14 20:38 boot
dr-xr-x---. 8 root root 4096 Jul 3 18:59 root
drwxrwxrwt. 16 root root 4096 Sep 24 18:32 tmp
drwxr-xr-x. 21 root root 4096 Jun 14 21:08 var
drwxr-xr-x. 20 root root 3160 Sep 24 17:20 dev
drwxr-xr-x. 41 root root 1280 Sep 24 17:24 run
drwxr-xr-x. 13 root root 155 Jun 14 20:28 usr
drwxr-xr-x. 3 root root 20 Jun 14 22:09 home
drwxr-xr-x. 3 root root 16 Jun 14 20:32 opt
lrwxrwxrwx. 1 root root 9 Jun 14 20:28 lib64 -> usr/lib64
lrwxrwxrwx. 1 root root 8 Jun 14 20:28 sbin -> usr/sbin
lrwxrwxrwx. 1 root root 7 Jun 14 20:28 bin -> usr/bin
lrwxrwxrwx. 1 root root 7 Jun 14 20:28 lib -> usr/lib
drwxr-xr-x. 2 root root 6 Apr 11 2018 media
drwxr-xr-x. 2 root root 6 Apr 11 2018 mnt
drwxr-xr-x. 2 root root 6 Apr 11 2018 srv
dr-xr-xr-x. 212 root root 0 Sep 24 17:20 proc
dr-xr-xr-x. 13 root root 0 Sep 24 17:20 sys

If you want to filter out all directories when sortting files by size using ls command, and you need to combine with grep command, type:

$ ls -lS / | grep -v "^d"

Outputs:

[devops@localhost ~]$ ls -lS / | grep -v "^d"
total 28
lrwxrwxrwx. 1 root root 9 Jun 14 20:28 lib64 -> usr/lib64
lrwxrwxrwx. 1 root root 8 Jun 14 20:28 sbin -> usr/sbin
lrwxrwxrwx. 1 root root 7 Jun 14 20:28 bin -> usr/bin
lrwxrwxrwx. 1 root root 7 Jun 14 20:28 lib -> usr/lib

Conclusion


You should know that how to list or sort all files by size using ls command with -S option at the shell prompt under CentOS or RHEL or Ubuntu Linux Operating systems.