• Home »
  • Insights »
  • Linux Sort By Date, List Files In A Directory By Date On A Linux Server

Linux Sort By Date, List Files In A Directory By Date On A Linux Server

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

bash

  1. [root@dev sysconfig]# ls -lt
  2. total 392
  3. drwxr-xr-x 2 root root 4096 Dec 13 16:12 network-scripts
  4. drwxr-xr-x 2 root root 4096 Nov 17 03:38 rhn
  5. -rw-r--r-- 1 root root  180 Nov 17 03:38 kernel
  6. -rw-r--r-- 1 root root  297 Nov 17 03:38 authconfig
  7. drwxr-xr-x 2 root root 4096 Nov 16 11:23 console
  8. drwxr-xr-x 2 root root 4096 Nov 16 11:23 modules
  9. drwxr-xr-x 4 root root 4096 Nov 16 11:23 networking
  10. -rw-r--r-- 1 root root 3604 Nov  8 07:42 autofs
  11. -rw-r--r-- 1 root root   68 Oct 28 12:39 network
  12. -rw-r--r-- 1 root root   17 Oct 19 07:41 firstboot
  13. -rw-r--r-- 1 root root 1763 Oct 19 07:40 iptables-config
  14. -rw-r--r-- 1 root root   79 Oct 19 07:40 system-config-securitylevel
  15. -rw-r--r-- 1 root root   42 Oct 19 07:36 clock
  16. -rw-r--r-- 1 root root    0 Oct 19 07:36 desktop
  17. -rw-r--r-- 1 root root   25 Oct 19 07:36 grub
  18. -rw-r--r-- 1 root root   47 Oct 19 07:36 i18n
  19. -rw-r--r-- 1 root root   32 Oct 19 07:36 keyboard
  20. lrwxrwxrwx 1 root root   17 Oct 19 07:34 selinux -> ../selinux/config
  21. drwxr-xr-x 2 root root 4096 Oct 19 07:31 cbq
  22. -rw-r--r-- 1 root root 1724 Aug  6  2010 nfs
  23. -rw-r--r-- 1 root root   47 Aug  4  2010 udev-stw
  24. drwxr-xr-x 2 root root 4096 Jul 21  2010 mkinitrd
  25. drwxr-xr-x 2 root root 4096 Jun 10  2010 pgsql
  26. -rwxr-xr-x 1 root root   85 May 25  2010 snmpd.options
  27. -rwxr-xr-x 1 root root   76 May 25  2010 snmptrapd.options
  28. -rw-r--r-- 1 root root 2377 Apr 24  2010 cpuspeed
  29. -rw-r--r-- 1 root root  168 Apr  4  2010 kudzu
  30. -rw-r--r-- 1 root root  610 Apr  2  2010 syslog
  31. -rw-r--r-- 1 root root  297 Mar 31  2010 squid
  32. -rw-r----- 1 root root  646 Mar 31  2010 auditd
  33. -rw-r--r-- 1 root root  134 Mar 31  2010 lm_sensors
  34. -rw-r--r-- 1 root root  391 Mar 30  2010 wpa_supplicant
  35. -rw-r--r-- 1 root root  433 Mar 17  2010 saslauthd
  36. -rw-r--r-- 1 root root  403 Jan 26  2010 atd
  37. -rw-r--r-- 1 root root  512 Jan  5  2010 crond
  38. -rw-r--r-- 1 root root  209 Dec 18  2009 ntpd
  39. -rw------- 1 root root 1753 Nov  2  2009 ip6tables-config
  40. -rw-r--r-- 1 root root 1118 Sep  3  2009 irqbalance
  41. -rw-r--r-- 1 root root 1068 Jul  4  2009 init
  42. -rw-r--r-- 1 root root  634 Jul  4  2009 netconsole
  43. -rw-r--r-- 1 root root  153 Jul  4  2009 rawdevices
  44. -rw-r--r-- 1 root root  544 Jul  4  2009 readonly-root
  45. -rw-r--r-- 1 root root  807 Jan 22  2009 pm-action
  46. -rw-r--r-- 1 root root   57 Jan 21  2009 smartmontools
  47. -rw-r--r-- 1 root root  232 Jul 14  2008 bluetooth
  48. -rw-r--r-- 1 root root   31 Jul 14  2008 dund
  49. -rw-r--r-- 1 root root   20 Jul 14  2008 hidd
  50. -rw-r--r-- 1 root root   71 Jul 14  2008 pand
  51. -rw-r--r-- 1 root root  903 Nov 11  2007 conman
  52. -rw-r--r-- 1 root root  376 Mar 14  2007 xinetd
  53. -rw-r--r-- 1 root root   58 Jan  6  2007 irda
  54. -rw-r--r-- 1 root root 1750 Jan  6  2007 tux
  55. -rw-r--r-- 1 root root 2391 Jan 16  2006 dsm_sa_ipmi
  56. -rw-r--r-- 1 root root   16 Feb 24  2005 krb524
  57. -rw-r--r-- 1 root root 1459 Nov 23  2004 prelink
  58. [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

bash

  1. [root@dev sysconfig]# ls -ltr
  2. total 392
  3. -rw-r--r-- 1 root root 1459 Nov 23  2004 prelink
  4. -rw-r--r-- 1 root root   16 Feb 24  2005 krb524
  5. -rw-r--r-- 1 root root 2391 Jan 16  2006 dsm_sa_ipmi
  6. -rw-r--r-- 1 root root 1750 Jan  6  2007 tux
  7. -rw-r--r-- 1 root root   58 Jan  6  2007 irda
  8. -rw-r--r-- 1 root root  376 Mar 14  2007 xinetd
  9. -rw-r--r-- 1 root root  903 Nov 11  2007 conman
  10. -rw-r--r-- 1 root root   71 Jul 14  2008 pand
  11. -rw-r--r-- 1 root root   20 Jul 14  2008 hidd
  12. -rw-r--r-- 1 root root   31 Jul 14  2008 dund
  13. -rw-r--r-- 1 root root  232 Jul 14  2008 bluetooth
  14. -rw-r--r-- 1 root root   57 Jan 21  2009 smartmontools
  15. -rw-r--r-- 1 root root  807 Jan 22  2009 pm-action
  16. -rw-r--r-- 1 root root  544 Jul  4  2009 readonly-root
  17. -rw-r--r-- 1 root root  153 Jul  4  2009 rawdevices
  18. -rw-r--r-- 1 root root  634 Jul  4  2009 netconsole
  19. -rw-r--r-- 1 root root 1068 Jul  4  2009 init
  20. -rw-r--r-- 1 root root 1118 Sep  3  2009 irqbalance
  21. -rw------- 1 root root 1753 Nov  2  2009 ip6tables-config
  22. -rw-r--r-- 1 root root  209 Dec 18  2009 ntpd
  23. -rw-r--r-- 1 root root  512 Jan  5  2010 crond
  24. -rw-r--r-- 1 root root  403 Jan 26  2010 atd
  25. -rw-r--r-- 1 root root  433 Mar 17  2010 saslauthd
  26. -rw-r--r-- 1 root root  391 Mar 30  2010 wpa_supplicant
  27. -rw-r--r-- 1 root root  134 Mar 31  2010 lm_sensors
  28. -rw-r----- 1 root root  646 Mar 31  2010 auditd
  29. -rw-r--r-- 1 root root  297 Mar 31  2010 squid
  30. -rw-r--r-- 1 root root  610 Apr  2  2010 syslog
  31. -rw-r--r-- 1 root root  168 Apr  4  2010 kudzu
  32. -rw-r--r-- 1 root root 2377 Apr 24  2010 cpuspeed
  33. -rwxr-xr-x 1 root root   76 May 25  2010 snmptrapd.options
  34. -rwxr-xr-x 1 root root   85 May 25  2010 snmpd.options
  35. drwxr-xr-x 2 root root 4096 Jun 10  2010 pgsql
  36. drwxr-xr-x 2 root root 4096 Jul 21  2010 mkinitrd
  37. -rw-r--r-- 1 root root   47 Aug  4  2010 udev-stw
  38. -rw-r--r-- 1 root root 1724 Aug  6  2010 nfs
  39. drwxr-xr-x 2 root root 4096 Oct 19 07:31 cbq
  40. lrwxrwxrwx 1 root root   17 Oct 19 07:34 selinux -> ../selinux/config
  41. -rw-r--r-- 1 root root   32 Oct 19 07:36 keyboard
  42. -rw-r--r-- 1 root root   47 Oct 19 07:36 i18n
  43. -rw-r--r-- 1 root root   25 Oct 19 07:36 grub
  44. -rw-r--r-- 1 root root    0 Oct 19 07:36 desktop
  45. -rw-r--r-- 1 root root   42 Oct 19 07:36 clock
  46. -rw-r--r-- 1 root root   79 Oct 19 07:40 system-config-securitylevel
  47. -rw-r--r-- 1 root root 1763 Oct 19 07:40 iptables-config
  48. -rw-r--r-- 1 root root   17 Oct 19 07:41 firstboot
  49. -rw-r--r-- 1 root root   68 Oct 28 12:39 network
  50. -rw-r--r-- 1 root root 3604 Nov  8 07:42 autofs
  51. drwxr-xr-x 4 root root 4096 Nov 16 11:23 networking
  52. drwxr-xr-x 2 root root 4096 Nov 16 11:23 modules
  53. drwxr-xr-x 2 root root 4096 Nov 16 11:23 console
  54. -rw-r--r-- 1 root root  297 Nov 17 03:38 authconfig
  55. -rw-r--r-- 1 root root  180 Nov 17 03:38 kernel
  56. drwxr-xr-x 2 root root 4096 Nov 17 03:38 rhn
  57. drwxr-xr-x 2 root root 4096 Dec 13 16:12 network-scripts
  58. [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.

Linux Command Line and Shell Scripting Bible, Second Edition (Paperback)


List Price: $49.99 USD
New From: $8.00 USD In Stock
Used from: $4.69 USD In Stock

A Practical Guide to Linux Commands, Editors, and Shell Programming (2nd Edition) (Paperback)


List Price: $49.99
New From: $13.50 USD In Stock
Used from: $6.94 USD In Stock

Share