How to list files by date in Linux
The “ls -lt” command can be used to get the list of files sorted by their modification date.
[neo@techpulp ~]# ls -lt total 60 -rw-rw-r-- 1 neo neo 122 2008-12-06 01:39 monitor.sh -rw-rw-r-- 1 neo neo 44 2008-12-06 01:33 1.sh -rw-rw-r-- 1 neo neo 10381 2008-12-06 00:53 ifconfig.pdf drwxrwxr-x 3 neo neo 4096 2008-12-05 20:06 workspace drwxr-xr-x 3 neo neo 4096 2008-12-05 02:35 Desktop drwxrwxr-x 3 neo neo 4096 2008-12-05 01:46 Examples drwxr-xr-x 2 neo neo 4096 2008-12-05 00:59 Download drwxr-xr-x 2 neo neo 4096 2008-12-04 22:11 Documents drwxr-xr-x 2 neo neo 4096 2008-12-04 22:11 Music drwxr-xr-x 2 neo neo 4096 2008-12-04 22:11 Pictures drwxr-xr-x 2 neo neo 4096 2008-12-04 22:11 Public drwxr-xr-x 2 neo neo 4096 2008-12-04 22:11 Templates drwxr-xr-x 2 neo neo 4096 2008-12-04 22:11 Videos [neo@techpulp ~]#
You can add “-r” option to the previous command to reverse the order i.e. to get oldest file first.
[neo@techpulp ~]# ls -lrt total 60 drwxr-xr-x 2 neo neo 4096 2008-12-04 22:11 Videos drwxr-xr-x 2 neo neo 4096 2008-12-04 22:11 Templates drwxr-xr-x 2 neo neo 4096 2008-12-04 22:11 Public drwxr-xr-x 2 neo neo 4096 2008-12-04 22:11 Pictures drwxr-xr-x 2 neo neo 4096 2008-12-04 22:11 Music drwxr-xr-x 2 neo neo 4096 2008-12-04 22:11 Documents drwxr-xr-x 2 neo neo 4096 2008-12-05 00:59 Download drwxrwxr-x 3 neo neo 4096 2008-12-05 01:46 Examples drwxr-xr-x 3 neo neo 4096 2008-12-05 02:35 Desktop drwxrwxr-x 3 neo neo 4096 2008-12-05 20:06 workspace -rw-rw-r-- 1 neo neo 10381 2008-12-06 00:53 ifconfig.pdf -rw-rw-r-- 1 neo neo 44 2008-12-06 01:33 1.sh -rw-rw-r-- 1 neo neo 122 2008-12-06 01:39 monitor.sh [neo@techpulp ~]#
If you want to get plain list of file names, just avoid “-l” option in the above command. You can use output of this command to populate a shell variable in a script or variable in GNU Makefile.
[neo@techpulp ~]# ls -t monitor.sh 1.sh ifconfig.pdf workspace Desktop Examples Download Documents Music Pictures Public Templates Videos [neo@techpulp ~]# ls -rt Videos Templates Public Pictures Music Documents Download Examples Desktop workspace ifconfig.pdf 1.sh monitor.sh [neo@techpulp ~]#


about 1 year ago
Thanks for sharing the info.
I use “ls -ltr” extensively when I attempt to download latest files from a FTP server. It shows last modified/created files at the end so you can always see the required file list even after scrolling of text.