Find And Sort Files Based On Access, Modification Date And Time In Linux

Sort Files Based On Access, Modification Date And Time In Linux

A while we write guide that described how to organize the similar type of files in specific folders in Unix-like operating systems. In that method, we can organize the same type of files in a folder. In other words, we organized the files based on their extension. We don’t know whether the file is new or old one. We simply put the same type of files in a folder. Today, in this brief guide, we are going to find and sort files based on access and modification date and time, not extension. In case, if you want to find a particular file accessed or modified on a particular date or time, you’re on the right track. Read on.

Find And Sort Files Based On Access, Modification Date And Time In Linux

You need not to install any extra applications to find and sort files. For the purpose of this guide, we are going to use three Linux commands namely ls, find, and sort. These commands are the part of Linux Kernel, so don’t bother installing them.

As may already know, the ls command lists a directory contents with additional information about them such as date and time of modification or access, permissions, size, owner, group etc. It will sort the entries of the given directory, alphabetically. If you didn’t mention the directory, it will list the contents of current working directory.

The find command is used to search for files in a directory hierarchy.

The sort command sorts the lines of text files and writes sorted concatenation of all FILE(s) to standard output.

Find and sort files based on last access time using ls command

The following will list files based on the last access time. The newest files will be listed first.

ls -ltu

Sample output:

total 104
drwxr-xr-x 18 sk users 4096 May 3 18:02 Personal
drwxr-xr-x 3 sk users 4096 May 3 11:57 Notebooks
drwxr-xr-x 9 sk users 4096 May 3 11:57 Official
drwxrwxr-x 3 sk users 4096 May 3 11:57 en-TED
drwxr-xr-x 5 sk users 4096 May 3 11:57 Soft_Backup
drwxr-xr-x 9 sk users 4096 May 3 11:57 'VirtualBox VMs'
drwxr-xr-x 2 sk users 4096 May 3 11:57 Wallpapers
drwxr-xr-x 2 sk users 4096 May 3 11:57 Public
drwxr-xr-x 2 sk users 4096 May 3 11:57 Documents
drwxr-xr-x 2 sk users 4096 May 3 11:57 Music
drwxr-xr-x 2 sk users 4096 May 3 11:57 Videos
drwxr-xr-x 2 sk users 4096 May 3 11:56 Desktop
drwxr-xr-x 2 sk users 4096 May 3 11:56 Templates
drwxr-xr-x 5 sk users 4096 May 2 20:53 Entertainment
-rw-r--r-- 1 sk users 951 May 2 20:52 'pre code.txt'
-rw-r--r-- 1 sk users 50 May 2 20:52 file.txt
drwxr-xr-x 11 sk users 20480 May 2 20:34 Downloads
drwxr-xr-x 5 sk users 12288 May 2 20:13 Pictures

Here,

  • -u – Sort by access time, newest first

Find and sort files based on modification time using ls command

To list files based on the modification time, run:

ls -lt

Here,

  • -l – Indicates long listing format
  • -t – Sort by modification time, newest first

Sample output:

total 104
drwxr-xr-x 11 sk users 20480 May 2 20:34 Downloads
drwxr-xr-x 5 sk users 12288 May 1 17:32 Pictures
drwxr-xr-x 9 sk users 4096 May 1 16:45 'VirtualBox VMs'
drwxr-xr-x 2 sk users 4096 Apr 24 20:30 Documents
drwxr-xr-x 2 sk users 4096 Apr 11 13:02 Desktop
-rw-r--r-- 1 sk users 951 Apr 7 20:20 'pre code.txt'
drwxr-xr-x 9 sk users 4096 Mar 27 19:31 Official
-rw-r--r-- 1 sk users 50 Mar 23 20:24 file.txt
drwxr-xr-x 2 sk users 4096 Mar 21 17:43 Videos
drwxr-xr-x 3 sk users 4096 Mar 14 16:32 Notebooks
drwxr-xr-x 2 sk users 4096 Jan 7 14:55 Music
drwxr-xr-x 18 sk users 4096 Dec 19 14:36 Personal
drwxrwxr-x 3 sk users 4096 Nov 30 01:57 en-TED
drwxr-xr-x 5 sk users 4096 Aug 20 2016 Entertainment
drwxr-xr-x 2 sk users 4096 May 11 2016 Public
drwxr-xr-x 2 sk users 4096 May 11 2016 Templates
drwxr-xr-x 2 sk users 4096 May 9 2016 Wallpapers
drwxr-xr-x 5 sk users 4096 May 4 2016 Soft_Backup

The above command will sort the files of the current working directory based on the modification time. The newest files will be displayed first.

To list the files of a specific directory based on modification time, type:

ls -lt ~/Downloads/

To list the files in reverse order, i.e oldest files files, use ‘r’ flag like below:

ls -ltr

Find and sort files based on last modification time using ls command

