How to Show All Files and Sort Them by File Size in Linux Using ls Command
The ls
command is used to list directory contents under Linux and Unix like operating systems. It lists all files and folders in a directory at the command line, but by default ls returns a list in alphabetical order. With a simple command flag/option, you can have ls sort by file size instead, showing the largest items at the top of the ls command results. This trick applies to the ls command output in Mac OS / Mac OS X, Linux, BSD, as well as Bash in Windows.
Default Output
Without any option, ls will return file list in alphabetical order:
- Open the Terminal if you have not done so already and navigate to the directory you wish to sort by file size with ls.
- Issue the following command:
$ ls
$ ls -a
$ ls -al
-a
will not ignore entries starting with .
-l
will show file list in a long listing format
Output sample for above commands:
adjie@HIU /usr/share/nginx/html/laravel $ ls
app bootstrap composer.lock database phpunit.xml readme.md routes storage vendor
artisan composer.json config package.json public resources server.php tests webpack.mix.js
adjie@HIU /usr/share/nginx/html/laravel $ ls -a
. artisan composer.lock .env .gitignore public routes tests
.. bootstrap config .env.example package.json readme.md server.php vendor
app composer.json database .gitattributes phpunit.xml resources storage webpack.mix.js
adjie@HIU /usr/share/nginx/html/laravel $ ls -al
total 232
drwxrwxr-x 12 adjie adjie 4096 Jan 25 2018 .
drwxrwxr-x 31 www-data www-data 4096 Aug 24 00:23 ..
drwxrwxr-x 7 adjie adjie 4096 Jan 24 2018 app
-rwxr-xr-x 1 adjie adjie 1646 Jan 24 2017 artisan
drwxrwxr-x 3 adjie adjie 4096 Jan 24 2017 bootstrap
-rw-rw-r-- 1 adjie adjie 1379 Jan 24 2018 composer.json
-rw-rw-r-- 1 adjie adjie 139667 Jan 24 2018 composer.lock
drwxrwxr-x 2 adjie adjie 4096 Jan 24 2018 config
drwxrwxr-x 5 adjie adjie 4096 Jan 24 2017 database
-rw-rw-r-- 1 adjie adjie 646 Jan 24 2018 .env
-rw-rw-r-- 1 adjie adjie 499 Jan 24 2017 .env.example
-rw-rw-r-- 1 adjie adjie 61 Jan 24 2017 .gitattributes
-rw-rw-r-- 1 adjie adjie 95 Jan 24 2017 .gitignore
-rw-rw-r-- 1 adjie adjie 991 Jan 24 2017 package.json
-rw-rw-r-- 1 adjie adjie 1055 Jan 24 2017 phpunit.xml
drwxrwxr-x 6 adjie adjie 4096 Jan 24 2018 public
-rw-rw-r-- 1 adjie adjie 2906 Jan 24 2017 readme.md
drwxrwxr-x 5 adjie adjie 4096 Jan 24 2017 resources
drwxrwxr-x 2 adjie adjie 4096 Jan 24 2017 routes
-rw-rw-r-- 1 adjie adjie 563 Jan 24 2017 server.php
drwxrwxr-x 5 adjie adjie 4096 Jan 24 2017 storage
drwxrwxr-x 4 adjie adjie 4096 Jan 24 2017 tests
drwxrwxr-x 34 adjie adjie 4096 Jan 24 2018 vendor
-rw-rw-r-- 1 adjie adjie 555 Jan 24 2017 webpack.mix.js
Sort Output and Print Sizes in Human Readable Format
To sort the file list based on their size, add S
option on your ls command:
$ ls -alS
Alternatively, you can use this command:
$ ls -al | sort -k 5 -nr
The output should be similar for those commands:
adjie@HIU /usr/share/nginx/html/laravel $ ls -al | sort -k 5 -nr
-rw-rw-r-- 1 adjie adjie 139667 Jan 24 2018 composer.lock
drwxrwxr-x 7 adjie adjie 4096 Jan 24 2018 app
drwxrwxr-x 6 adjie adjie 4096 Jan 24 2018 public
drwxrwxr-x 5 adjie adjie 4096 Jan 24 2017 storage
drwxrwxr-x 5 adjie adjie 4096 Jan 24 2017 resources
drwxrwxr-x 5 adjie adjie 4096 Jan 24 2017 database
drwxrwxr-x 4 adjie adjie 4096 Jan 24 2017 tests
drwxrwxr-x 3 adjie adjie 4096 Jan 24 2017 bootstrap
drwxrwxr-x 34 adjie adjie 4096 Jan 24 2018 vendor
drwxrwxr-x 31 www-data www-data 4096 Aug 24 00:23 ..
drwxrwxr-x 2 adjie adjie 4096 Jan 24 2018 config
drwxrwxr-x 2 adjie adjie 4096 Jan 24 2017 routes
drwxrwxr-x 12 adjie adjie 4096 Jan 25 2018 .
-rw-rw-r-- 1 adjie adjie 2906 Jan 24 2017 readme.md
-rwxr-xr-x 1 adjie adjie 1646 Jan 24 2017 artisan
-rw-rw-r-- 1 adjie adjie 1379 Jan 24 2018 composer.json
-rw-rw-r-- 1 adjie adjie 1055 Jan 24 2017 phpunit.xml
-rw-rw-r-- 1 adjie adjie 991 Jan 24 2017 package.json
-rw-rw-r-- 1 adjie adjie 646 Jan 24 2018 .env
-rw-rw-r-- 1 adjie adjie 563 Jan 24 2017 server.php
-rw-rw-r-- 1 adjie adjie 555 Jan 24 2017 webpack.mix.js
-rw-rw-r-- 1 adjie adjie 499 Jan 24 2017 .env.example
-rw-rw-r-- 1 adjie adjie 95 Jan 24 2017 .gitignore
-rw-rw-r-- 1 adjie adjie 61 Jan 24 2017 .gitattributes
total 232
adjie@HIU /usr/share/nginx/html/laravel $ ls -alS
total 232
-rw-rw-r-- 1 adjie adjie 139667 Jan 24 2018 composer.lock
drwxrwxr-x 12 adjie adjie 4096 Jan 25 2018 .
drwxrwxr-x 31 www-data www-data 4096 Aug 24 00:23 ..
drwxrwxr-x 7 adjie adjie 4096 Jan 24 2018 app
drwxrwxr-x 3 adjie adjie 4096 Jan 24 2017 bootstrap
drwxrwxr-x 2 adjie adjie 4096 Jan 24 2018 config
drwxrwxr-x 5 adjie adjie 4096 Jan 24 2017 database
drwxrwxr-x 6 adjie adjie 4096 Jan 24 2018 public
drwxrwxr-x 5 adjie adjie 4096 Jan 24 2017 resources
drwxrwxr-x 2 adjie adjie 4096 Jan 24 2017 routes
drwxrwxr-x 5 adjie adjie 4096 Jan 24 2017 storage
drwxrwxr-x 4 adjie adjie 4096 Jan 24 2017 tests
drwxrwxr-x 34 adjie adjie 4096 Jan 24 2018 vendor
-rw-rw-r-- 1 adjie adjie 2906 Jan 24 2017 readme.md
-rwxr-xr-x 1 adjie adjie 1646 Jan 24 2017 artisan
-rw-rw-r-- 1 adjie adjie 1379 Jan 24 2018 composer.json
-rw-rw-r-- 1 adjie adjie 1055 Jan 24 2017 phpunit.xml
-rw-rw-r-- 1 adjie adjie 991 Jan 24 2017 package.json
-rw-rw-r-- 1 adjie adjie 646 Jan 24 2018 .env
-rw-rw-r-- 1 adjie adjie 563 Jan 24 2017 server.php
-rw-rw-r-- 1 adjie adjie 555 Jan 24 2017 webpack.mix.js
-rw-rw-r-- 1 adjie adjie 499 Jan 24 2017 .env.example
-rw-rw-r-- 1 adjie adjie 95 Jan 24 2017 .gitignore
-rw-rw-r-- 1 adjie adjie 61 Jan 24 2017 .gitattributes
Bonus: Output and Print File Size in Human Readable Format
It is normal for us as human being to have difficulties when it comes to reading long number. It's far more easier to identify file size in format like 137K other than mere number 139667. Fortunately, it's easy to be done using h
option.
$ ls -alSh
Output sample:
adjie@HIU /usr/share/nginx/html/laravel $ ls -alSh
total 232K
-rw-rw-r-- 1 adjie adjie 137K Jan 24 2018 composer.lock
drwxrwxr-x 12 adjie adjie 4,0K Jan 25 2018 .
drwxrwxr-x 31 www-data www-data 4,0K Aug 24 00:23 ..
drwxrwxr-x 7 adjie adjie 4,0K Jan 24 2018 app
drwxrwxr-x 3 adjie adjie 4,0K Jan 24 2017 bootstrap
drwxrwxr-x 2 adjie adjie 4,0K Jan 24 2018 config
drwxrwxr-x 5 adjie adjie 4,0K Jan 24 2017 database
drwxrwxr-x 6 adjie adjie 4,0K Jan 24 2018 public
drwxrwxr-x 5 adjie adjie 4,0K Jan 24 2017 resources
drwxrwxr-x 2 adjie adjie 4,0K Jan 24 2017 routes
drwxrwxr-x 5 adjie adjie 4,0K Jan 24 2017 storage
drwxrwxr-x 4 adjie adjie 4,0K Jan 24 2017 tests
drwxrwxr-x 34 adjie adjie 4,0K Jan 24 2018 vendor
-rw-rw-r-- 1 adjie adjie 2,9K Jan 24 2017 readme.md
-rwxr-xr-x 1 adjie adjie 1,7K Jan 24 2017 artisan
-rw-rw-r-- 1 adjie adjie 1,4K Jan 24 2018 composer.json
-rw-rw-r-- 1 adjie adjie 1,1K Jan 24 2017 phpunit.xml
-rw-rw-r-- 1 adjie adjie 991 Jan 24 2017 package.json
-rw-rw-r-- 1 adjie adjie 646 Jan 24 2018 .env
-rw-rw-r-- 1 adjie adjie 563 Jan 24 2017 server.php
-rw-rw-r-- 1 adjie adjie 555 Jan 24 2017 webpack.mix.js
-rw-rw-r-- 1 adjie adjie 499 Jan 24 2017 .env.example
-rw-rw-r-- 1 adjie adjie 95 Jan 24 2017 .gitignore
-rw-rw-r-- 1 adjie adjie 61 Jan 24 2017 .gitattributes
Final Words
I hope that you now know how to list all files and sort them by file size. If you run into any issues or have any feedback feel free to drop a comment below.