How to list directories first with sorted output in Linux bash shell ?

We all know ls command to list contents of a particular directory.

In this quick tip, we will look how to list directories first when listing directory contents.

1. Default “ls -l” i.e long list command output

[root@nglinux ~]# ls -l
total 886212
-rw-r--r--. 1 root root      4096 Jan 15 07:56 6511aba475c4212fd99e06f7a8baec3a.png
-rw-------. 1 root root      3366 Nov 14 08:23 anaconda-ks.cfg
-rw-r--r--. 1 root root         0 Jan 16 06:24 bigfile2.txt
-rw-r--r--. 1 root root         2 Jan 16 06:24 bigfile.txt
-r--r--r--. 1 root root        14 Mar 28  2017 Centos_BuildTag
drwxr-xr-x. 2 root root      4096 Dec 14 22:36 Desktop
drwxr-xr-x. 2 root root      4096 Jan 15 03:34 dir1
drwxr-xr-x. 2 root root      4096 Nov 14 08:57 Documents
### Output Truncated ###

2. Long list “ls -l” Output with directory listing first.
In the below output, we can see all directories are listed first and then files are listed.

[root@nglinux ~]# ls -l --group-directories-first
total 886212
drwxr-xr-x. 2 root root      4096 Dec 14 22:36 Desktop
drwxr-xr-x. 2 root root      4096 Jan 15 03:34 dir1
drwxr-xr-x. 2 root root      4096 Nov 14 08:57 Documents
drwxr-xr-x. 2 root root      4096 Nov 14 08:57 Downloads
drwxr-xr-x. 2 root root      4096 Jan 22 03:37 hello
drwxr-xr-x. 2 root root      4096 Nov 14 08:57 Templates
drwxr-xr-x. 2 root root      4096 Nov 14 08:57 Videos
-rw-r--r--. 1 root root      4096 Jan 15 07:56 6511aba475c4212fd99e06f7a8baec3a.png
-rw-------. 1 root root      3366 Nov 14 08:23 anaconda-ks.cfg
-rw-r--r--. 1 root root         0 Jan 16 06:24 bigfile2.txt
### Output truncated ###

3. Directory listing first with sorting based on time/size.

### First lets see how to sort files according to size
[root@nglinux ~]# ls -l --group-directories-first --sort size | head 
total 886212
drwxr-xr-x. 2 root root  25985024 Jan  4 03:07 testing
drwxr-xr-x. 2 root root      4096 Dec 14 22:36 Desktop
drwxr-xr-x. 2 root root      4096 Jan 15 03:34 dir1
drwxr-xr-x. 2 root root      4096 Nov 14 08:57 Documents
drwxr-xr-x. 2 root root      4096 Nov 14 08:57 Downloads
drwxr-xr-x. 2 root root      4096 Jan 22 03:37 hello
drwxr-xr-x. 2 root root      4096 Nov 14 08:57 Music
drwxr-xr-x. 2 root root      4096 Nov 14 08:57 Pictures
drwxr-xr-x. 2 root root      4096 Nov 14 08:57 Public

### Now lets sort it according to time.
[root@nglinux ~]# ls -l --group-directories-first --sort time | head 
total 886212
drwxr-xr-x. 2 root root      4096 Jan 22 03:37 hello
drwxr-xr-x. 2 root root      4096 Jan 20 02:54 testdir
drwxr-xr-x. 2 root root      4096 Jan 15 03:34 dir1
drwxr-xr-x. 2 root root  25985024 Jan  4 03:07 testing
drwxr-xr-x. 3 root root      4096 Dec 29 03:01 test
drwxr-xr-x. 2 root root      4096 Dec 14 22:36 Desktop
drwxr-xr-x. 2 root root      4096 Nov 14 08:57 Documents
drwxr-xr-x. 2 root root      4096 Nov 14 08:57 Downloads
drwxr-xr-x. 2 root root      4096 Nov 14 08:57 Music
[root@nglinux ~]# 

Using above trick, we can sort directories and files and that too according to size or time.

“–group-directories-first” is a new feature introduced in new coreutils package available from RHEL/CentOS6 onwards.
0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments