How to find and sort files based on modification date and time in linux

Information TechnologyLinux

While working with computers, we have a habit of saving a lot of information in our computers such as files, folders, etc. Normally desktop looks like a mess but the problem arises when users want to search a modified file on a particular date or time. There are simple commands which are available in Linux to search a modified file. This article describes “How to Find and Sort Files Based on Modification Date and Time in Linux”.

List Files Based on Modification Time

The list command shows a list of files, directories, information about date and time of modification or access, permissions, size, owner, group etc. The below command shows the list of files along with format, sorts files based on modification time and newest file first.

$ ls -lt

The sample output should be like this –

total 322428
drwxr-xr-x 3 linux linux    4096 Mar 8 13:59 Downloads
drwxr-xr-x 6 linux linux    4096 Mar 3 14:34 Desktop
lrwxrwxrwx 1 linux linux    37 Feb 27 13:25 PlayOnLinux's virtual drives -> /home/linux/.PlayOnLinux//wineprefix/
-rw-r--r-- 1 root root    70706 Feb 23 14:52 Selection_007.png
-rw-r--r-- 1 root root    108159 Feb 23 14:49 root@linux: ~_005.png
-rw-r--r-- 1 root root    145629 Feb 23 14:47 Workspace 1_004.png
drwxr-xr-x 2 linux linux    4096 Feb 23 14:30 Pictures
-rw-rw-r-- 1 linux linux    87631 Feb 19 14:08 account.png
-rw-rw-r-- 1 linux linux    72172 Feb 19 14:07 network.png
-rw-rw-r-- 1 linux linux    98362 Feb 19 14:05 sample1.png
drwxr-xr-x 8 root root       4096 Feb 19 11:38 linux-dash
drwxr-xr-x 2 linux linux    4096 Feb 19 11:08 Documents
drwxr-xr-x 2 linux linux    4096 Feb 19 11:08 Music
drwxr-xr-x 2 linux linux    4096 Feb 19 11:08 Public
drwxr-xr-x 2 linux linux    4096 Feb 19 11:08 Templates
drwxr-xr-x 2 linux linux    4096 Feb 19 11:08 Videos
-rw-r--r-- 1 linux linux    8980 Feb 19 10:55 examples.desktop

List Files Based on Last Access Time

To get the list of all files based on last access time, use the following command-

$ ls -ltu

The sample output should be like this –

total 322428
drwxr-xr-x 3 linux linux    4096 Mar 8 14:00 Downloads
drwxr-xr-x 8 root root     4096 Mar 8 11:18 linux-dash
-rw-r--r-- 1 linux linux     8980 Mar 8 11:18 examples.desktop
-rw-r--r-- 1 root root      70706 Mar 8 11:18 Selection_007.png
-rw-r--r-- 1 root root     108159 Mar 8 11:18 root@linux: ~_005.png
-rw-r--r-- 1 root root     145629 Mar 8 11:18 Workspace 1_004.png
lrwxrwxrwx 1 linux linux       37 Mar 8 11:18 PlayOnLinux's virtual drives -> /home/linux/.PlayOnLinux//wineprefix/
drwxr-xr-x 2 linux linux    4096 Mar 8 11:18 Public
drwxr-xr-x 2 linux linux    4096 Mar 8 11:18 Documents
drwxr-xr-x 2 linux linux    4096 Mar 8 11:18 Music
drwxr-xr-x 2 linux linux    4096 Mar 8 11:18 Pictures
drwxr-xr-x 2 linux linux    4096 Mar 8 11:18 Videos
drwxr-xr-x 2 linux linux    4096 Mar 8 11:15 Templates
drwxr-xr-x 6 linux linux    4096 Mar 8 11:15 Desktop

List of Files Based on Last Modification Time

To get the last modified files, use the following command –

$ ls -ltc

The sample output should be like this –

total 322428
drwxr-xr-x 3    linux linux 4096 Mar 8 13:59 Downloads
drwxr-xr-x 6    linux linux 4096 Mar 3 14:34 Desktop
lrwxrwxrwx 1    linux linux 37 Feb 27 13:25 PlayOnLinux's virtual drives -> /home/linux/.PlayOnLinux//wineprefix/
-rw-r--r-- 1   root root 70706 Feb 23 14:52 Selection_007.png
-rw-r--r-- 1    root root 108159 Feb 23 14:49 root@linux: ~_005.png
-rw-r--r-- 1    root root 145629 Feb 23 14:47 Workspace 1_004.png
drwxr-xr-x 2    linux linux 4096 Feb 23 14:30 Pictures
-rw-rw-r-- 1    linux linux 87631 Feb 19 14:08 account.png
-rw-rw-r-- 1    linux linux 72172 Feb 19 14:07 network.png
-rw-rw-r-- 1    linux linux 98362 Feb 19 14:05 sample1.png
drwxr-xr-x 8    root root 4096 Feb 19 11:38 linux-dash
drwxr-xr-x 2    linux linux 4096 Feb 19 11:08 Documents
drwxr-xr-x 2    linux linux 4096 Feb 19 11:08 Music
drwxr-xr-x 2    linux linux 4096 Feb 19 11:08 Public
drwxr-xr-x 2    linux linux 4096 Feb 19 11:08 Templates
drwxr-xr-x 2    linux linux 4096 Feb 19 11:08 Videos
-rw-r--r-- 1    linux linux 8980 Feb 19 10:55 examples.desktop

Sorting Ouptut of ls -l based on Date

This command sorts the output of ‘ls -l’ command based on 1th field of month.

Use the following command –

ls -l | sort -k1M

The sample ouput should be like this –

drwxr-xr-x 9 tutorialspoint 2000 4096 Feb 23 10:37 psensor-1.1.3
-rw-r--r-- 1    root root 108159 Feb 23 14:49 root@linux: ~_005.png
-rw-r--r-- 1    root root 124850 Feb 23 14:52 root@linux: ~_006.png
-rw-r--r-- 1    root root 145629 Feb 23 14:46 Workspace 1_004.png
-rw-r--r-- 1    root root 145658 Feb 23 14:43 Workspace 1_003.png
-rw-r--r-- 1    root root 146010 Feb 23 14:39 Workspace 1_002.png
-rw-r--r-- 1    root root 178005 Feb 23 14:39 Workspace 1_001.png
-rw-r--r-- 1    root root 200505 Aug 29 2015 Tutorialspoint ad-DADCpj8sFCE.mp3
-rw-r--r-- 1    root root 215 Mar 3 10:28 dead.letter
................

For more in-depth sorting, use the following commands-

Find Command

Find command is used to search and locate a list of files and directories based on conditions which are specified by the user.

Sorting Files based on Month

To find the sorted root files based on month, use the following command –

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

The above command gives a complete list of top 11 entries which were accessed based on month.

May /etc/newt/palette.ubuntu
May /etc/newt/palette.original
May /etc/ltrace.conf
Mar /etc/pulse/daemon.conf
Mar /etc/pulse/default.pa
Jan /etc/pulse/system.pa
Mar /etc/pulse/client.conf
Mar /etc/gtk-3.0/settings.ini
Jul /etc/gtk-3.0/im-multipress.conf
Feb /etc/subgid-

To find the sorted root files using the first key with month, use -k1M option as shown below –

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

The sample output should be like this –

Jan /etc/pulse/system.pa
Feb /etc/subgid-
Mar /etc/gtk-3.0/settings.ini
Mar /etc/pulse/client.conf
Mar /etc/pulse/daemon.conf
Mar /etc/pulse/default.pa
May /etc/ltrace.conf
May /etc/newt/palette.original
May /etc/newt/palette.ubuntu
Jul /etc/gtk-3.0/im-multipress.conf

The above command has sorted according to the month.

Sort Files Based on Date

To find the sorted root files based on date, use the following command –

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

The above command gives the result according to date as shown below –

05/14/13 22:26:41.0000000000 /etc/newt/palette.ubuntu
05/14/13 22:26:41.0000000000 /etc/newt/palette.original
05/10/14 05:20:35.0000000000 /etc/ltrace.conf
03/08/16 11:14:01.9113136790 /etc/pulse/daemon.conf
03/08/16 11:14:01.9193136790 /etc/pulse/default.pa
01/29/15 04:17:39.0000000000 /etc/pulse/system.pa
03/08/16 11:14:01.3433136590 /etc/pulse/client.conf
03/08/16 11:14:00.0873136140 /etc/gtk-3.0/settings.ini
07/01/15 08:44:19.0000000000 /etc/gtk-3.0/im-multipress.conf
02/22/16 10:49:09.0000000000 /etc/subgid-

Sorting Files Based on Time

To find the sorted root files based on time, use the following command –

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

The sample output should be like this –

22:26:41.0000000000 /etc/newt/palette.ubuntu
22:26:41.0000000000 /etc/newt/palette.original
05:20:35.0000000000 /etc/ltrace.conf
11:14:01.9113136790 /etc/pulse/daemon.conf
11:14:01.9193136790 /etc/pulse/default.pa
04:17:39.0000000000 /etc/pulse/system.pa
11:14:01.3433136590 /etc/pulse/client.conf
11:14:00.0873136140 /etc/gtk-3.0/settings.ini
08:44:19.0000000000 /etc/gtk-3.0/im-multipress.conf
10:49:09.0000000000 /etc/subgid-

Congratulations! Now, you know “How to Find and Sort Files Based on Modification Date and Time in Linux”. We’ll learn more about these types of commands in our next Linux post. Keep reading!

raja
Published on 21-Oct-2019 10:28:26
Advertisements