Usually, we are in habit of saving a lot of information in form of files on our system. Some, hidden files, some kept in a separate folder created for our ease of understanding, while some as it is. But, this whole stuff fills our directories; usually desktop, making it look like a mess. But, the problem arises when we need to search for a particular file modified on particular date and time in this huge collection.

 

People comfortable with GUI’s can find it using File Manager, which lists files in long listing format, making it easy to figure out what we wanted, but those users having habit of black screens, or even anyone working on servers which are devoid of GUI’s would want a simple command or set of commands that could ease out their search.

 

Real beauty of Linux shows here, as Linux has a collection of commands which if used separately or together can help to search for a file, or sort a collection of files according to their name, date of modification, time of creation, or even any filter you could think of applying to get your result.

 

Here, we will unveil the real strength of Linux by examining a set of commands which can help sorting a file or even a list of files by Date and Time.

 

Linux Utilities to Sort Files in Linux

Some basic Linux command-line utilities that are just sufficient for sorting a directory based on Date and Time are:

  • ls command

ls – Listing contents of the directory, this utility can list the files and directories and can even list all the status information about them including date and time of modification or access, permissions, size, owner, group etc.

  • sort command

sort – This command can be used to sort the output of any search just by any field or any particular column of the field.

 

Some Ways to Sort Files using Date and Time

 

Below are the list of commands to sort based on Date and Time.

 

1. List Files Based on Modification Time

 

The below command lists files in long listing format, and sorts files based on modification time, newest first. To sort in reverse order, use '-r' switch with this command.

# ls -lt

total 673768
-rwxr----- 1 rootadminz rootadminz  3312130 Jan 19 15:24 When You Are Gone.MP3
-rwxr----- 1 rootadminz rootadminz  4177212 Jan 19 15:24 When I Dream At Night - Marc Anthony-1.mp3
-rwxr----- 1 rootadminz rootadminz  4177212 Jan 19 15:24 When I Dream At Night - Marc Anthony.mp3
-rwxr----- 1 rootadminz rootadminz  6629090 Jan 19 15:24 Westlife_Tonight.MP3
-rwxr----- 1 rootadminz rootadminz  3448832 Jan 19 15:24 We Are The World by USA For Africa (Michael Jackson).mp3
-rwxr----- 1 rootadminz rootadminz  8580934 Jan 19 15:24 This Love.mp3
-rwxr----- 1 rootadminz rootadminz  2194832 Jan 19 15:24 The Cross Of Changes.mp3
-rwxr----- 1 rootadminz rootadminz  5087527 Jan 19 15:24 T.N.T. For The Brain 5.18.mp3
-rwxr----- 1 rootadminz rootadminz  3437100 Jan 19 15:24 Summer Of '69.MP3
-rwxr----- 1 rootadminz rootadminz  4360278 Jan 19 15:24 Smell Of Desire.4.32.mp3
-rwxr----- 1 rootadminz rootadminz  4582632 Jan 19 15:24 Silence Must Be Heard 4.46.mp3
-rwxr----- 1 rootadminz rootadminz  4147119 Jan 19 15:24 Shadows In Silence 4.19.mp3
-rwxr----- 1 rootadminz rootadminz  4189654 Jan 19 15:24 Sarah Brightman  & Enigma - Eden (remix).mp3
-rwxr----- 1 rootadminz rootadminz  4124421 Jan 19 15:24 Sade - Smooth Operator.mp3
-rwxr----- 1 rootadminz rootadminz  4771840 Jan 19 15:24 Sade - And I Miss You.mp3
-rwxr----- 1 rootadminz rootadminz  3749477 Jan 19 15:24 Run To You.MP3
-rwxr----- 1 rootadminz rootadminz  7573679 Jan 19 15:24 Roger Sanchez_Another Chance_Full_Mix.mp3
-rwxr----- 1 rootadminz rootadminz  3018211 Jan 19 15:24 Principal Of Lust.3.08.mp3
-rwxr----- 1 rootadminz rootadminz  5688390 Jan 19 15:24 Please Forgive Me.MP3
-rwxr----- 1 rootadminz rootadminz  3381827 Jan 19 15:24 Obvious.mp3
-rwxr----- 1 rootadminz rootadminz  5499073 Jan 19 15:24 Namstey-London-Viraaniya.mp3
-rwxr----- 1 rootadminz rootadminz  3129210 Jan 19 15:24 MOS-Enya - Only Time (Pop Radio mix).m

 

