It can be useful on a Linux server to sort all of the files in a directory by date. Sometimes you may want to backup files older than a certain date or you may just want to see what is in a directory and what date those files were created. Use the information below to list all of the files in a directory sorted by date.
Sort All Files In A Directory By Date: Newest To Oldest
- [root@dev sysconfig]# ls -lt
- total 392
- drwxr-xr-x 2 root root 4096 Dec 13 16:12 network-scripts
- drwxr-xr-x 2 root root 4096 Nov 17 03:38 rhn
- -rw-r--r-- 1 root root 180 Nov 17 03:38 kernel
- -rw-r--r-- 1 root root 297 Nov 17 03:38 authconfig
- drwxr-xr-x 2 root root 4096 Nov 16 11:23 console
- drwxr-xr-x 2 root root 4096 Nov 16 11:23 modules
- drwxr-xr-x 4 root root 4096 Nov 16 11:23 networking
- -rw-r--r-- 1 root root 3604 Nov 8 07:42 autofs
- -rw-r--r-- 1 root root 68 Oct 28 12:39 network
- -rw-r--r-- 1 root root 17 Oct 19 07:41 firstboot
- -rw-r--r-- 1 root root 1763 Oct 19 07:40 iptables-config
- -rw-r--r-- 1 root root 79 Oct 19 07:40 system-config-securitylevel
- -rw-r--r-- 1 root root 42 Oct 19 07:36 clock
- -rw-r--r-- 1 root root 0 Oct 19 07:36 desktop
- -rw-r--r-- 1 root root 25 Oct 19 07:36 grub
- -rw-r--r-- 1 root root 47 Oct 19 07:36 i18n
- -rw-r--r-- 1 root root 32 Oct 19 07:36 keyboard
- lrwxrwxrwx 1 root root 17 Oct 19 07:34 selinux -> ../selinux/config
- drwxr-xr-x 2 root root 4096 Oct 19 07:31 cbq
- -rw-r--r-- 1 root root 1724 Aug 6 2010 nfs
- -rw-r--r-- 1 root root 47 Aug 4 2010 udev-stw
- drwxr-xr-x 2 root root 4096 Jul 21 2010 mkinitrd
- drwxr-xr-x 2 root root 4096 Jun 10 2010 pgsql
- -rwxr-xr-x 1 root root 85 May 25 2010 snmpd.options
- -rwxr-xr-x 1 root root 76 May 25 2010 snmptrapd.options
- -rw-r--r-- 1 root root 2377 Apr 24 2010 cpuspeed
- -rw-r--r-- 1 root root 168 Apr 4 2010 kudzu
- -rw-r--r-- 1 root root 610 Apr 2 2010 syslog
- -rw-r--r-- 1 root root 297 Mar 31 2010 squid
- -rw-r----- 1 root root 646 Mar 31 2010 auditd
- -rw-r--r-- 1 root root 134 Mar 31 2010 lm_sensors
- -rw-r--r-- 1 root root 391 Mar 30 2010 wpa_supplicant
- -rw-r--r-- 1 root root 433 Mar 17 2010 saslauthd
- -rw-r--r-- 1 root root 403 Jan 26 2010 atd
- -rw-r--r-- 1 root root 512 Jan 5 2010 crond
- -rw-r--r-- 1 root root 209 Dec 18 2009 ntpd
- -rw------- 1 root root 1753 Nov 2 2009 ip6tables-config
- -rw-r--r-- 1 root root 1118 Sep 3 2009 irqbalance
- -rw-r--r-- 1 root root 1068 Jul 4 2009 init
- -rw-r--r-- 1 root root 634 Jul 4 2009 netconsole
- -rw-r--r-- 1 root root 153 Jul 4 2009 rawdevices
- -rw-r--r-- 1 root root 544 Jul 4 2009 readonly-root
- -rw-r--r-- 1 root root 807 Jan 22 2009 pm-action
- -rw-r--r-- 1 root root 57 Jan 21 2009 smartmontools
- -rw-r--r-- 1 root root 232 Jul 14 2008 bluetooth
- -rw-r--r-- 1 root root 31 Jul 14 2008 dund
- -rw-r--r-- 1 root root 20 Jul 14 2008 hidd
- -rw-r--r-- 1 root root 71 Jul 14 2008 pand
- -rw-r--r-- 1 root root 903 Nov 11 2007 conman
- -rw-r--r-- 1 root root 376 Mar 14 2007 xinetd
- -rw-r--r-- 1 root root 58 Jan 6 2007 irda
- -rw-r--r-- 1 root root 1750 Jan 6 2007 tux
- -rw-r--r-- 1 root root 2391 Jan 16 2006 dsm_sa_ipmi
- -rw-r--r-- 1 root root 16 Feb 24 2005 krb524
- -rw-r--r-- 1 root root 1459 Nov 23 2004 prelink
- [root@dev sysconfig]#
Above you can see that the files are listed newest files first. If you need to list the files in the directory with the oldest files first you can simply add the -r switch to sort the files in reverse as shown in the below example.
Sort All Files In A Directory By Date: Oldest To Newest
- [root@dev sysconfig]# ls -ltr
- total 392
- -rw-r--r-- 1 root root 1459 Nov 23 2004 prelink
- -rw-r--r-- 1 root root 16 Feb 24 2005 krb524
- -rw-r--r-- 1 root root 2391 Jan 16 2006 dsm_sa_ipmi
- -rw-r--r-- 1 root root 1750 Jan 6 2007 tux
- -rw-r--r-- 1 root root 58 Jan 6 2007 irda
- -rw-r--r-- 1 root root 376 Mar 14 2007 xinetd
- -rw-r--r-- 1 root root 903 Nov 11 2007 conman
- -rw-r--r-- 1 root root 71 Jul 14 2008 pand
- -rw-r--r-- 1 root root 20 Jul 14 2008 hidd
- -rw-r--r-- 1 root root 31 Jul 14 2008 dund
- -rw-r--r-- 1 root root 232 Jul 14 2008 bluetooth
- -rw-r--r-- 1 root root 57 Jan 21 2009 smartmontools
- -rw-r--r-- 1 root root 807 Jan 22 2009 pm-action
- -rw-r--r-- 1 root root 544 Jul 4 2009 readonly-root
- -rw-r--r-- 1 root root 153 Jul 4 2009 rawdevices
- -rw-r--r-- 1 root root 634 Jul 4 2009 netconsole
- -rw-r--r-- 1 root root 1068 Jul 4 2009 init
- -rw-r--r-- 1 root root 1118 Sep 3 2009 irqbalance
- -rw------- 1 root root 1753 Nov 2 2009 ip6tables-config
- -rw-r--r-- 1 root root 209 Dec 18 2009 ntpd
- -rw-r--r-- 1 root root 512 Jan 5 2010 crond
- -rw-r--r-- 1 root root 403 Jan 26 2010 atd
- -rw-r--r-- 1 root root 433 Mar 17 2010 saslauthd
- -rw-r--r-- 1 root root 391 Mar 30 2010 wpa_supplicant
- -rw-r--r-- 1 root root 134 Mar 31 2010 lm_sensors
- -rw-r----- 1 root root 646 Mar 31 2010 auditd
- -rw-r--r-- 1 root root 297 Mar 31 2010 squid
- -rw-r--r-- 1 root root 610 Apr 2 2010 syslog
- -rw-r--r-- 1 root root 168 Apr 4 2010 kudzu
- -rw-r--r-- 1 root root 2377 Apr 24 2010 cpuspeed
- -rwxr-xr-x 1 root root 76 May 25 2010 snmptrapd.options
- -rwxr-xr-x 1 root root 85 May 25 2010 snmpd.options
- drwxr-xr-x 2 root root 4096 Jun 10 2010 pgsql
- drwxr-xr-x 2 root root 4096 Jul 21 2010 mkinitrd
- -rw-r--r-- 1 root root 47 Aug 4 2010 udev-stw
- -rw-r--r-- 1 root root 1724 Aug 6 2010 nfs
- drwxr-xr-x 2 root root 4096 Oct 19 07:31 cbq
- lrwxrwxrwx 1 root root 17 Oct 19 07:34 selinux -> ../selinux/config
- -rw-r--r-- 1 root root 32 Oct 19 07:36 keyboard
- -rw-r--r-- 1 root root 47 Oct 19 07:36 i18n
- -rw-r--r-- 1 root root 25 Oct 19 07:36 grub
- -rw-r--r-- 1 root root 0 Oct 19 07:36 desktop
- -rw-r--r-- 1 root root 42 Oct 19 07:36 clock
- -rw-r--r-- 1 root root 79 Oct 19 07:40 system-config-securitylevel
- -rw-r--r-- 1 root root 1763 Oct 19 07:40 iptables-config
- -rw-r--r-- 1 root root 17 Oct 19 07:41 firstboot
- -rw-r--r-- 1 root root 68 Oct 28 12:39 network
- -rw-r--r-- 1 root root 3604 Nov 8 07:42 autofs
- drwxr-xr-x 4 root root 4096 Nov 16 11:23 networking
- drwxr-xr-x 2 root root 4096 Nov 16 11:23 modules
- drwxr-xr-x 2 root root 4096 Nov 16 11:23 console
- -rw-r--r-- 1 root root 297 Nov 17 03:38 authconfig
- -rw-r--r-- 1 root root 180 Nov 17 03:38 kernel
- drwxr-xr-x 2 root root 4096 Nov 17 03:38 rhn
- drwxr-xr-x 2 root root 4096 Dec 13 16:12 network-scripts
- [root@dev sysconfig]#
To read more about the switches you can use with the Linux ls command read the man page by typing “man ls” from a shell or by clicking here to see the ls man page version on Question Defense.