1775

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

Garrett
  • 286
  • 3
  • 9

10 Answers10

2138
ls -t

or (for reverse, most recent at bottom):

ls -tr

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

Adnan
  • 21,489
190

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

hirana
  • 2,001
57

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,051
42

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

or

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

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 -gtr {} +

... and even for not limiting to files:

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

(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

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
  • 697
  • 1
  • 7
  • 14
24

Add:

alias lt='ls -Alhtr'

in $homedir/.bashrc

slhck
  • 235,242
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
pbies
  • 3,550
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
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.

bbarker
  • 336
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)...