2. List Files Based on Last Access Time

 

Listing of files in directory based on last access time, i.e. based on time the file was last accessed, not modified.

# ls -ltu

total 3084272
drwxr-xr-x  2 rootadminz rootadminz       4096 Jan 19 15:24 Music
drwxr-xr-x  2 rootadminz rootadminz       4096 Jan 19 15:22 Linux-ISO
drwxr-xr-x  2 rootadminz rootadminz       4096 Jan 19 15:22 Music-Player
drwx------  3 rootadminz rootadminz       4096 Jan 19 15:22 tor-browser_en-US
drwxr-xr-x  2 rootadminz rootadminz       4096 Jan 19 15:22 bin
drwxr-xr-x 11 rootadminz rootadminz       4096 Jan 19 15:22 Android Games
drwxr-xr-x  2 rootadminz rootadminz       4096 Jan 19 15:22 Songs
drwxr-xr-x  2 rootadminz rootadminz       4096 Jan 19 15:22 renamefiles
drwxr-xr-x  2 rootadminz rootadminz       4096 Jan 19 15:22 katoolin-master
drwxr-xr-x  2 rootadminz rootadminz       4096 Jan 19 15:22 Tricks
drwxr-xr-x  3 rootadminz rootadminz       4096 Jan 19 15:22 Linux-Tricks
drwxr-xr-x  6 rootadminz rootadminz       4096 Jan 19 15:22 tuptime
drwxr-xr-x  4 rootadminz rootadminz       4096 Jan 19 15:22 xdm
drwxr-xr-x  2 rootadminz rootadminz      20480 Jan 19 15:22 ffmpeg usage
drwxr-xr-x  2 rootadminz rootadminz       4096 Jan 19 15:22 xdm-helper

 

3. List Files Based on Last Modification Time

 

Listing of files in directory based on last modification time of file’s status information, or the 'ctime'. This command would list that file first whose any status information like: owner, group, permissions, size etc has been recently changed.

 # ls -ltc

total 3084272
drwxr-xr-x  2 rootadminz rootadminz       4096 Jan 19 15:24 Music
drwxr-xr-x  2 rootadminz rootadminz       4096 Jan 19 13:05 img
-rw-------  1 rootadminz rootadminz     262191 Jan 19 12:15 rootadminz.jpeg
drwxr-xr-x  5 rootadminz rootadminz       4096 Jan 19 10:57 Desktop
drwxr-xr-x  7 rootadminz rootadminz      12288 Jan 18 16:00 Downloads
drwxr-xr-x 13 rootadminz rootadminz       4096 Jan 18 15:36 VirtualBox VMs
-rwxr-xr-x  1 rootadminz rootadminz        691 Jan 13 14:57 special.sh
-rw-r--r--  1 rootadminz rootadminz     654325 Jan  4 16:55 powertop-2.7.tar.gz.save
-rw-r--r--  1 rootadminz rootadminz     654329 Jan  4 11:17 filename.tar.gz
drwxr-xr-x  3 rootadminz rootadminz       4096 Jan  4 11:04 powertop-2.7
-rw-r--r--  1 rootadminz rootadminz     447795 Dec 31 14:22 Happy-New-Year-2016.jpg
-rw-r--r--  1 rootadminz rootadminz         12 Dec 18 18:46 ravi
-rw-r--r--  1 rootadminz rootadminz       1823 Dec 16 12:45 setuid.txt
...

If '-a' switch is used with above commands, they can list and sort even the hidden files in current directory, and '-r' switch lists the output in reverse order.

 

