Redhat/CentOS/Generic Linux: How to sort files by their size ?

Today we will have a look how to sort files by their size in a directory on Linux Operating system.

The tip is very basic, however we should be aware of this as sometimes it is very useful to sort files by their size quickly.

1. Default File Listing: Alphabetically sorted

[root@ngelinux ~]# ls -l
total 16
-rw-------. 1 root root 1920 Mar 13 19:38 anaconda-ks.cfg
-rw-r--r--. 1 root root  411 Mar 14 12:26 id_rsa.pub
-rw-r--r--. 1 root root 1968 Mar 13 20:56 initial-setup-ks.cfg
drwxr-xr-x. 3 root root   53 Apr  2 16:12 testdir
-rw-r--r--. 1 root root    8 Apr  2 20:32 testfile
[root@ngelinux ~]# 

 

2. Sort Files by Size

[root@ngelinux ~]# ls -lS
total 16
-rw-r--r--. 1 root root 1968 Mar 13 20:56 initial-setup-ks.cfg
-rw-------. 1 root root 1920 Mar 13 19:38 anaconda-ks.cfg
-rw-r--r--. 1 root root  411 Mar 14 12:26 id_rsa.pub
drwxr-xr-x. 3 root root   53 Apr  2 16:12 testdir
-rw-r--r--. 1 root root    8 Apr  2 20:32 testfile
[root@ngelinux ~]# 

 

3. Sort by Filesize in Reverse order.

[root@ngelinux ~]# ls -lSr
total 16
-rw-r--r--. 1 root root    8 Apr  2 20:32 testfile
drwxr-xr-x. 3 root root   53 Apr  2 16:12 testdir
-rw-r--r--. 1 root root  411 Mar 14 12:26 id_rsa.pub
-rw-------. 1 root root 1920 Mar 13 19:38 anaconda-ks.cfg
-rw-r--r--. 1 root root 1968 Mar 13 20:56 initial-setup-ks.cfg
[root@ngelinux ~]# 

 

4. Making the list in Human Readable format.

[root@ngelinux ~]# ls -lSrh
total 16K
-rw-r--r--. 1 root root    8 Apr  2 20:32 testfile
drwxr-xr-x. 3 root root   53 Apr  2 16:12 testdir
-rw-r--r--. 1 root root  411 Mar 14 12:26 id_rsa.pub
-rw-------. 1 root root 1.9K Mar 13 19:38 anaconda-ks.cfg
-rw-r--r--. 1 root root 2.0K Mar 13 20:56 initial-setup-ks.cfg
[root@ngelinux ~]# 

In short, this tip shows the usage of “-S”(sort by size), -r(in reverse order) options with ls command.
 

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments