Listing and Sorting Files

Listing files by access date.

You can use the “-ltur” option. The “u” enforces the “by access date” listing order.  The “t” option to list files in order of age. The “r” option is to reverse the option

$ ls -ltur
-rwxr-xr-x  1 user1 users 1622374400 Mar 10 00:08 yyyy.iso
-rw-rw-r--  1 user1 users   18387452 Mar 13 14:06 xxxx.rpm
-rwxr-xr-x  1 user1 users        303 Mar 30 16:32 visJob.pbs
-rw-------  1 user1 users        762 Mar 30 16:35 visJob.o1475403
-rw-------  1 user1 users         59 Mar 30 16:35 visJob.e1475403

Listing Files by Modified Date and Time

You can use the “-ltr” option. The “r” is the reverse order.

$ ls -ltr
-rw-rw-r--  1 melvin melvin   18387452 Mar 13 14:03 xxxx.rpm
-rwxr-xr-x  1 melvin melvin        303 Mar 13 17:00 vis.pbs
-rw-rw-r--  1 melvin melvin       1084 Mar 23 16:52 yyy.pem

Listing files by owner

Use the output of the ls command to sort and pick out the owner column by adding “-k3” to sort on the third field.

$ ls -l | sort -k3 | more
drwx------  29 www           users                 1005 Sep 20  2022 www-home
drwx------  16 yyy           users                  684 Apr  3 14:07 yyy-home
drwx------  16 zzz           users                  746 Mar 21 11:31 zzz-home

Listing files by group

Sort files by the associated groups, you can pass the output from a long listing to the sort command and tell it to sort on column 4.

$ ls -l | sort -k4 | more
drwx------  29 www           users1                 1005 Sep 20  2022 www-home
drwx------  16 yyy           users2                  684 Apr  3 14:07 yyy-home
drwx------  16 zzz           users3                  746 Mar 21 11:31 zzz-home

Listing files by size

To sort files by Size, use the “-S” Option. If you wish to put the largest files at the end, use the “-r”. Use the “-h” to be human readable.

ls -lSrh
-rw-rw-r--  1 www users 1.9G Nov 15 11:04 integr8_120.xml.gz
-rw-rw-r--  1 www users 6.0G Jul 26  2022 20220712_msconvert.zip
-rw-rw-r--  1 www users 6.9G May 31  2022 ELECTRONICS.tgz

Sorting ls Output Based on Time and Date

You may want to list the files in a directory based on last modified time and date from the latest to the oldest, you will need to use -t

% ls -lt
drwxr-xr-x  26 root root    4096 Mar 29 13:40 mono
drwxr-xr-x   2 root root      42 Mar 29 13:40 mono-source-libs
lrwxrwxrwx   1 root root      22 Dec 21 12:55 libnss_centrifyda.so.0 -> libnss_centrifyda.so.2
drwxr-xr-x.  2 root root      62 Dec 21 12:54 security
lrwxrwxrwx   1 root root      22 Dec 21 12:54 libnss_centrifydc.so.0 -> libnss_centrifydc.so.2
lrwxrwxrwx   1 root root      28 Dec 21 12:54 libnss_centrify_logonly.so.0 -> libnss_centrify_logonly.so.2
-rwxr-xr-x   1 root root  238560 Nov 17 09:13 libnss_centrifydc.so.2
-rwxr-xr-x   1 root root  140064 Nov 17 09:13 libnss_centrify_logonly.so.2
-rwxr-xr-x   1 root root  159112 Nov  9 02:39 libnss_centrifyda.so.2
drwxr-xr-x   4 root root      40 Jul 25  2021 fastx
drwxr-xr-x.  2 root root     100 Dec 11  2020 modprobe.d
drwxr-xr-x   8 root root     180 Oct 26  2020 rstudio-server
.....
.....

If you want a sorting from the oldest to the latest, you use the -r option

% ls -ltr
.....
.....
-rwxr-xr-x   1 root root  159112 Nov  9 02:39 libnss_centrifyda.so.2
-rwxr-xr-x   1 root root  140064 Nov 17 09:13 libnss_centrify_logonly.so.2
-rwxr-xr-x   1 root root  238560 Nov 17 09:13 libnss_centrifydc.so.2
lrwxrwxrwx   1 root root      28 Dec 21 12:54 libnss_centrify_logonly.so.0 -> libnss_centrify_logonly.so.2
lrwxrwxrwx   1 root root      22 Dec 21 12:54 libnss_centrifydc.so.0 -> libnss_centrifydc.so.2
drwxr-xr-x.  2 root root      62 Dec 21 12:54 security
lrwxrwxrwx   1 root root      22 Dec 21 12:55 libnss_centrifyda.so.0 -> libnss_centrifyda.so.2
drwxr-xr-x   2 root root      42 Mar 29 13:40 mono-source-libs
drwxr-xr-x  26 root root    4096 Mar 29 13:40 mono