For more in-depth sorting, like sorting on Output of find command, however ls can also be used, but there  'sort' proves more helpful as the output may not have only file name but any fields desired by user.

 

Below commands show usage of sort with find command to sort the list of files based on Date and Time.

 

4. Sorting Files based on Month

 

Here, we use find command to find all files in root (‘/’) directory and then print the result as: Month in which file was accessed and then filename. Of that complete result, here we list out top 11 entries.

# find / -type f -printf "\n%Ab %p" | head -n 11

Dec /usr/lib/nvidia/pre-install
Dec /usr/lib/libcpufreq.so.0.0.0
Apr /usr/lib/libchromeXvMCPro.so.1.0.0
Apr /usr/lib/libt1.so.5.1.2
Apr /usr/lib/libchromeXvMC.so.1.0.0
Apr /usr/lib/libcdr-0.0.so.0.0.15
Dec /usr/lib/msttcorefonts/update-ms-fonts
Nov /usr/lib/ldscripts/elf32_x86_64.xr
Nov /usr/lib/ldscripts/elf_i386.xbn
Nov /usr/lib/ldscripts/i386linux.xn

 

The below command sorts the output using the key as the first field, specified by '-k1' and then it sorts on Month as specified by 'M' ahead of it.

 

# find / -type f -printf "\n%Ab %p" | head -n 11 | sort -k1M

Apr /usr/lib/libcdr-0.0.so.0.0.15
Apr /usr/lib/libchromeXvMCPro.so.1.0.0
Apr /usr/lib/libchromeXvMC.so.1.0.0
Apr /usr/lib/libt1.so.5.1.2
Nov /usr/lib/ldscripts/elf32_x86_64.xr
Nov /usr/lib/ldscripts/elf_i386.xbn
Nov /usr/lib/ldscripts/i386linux.xn
Dec /usr/lib/libcpufreq.so.0.0.0
Dec /usr/lib/msttcorefonts/update-ms-fonts
Dec /usr/lib/nvidia/pre-install

 

5. Sort Files Based on Date

 

Here, again we use find command to find all the files in the root directory, but now we will print the result as the last date the file was accessed, last time the file was accessed and then the filename. Of that, we take out top 11 entries.

# find / -type f -printf "\n%AD %AT %p" | head -n 11

12/08/15 11:30:38.0000000000 /usr/lib/nvidia/pre-install
12/07/15 10:34:45.2694776230 /usr/lib/libcpufreq.so.0.0.0
04/11/15 06:08:34.9819910430 /usr/lib/libchromeXvMCPro.so.1.0.0
04/11/15 06:08:34.9939910430 /usr/lib/libt1.so.5.1.2
04/11/15 06:08:35.0099910420 /usr/lib/libchromeXvMC.so.1.0.0
04/11/15 06:08:35.0099910420 /usr/lib/libcdr-0.0.so.0.0.15
12/18/15 11:19:25.2656728990 /usr/lib/msttcorefonts/update-ms-fonts
11/12/15 12:56:34.0000000000 /usr/lib/ldscripts/elf32_x86_64.xr
11/12/15 12:56:34.0000000000 /usr/lib/ldscripts/elf_i386.xbn
11/12/15 12:56:34.0000000000 /usr/lib/ldscripts/i386linux.xn

 

 

The below sort command first sorts on the basis of the last digit of the year then sort on basis of the last digit of the month in reverse order and finally sorts on basis of the first field. Here, ‘1.8‘ means 8th column of the first field and ‘n’ ahead of it means numerical sort, while ‘r’ indicates reverse order sorting.

 

# find / -type f -printf "\n%AD %AT %p" | head -n 11 | sort -k1.8n -k1.1nr -k1

