1717

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

Garrett
  • 276
  • 3
  • 8

10 Answers10

2066
ls -t

or (for reverse, most recent at bottom):

ls -tr

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

Tamara Wijsman
  • 57,083
  • 27
  • 185
  • 256
Adnan
  • 20,769
  • 1
  • 16
  • 12
186

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

Eliran Malka
  • 504
  • 7
  • 15
Rana
  • 1,961
  • 1
  • 6
  • 3
  • 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! – NobleUplift Aug 14 '19 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.

Ura
  • 1,031
  • 1
  • 11
  • 20
  • 15
    You can also call `source /etc/bashrc` if you want to add it to your repertoire while running. – cwallenpoole Feb 11 '15 at 07: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 Apr 24 '18 at 01:23
38

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

  • By recursively you mean it lists all files in subdirectories, doesn't ls already have a switch to do that? – jiggunjer May 14 '15 at 16:28
  • @jiggunjer `ls -Rltr` will sort by dir, then by dates, `find -type f -mmin -5 -exec ls -ltr {} +` will just print files modified in last 5 minutes, sorted by date, regardless of directory tree! – F. Hauri - Give Up GitHub Dec 07 '16 at 18:08
  • Note that this won't work if the list of files is too long to be passed as one shell invocation to `ls` (https://unix.stackexchange.com/a/118200/27186) – then you'll see one sorted bunch of files, then another sorted bunch of files, etc. but the whole list won't be sorted. See https://superuser.com/questions/294161/unix-linux-find-and-sort-by-date-modified for sorting longer lists with find. – unhammer Sep 11 '19 at 07:21
  • @unhammer You're right, for this to work safely, see [my recent anser to Unix/Linux find and sort by date modified](https://superuser.com/a/1481352/178656) – F. Hauri - Give Up GitHub Sep 11 '19 at 08:25
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)

simhumileco
  • 667
  • 7
  • 14
24

Add:

alias lt='ls -Alhtr'

in $homedir/.bashrc

slhck
  • 223,558
  • 70
  • 607
  • 592
Layer8
  • 241
  • 2
  • 2
18

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

find / -ctime 3
Peter Mortensen
  • 12,090
  • 23
  • 70
  • 90
pbies
  • 2,757
  • 3
  • 21
  • 23
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
  • 1
    it giv`ls -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 Jun 27 '18 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 Feb 28 '20 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.

Peter Mortensen
  • 12,090
  • 23
  • 70
  • 90
bbarker
  • 316
  • 3
  • 9
  • 6
    I suspect your answer hasn't gotten any up-votes because it parses the output of ls - see [the canonical argument against doing so](http://mywiki.wooledge.org/ParsingLs) and [this question about not parsing ls](http://unix.stackexchange.com/questions/128985/why-not-parse-ls) – Eponymous Dec 15 '14 at 22:32
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)...