The command lists the files based on the last modification time. It means, this command lists the files first whose any status information (such as owner, group, permissions, size etc) have been recently changed.

ls -ltc

Sample output:

total 104
drwxr-xr-x 11 sk users 20480 May 2 20:34 Downloads
drwxr-xr-x 5 sk users 12288 May 1 17:32 Pictures
drwxr-xr-x 9 sk users 4096 May 1 16:45 'VirtualBox VMs'
drwxr-xr-x 2 sk users 4096 Apr 24 20:30 Documents
drwxr-xr-x 2 sk users 4096 Apr 11 13:02 Desktop
-rw-r--r-- 1 sk users 50 Apr 8 16:28 file.txt
-rw-r--r-- 1 sk users 951 Apr 7 20:20 'pre code.txt'
drwxr-xr-x 9 sk users 4096 Mar 27 19:31 Official
drwxr-xr-x 2 sk users 4096 Mar 21 17:43 Videos
drwxr-xr-x 3 sk users 4096 Mar 14 16:32 Notebooks
drwxrwxr-x 3 sk users 4096 Jan 18 12:36 en-TED
drwxr-xr-x 2 sk users 4096 Jan 7 14:55 Music
drwxr-xr-x 18 sk users 4096 Dec 19 14:36 Personal
drwxr-xr-x 5 sk users 4096 Aug 20 2016 Entertainment
drwxr-xr-x 2 sk users 4096 May 11 2016 Public
drwxr-xr-x 2 sk users 4096 May 11 2016 Templates
drwxr-xr-x 2 sk users 4096 May 9 2016 Wallpapers
drwxr-xr-x 5 sk users 4096 May 9 2016 Soft_Backup

Here,

  • -c – Sort by last modification time

Find and short files based on date

The following command lists the files based on last accessed date and time. Newest files will be listed first.

find /home/sk/ostechnix/ -type f -printf "\n%AD %AT %p" | head -n 10

Sample output:

04/27/17 15:51:16.4508342970 /home/sk/ostechnix/The Devops toolkit.pdf
04/27/17 15:51:17.1375009660 /home/sk/ostechnix/THE LINUX COMMAND LINE.pdf
04/27/17 15:51:12.9508342850 /home/sk/ostechnix/Google power search.pdf
04/27/17 15:51:10.3475009420 /home/sk/ostechnix/Docker.pdf
04/27/17 15:51:14.0108342880 /home/sk/ostechnix/Linux Shell Scripting.pdf
04/27/17 15:51:17.4208343000 /home/sk/ostechnix/WpPMaq8.jpg
04/27/17 15:51:12.7675009510 /home/sk/ostechnix/DoubleClick_Mobile_Web_Speed_Toolkit.pdf
04/27/17 15:51:01.9308342460 /home/sk/ostechnix/Ed Sheeran - Shape Of You.mp3
04/27/17 15:51:02.7341675830 /home/sk/ostechnix/tor-browser_en-US/start-tor-browser.desktop

In the above output, the first column shows the last accessed date of the file, second column shows the last accessed time, and third column shows the name of file. Here, ‘head -n 10’ is not the number files. It is number of entries in the output including the blank spaces.

Also, you can use “sort” command with “ls” command to list the files based on last accessed month, date, and time like below.

ls -l ostechnix/ | sort -k6M -k7n | head -n 10

Sample output:

total 44024
drwx------ 3 sk users 4096 Apr 27 15:51 tor-browser_en-US
drwxr-xr-x 3 sk users 4096 Apr 27 15:51 Arch master
-rw-r--r-- 1 sk users 183396 Apr 27 15:51 Linux commandline cheat sheet.pdf
-rw-r--r-- 1 sk users 2031375 Apr 27 15:51 Google power search.pdf
-rw-r--r-- 1 sk users 233955 Apr 27 15:51 bash_tips.pdf
-rw-r--r-- 1 sk users 2597261 Apr 27 15:51 Docker.pdf
-rw-r--r-- 1 sk users 270623 Apr 27 15:51 sag-0.6.1.pdf.gz
-rw-r--r-- 1 sk users 3857569 Apr 27 15:51 THE LINUX COMMAND LINE.pdf
-rw-r--r-- 1 sk users 3902301 Apr 27 15:51 Ed Sheeran - Shape Of You.mp3

Here,

  • -k6M – Lists files monthly wise (6th field) in the output.
  • -k7n – Lists files date wise (7th field) in the output.

And, that’s all. You know now how to find and sort files based on the access, modification time and date in Linux. As you can see in the above examples, it is fairly easy to sort the files. For more details, refer man pages.

man ls
man find
man sort

If you find our guides useful, please share them on your social, professional networks and support OSTechNix. More good stuffs to come. Stay tuned!

Cheers!

Thanks for stopping by!

Help us to help you:

Have a Good day!!

You may also like...