12/07/15 10:34:45.2694776230 /usr/lib/libcpufreq.so.0.0.0
12/08/15 11:30:38.0000000000 /usr/lib/nvidia/pre-install
12/18/15 11:19:25.2656728990 /usr/lib/msttcorefonts/update-ms-fonts
11/12/15 12:56:34.0000000000 /usr/lib/ldscripts/elf32_x86_64.xr
11/12/15 12:56:34.0000000000 /usr/lib/ldscripts/elf_i386.xbn
11/12/15 12:56:34.0000000000 /usr/lib/ldscripts/i386linux.xn
04/11/15 06:08:34.9819910430 /usr/lib/libchromeXvMCPro.so.1.0.0
04/11/15 06:08:34.9939910430 /usr/lib/libt1.so.5.1.2
04/11/15 06:08:35.0099910420 /usr/lib/libcdr-0.0.so.0.0.15
04/11/15 06:08:35.0099910420 /usr/lib/libchromeXvMC.so.1.0.0

 

6. Sorting Files Based on Time

 

Here, again we use find command to list out top 11 files in root directory and print the result in format: last time file was accessed and then filename.

 

# find / -type f -printf "\n%AT %p" | head -n 11

11:30:38.0000000000 /usr/lib/nvidia/pre-install
10:34:45.2694776230 /usr/lib/libcpufreq.so.0.0.0
06:08:34.9819910430 /usr/lib/libchromeXvMCPro.so.1.0.0
06:08:34.9939910430 /usr/lib/libt1.so.5.1.2
06:08:35.0099910420 /usr/lib/libchromeXvMC.so.1.0.0
06:08:35.0099910420 /usr/lib/libcdr-0.0.so.0.0.15
11:19:25.2656728990 /usr/lib/msttcorefonts/update-ms-fonts
12:56:34.0000000000 /usr/lib/ldscripts/elf32_x86_64.xr
12:56:34.0000000000 /usr/lib/ldscripts/elf_i386.xbn
12:56:34.0000000000 /usr/lib/ldscripts/i386linux.xn

 

The below command sorts the output based on first column of the first field of the output which is first digit of hour.

 

# find / -type f -printf "\n%AT %p" | head -n 11 | sort -k1.1n

06:08:34.9819910430 /usr/lib/libchromeXvMCPro.so.1.0.0
06:08:34.9939910430 /usr/lib/libt1.so.5.1.2
06:08:35.0099910420 /usr/lib/libcdr-0.0.so.0.0.15
06:08:35.0099910420 /usr/lib/libchromeXvMC.so.1.0.0
10:34:45.2694776230 /usr/lib/libcpufreq.so.0.0.0
11:19:25.2656728990 /usr/lib/msttcorefonts/update-ms-fonts
11:30:38.0000000000 /usr/lib/nvidia/pre-install
12:56:34.0000000000 /usr/lib/ldscripts/elf32_x86_64.xr
12:56:34.0000000000 /usr/lib/ldscripts/elf_i386.xbn
12:56:34.0000000000 /usr/lib/ldscripts/i386linux.xn

 

7. Sorting Output of ls -l based on Date

 

This command sorts the output of 'ls -l' command based on 6th field month-wise, then based on 7th field which is date, numerically.

 

# ls -l | sort -k6M -k7n

total 116
-rw-r--r-- 1 root root     0 Oct  1 19:51 backup.tgz
drwxr-xr-x 2 root root  4096 Oct  7 15:27 Desktop
-rw-r--r-- 1 root root 15853 Oct  7 15:19 powertop_report.csv
-rw-r--r-- 1 root root 79112 Oct  7 15:25 powertop.html
-rw-r--r-- 1 root root     0 Oct 16 15:26 file3
-rw-r--r-- 1 root root    13 Oct 16 15:17 B
-rw-r--r-- 1 root root    21 Oct 16 15:16 A
-rw-r--r-- 1 root root    64 Oct 16 15:38 C

 

Conclusion

Likewise, by having some knowledge of sort command, you can sort almost any listing based on any field and even its any column you desire. These were some of tricks to help you sort files based on Date or Time. You can have your own tricks build based on these. However, if you have any other interesting trick, you can always mention that in your comments.

Was this answer helpful? 0 Users Found This Useful (0 Votes)