6

The linux locate command is great at finding files quicky.

My question: how can we use the locate command so that the results are returned in date/time order?

2 Answers 2

7

This works as long as there are not spaces in the filenames, but errors if there are too many files (see http://www.gnu.org/software/coreutils/faq/#Argument-list-too-long):

$ ls -td $(locate sh)
bash: /bin/ls: Argument list too long

This will work even with spaces or other characters in filenames, but doesn't sort correctly with too many files:

locate something -0 | xargs -0 ls -ltd

The following will always work (although it might take awhile):

locate something -0 | xargs -0 stat -c'%Y %n' | sort -n
2
  • Piping to stat was ten times faster than piping to ls for me, although it doesn't show all the ls -l information, obviously.
    – Sparhawk
    Oct 6, 2016 at 4:17
  • to show modification time in human readable format (works as long as there is no # character in the filename: locate SomeSearchTerm -0 | xargs -0 stat -c'%Y#%y %n' | sort -n | cut -d"#" -f2 | sed "s/\.[0-9]*//"
    – qwertz
    Jul 30, 2022 at 20:11
3

How about:

ls -td $(locate something)

or

ls -td1 $(locate something)
3
  • Maybe a -d would be handy, too.
    – choroba
    Aug 27, 2012 at 11:47
  • @choroba: good point, I'll update the answer. Aug 27, 2012 at 11:49
  • 1
    This fails for files with spaces in their name.
    – Sparhawk
    Oct 6, 2016 at 4:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .