1742

How can I sort the output of ls by last modified date?

1

10 Answers 10

2103
ls -t

or (for reverse, most recent at bottom):

ls -tr

The ls man page describes this in more details, and lists other options.

13
  • 396
    ls -halt is for human readable, show hidden, print details, sort by date. Commented Oct 1, 2013 at 5:24
  • 12
    In case anyone's wondering, both the -t and -r arguments are specified in the section about ls in the POSIX standard, so should be compatible across Unices.
    – Mark Amery
    Commented Oct 27, 2015 at 12:09
  • 11
    ls -llt for showing date-timestamp along with sorting Commented Sep 26, 2016 at 20:01
  • 5
    Yes it is possible Commented Feb 20, 2017 at 13:09
  • 16
    @EvgeniSergeev DONT MEMORISE ls -halt a simple mistype may cause your server to crash! linux.die.net/man/8/halt
    – Isaac
    Commented Jul 9, 2017 at 21:43
186

Try this: ls -ltr. It will give you the recent to the end of the list

1
  • I used this to get the list of files in my Git repository by their last edit date. ls -alt $(git ls-files -m) Thanks! Commented Aug 14, 2019 at 22:07
56

For a complete answer here is what I use: ls -lrth

Put this in your startup script /etc/bashrc and assign an alias like this: alias l='ls -lrth' Restart your terminal and you should be able to type l and see a long list of files.

2
  • 15
    You can also call source /etc/bashrc if you want to add it to your repertoire while running. Commented Feb 11, 2015 at 7:57
  • 1
    You can also add it in ~/.bash_aliases just for your user (one can create the file if it doesn't exist already
    – Dinei
    Commented Apr 24, 2018 at 1:23
41

I use sometime this:

find . -type f -mmin -5 -print0 | xargs -0 /bin/ls -tr

or

find . -type f -mmin -5 -print0 | xargs -0 /bin/ls -ltr

to look recursively for which files were modified in last 5 minutes.

... or now, with recent version of GNU find:

find . -type f -mmin -5 -exec ls -ltr {} +

... and even for not limiting to files:

find . -mmin -5 -exec ls -ltrd {} +

(note the -d switch to ls for not displaying content of directories)

More robust way?

Have a look at my answer to find and sort by date modified

4
24

Add:

alias lt='ls -Alhtr'

in $homedir/.bashrc

24

Mnemonic

For don't ignore entries starting with . and sort by date (newest first):

ls -at

For don't ignore entries starting with . and reverse sort by date (oldest first):

ls -art

For don't ignore entries starting with ., use a long listing format and sort by date (newest first):

ls -alt

For print human readable sizes, don't ignore entries starting with ., use a long listing format and sort by date (newest first) (@EvgeniSergeev note):

ls -halt

but be careful with the last one, because a simple mistype can cause a server crash... (@Isaac note)

18

Find all files on the file system that were modified maximally 3 * 24 hours (3 days) ago till now:

find / -ctime 3
0
15

To show 10 most recent sorted by date, I use something like this:

ls -t ~/Downloads | head -10

or to show oldest

ls -tr ~/Downloads | tail -10
2
  • 1
    it givls -t head -2 and ls -tr | tail -2 gives same result, option (-t/-tr) should be kept fixed and modified the tail/head or vice verse, modifing both is like modyfing nothing
    – DDS
    Commented Jun 27, 2018 at 16:09
  • 1
    Did you see the comment above? Indeed, one should use head in both commands (to change the sort order too), or use ls -t in both commands (which would always sort descending by date).
    – Arjan
    Commented Feb 28, 2020 at 11:15
10

Using only very basic Unix commands:

ls -nl | sort -k 8,8n -k 6,6M

This worked on Linux; column 8 is "n" (numeric), column 6 is "M", month.

I'm new at sort, so this answer could probably be improved. Not to mention, it needs additional options to ls and sort to use exact timestamps, but not everyone will need this.

1
6

One possible way of showing for example last 10 modified files is following command:

ls -lrth | tail -n 10

Description of above command:

ls - list

arguments:

l - long
r - reverse
t - sort by time
h - human readable

then it's piped to tail command, which shows only 10 recent lines, defined by n parameter (number of lines)...

You must log in to answer this question.