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

1 422

251

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

nearly_lunchtime

Posted 2009-04-09T13:17:20.547

Reputation:

1

Related (not necessarily a duplicate): Unix/Linux find and sort by date modified

– Peter Mortensen – 2016-12-27T14:15:52.443

Answers

1 687

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

Posted 2009-04-09T13:17:20.547

Reputation: 16 971

7

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 – 2015-10-27T12:09:03.170

7ls -llt for showing date-timestamp along with sorting – Harshul Pandav – 2016-09-26T20:01:14.890

1Is it possible to just show file name & date for each? – android developer – 2016-10-14T11:28:46.290

4Yes it is possible – Ashraf.Shk786 – 2017-02-20T13:09:57.277

Note that this argument is also applicable to ll. – Epoc – 2017-03-07T14:20:12.333

7

@EvgeniSergeev DONT MEMORISE ls -halt a simple mistype may cause your server to crash! https://linux.die.net/man/8/halt

– Isaac – 2017-07-09T21:43:03.643

1@Isaac: for a regular user halt is not gonna work without sudo unless explicitly configured. – ccpizza – 2019-09-02T15:19:11.333

317ls -halt is for human readable, show hidden, print details, sort by date. – Evgeni Sergeev – 2013-10-01T05:24:17.100

155

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

Rana

Posted 2009-04-09T13:17:20.547

Reputation: 1 651

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 – 2019-08-14T22:07:44.087

48

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.

Dejan

Posted 2009-04-09T13:17:20.547

Reputation: 914

14You can also call source /etc/bashrc if you want to add it to your repertoire while running. – cwallenpoole – 2015-02-11T07:57:16.993

1You can also add it in ~/.bash_aliases just for your user (one can create the file if it doesn't exist already – Dinei – 2018-04-24T01:23:10.070

31

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 about which files was 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 anser to find and sort by date modified

F. Hauri

Posted 2009-04-09T13:17:20.547

Reputation: 1 394

By recursively you mean it lists all files in subdirectories, doesn't ls already have a switch to do that? – jiggunjer – 2015-05-14T16:28:06.413

@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 – 2016-12-07T18:08:06.967

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 – 2019-09-11T07:21:41.713

@unhammer You're right, for this to work safely, see my recent anser to Unix/Linux find and sort by date modified

– F. Hauri – 2019-09-11T08:25:30.310

22

Add:

alias lt='ls -Alhtr'

in $homedir/.bashrc

Layer8

Posted 2009-04-09T13:17:20.547

Reputation: 221

18

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

Posted 2009-04-09T13:17:20.547

Reputation: 431

16

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

find / -ctime 3

pbies

Posted 2009-04-09T13:17:20.547

Reputation: 1 633

12

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

lfender6445

Posted 2009-04-09T13:17:20.547

Reputation: 311

1it 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 – 2018-06-27T16:09:53.103

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 – 2020-02-28T11:15:37.360

9

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

Posted 2009-04-09T13:17:20.547

Reputation: 276

5

I suspect your answer hasn't gotten any up-votes because it parses the output of ls - see the canonical argument against doing so and this question about not parsing ls

– Eponymous – 2014-12-15T22:32:37.977

-1

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)...

Aleksandar Pavić

Posted 2009-04-09T13:17:20.547

Reputation: 319