Linux ls Command Sort Files By Size

The ls command is used to list files in Linux distributions like Ubuntu, Mint, Debian, Kali, CentOS, etc. The ls command provides a lot of features where one of them is listing or sorting files according to their sizes. In this tutorial, we will learn different use cases to list files by their sizes.

Sort Files By Name (Alphabetically)

The ls command has a lot of options where the files and folders are listed by name or alphabetically by default. The -l option is used to list files and folders line by line.

ls -l
Sort Files By Name (Alphabetically)

Alternatively we can specify the file names or extensions to sort alphabetically. In the following example we will sort all text files or files with the *.txt extension alphebatically.

ls -l *.txt

The output is like below.

-rw-rw-r-- 1 ismail ismail  30 Kas 29 11:15 names.txt
-rw-rw-r-- 1 ismail ismail 607 Kas 27 20:21 sample.txt
-rw-rw-r-- 1 ismail ismail   0 Kas  3 10:20 text.txt

Sort Files By Size

Even files are sorted by named by default there are alternatives like sort files by size. In order to sort files by size the -S option should be provided. In the following example we will sort files in the current working directory .

ls -S -l
Sort Files By Size

Alternatively, we can use the long format of the -S option. The –sort=size is the long-form where the sorting attribute is set as size.

ls --sort=size -l

We can also specify the file types or extensions we want to sort according to the size. In the following example, we will sort text files or *.txt extension according to their sizes.

ls -S -l *.txt

The output is like below.

-rw-rw-r-- 1 ismail ismail 607 Kas 27 20:21 sample.txt
-rw-rw-r-- 1 ismail ismail  30 Kas 29 11:15 names.txt
-rw-rw-r-- 1 ismail ismail   0 Kas  3 10:20 text.txt

Filter Size Sorted File List

In some cases there may be hundereds of files which are sorted. This may fill the terminal and we can see the biggest files. The size sorted file list can be filtered with the head command. By default head command list first 10 lines or the biggest 10 files.

ls -S -l | head

Output is like below.

-rw-rw-r-- 1 ismail ismail 10503500 Eki 10 22:11 nmap-7.91.tar.bz2
-rwxrwxr-x 1 ismail ismail    16920 Oca  1 12:16 a.out
drwxrwxr-x 2 ismail ismail     4096 Ara 21 10:56 data1
drwxrwxr-x 2 ismail ismail     4096 Ara 21 10:56 data2
drwxrwxr-x 2 ismail ismail     4096 Ara 21 10:56 data3
drwxrwxr-x 2 ismail ismail     4096 Ara 21 10:56 data4
drwxrwxr-x 2 ismail ismail     4096 Ara 21 10:56 data5
drwxr-xr-x 2 ismail ismail     4096 Kas  2 14:38 Desktop
drwxr-xr-x 2 ismail ismail     4096 Kas  2 14:38 Documents

We can also specify the the limit number like below. In the following example we will limit the biggest 15 files.

ls -S -l | head -n 15
Filter Size Sorted Files List

Reverse Sort By Size

By default the -S size sorting option sort from the biggest file into the smallest file. But this can be reversed where first the smallest or lowest sized files are listed. The -r option is used to reverse sort by size.

ls -S -l -r
total 10372
 -rw-rw-r-- 1 ismail ismail        0 Kas  3 10:20 text.txt
 -rw-rw-r-- 1 ismail ismail        0 Ara 23 18:25 newfile
 -rw-rw-r-- 1 ismail ismail        0 Ara 19 16:45 myfile
 -rw-rw-r-- 1 ismail ismail        0 Ara 19 17:02 file4
 -rw-rw-r-- 1 ismail ismail        0 Ara 19 16:54 file3
 -rw-rw-r-- 1 ismail ismail        0 Ara 19 17:03 file2
 -rw-rw-r-- 1 ismail ismail        0 Ara 19 17:03 file1
 -rw-rw-r-- 1 ismail ismail       10 Ara 21 11:01 test
 lrwxrwxrwx 1 ismail ismail       10 Ara 26 17:05 mylink -> myfile.txt
 -rw-rw-r-- 1 ismail ismail       30 Kas 29 11:15 names.txt
 -rwxrw-r-- 1 ismail ismail       97 Oca  3 15:35 while.sh
 -rwxrw-r-- 1 ismail ismail      102 Kas 29 11:25 read_line_by_line.sh
 -rw-rw-r-- 1 ismail ismail      224 Kas 30 15:28 userinput.py
 -rw-rw-r-- 1 ismail ismail      308 Oca  1 12:16 random.c
 -rw-rw-r-- 1 ismail ismail      607 Kas 27 20:21 sample.txt
READ  How To Create File In Linux?

Leave a Comment