Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to use find and ls to sort files based on date, however, it's not doing anything. Here's my command:

find ./.BUBackups -name "test*" -exec ls -t {} \;

I also tried:

find ./.BUBackups -name "test*" -exec ls -lt {} \;

In testing, -t is simply doing -nothing-. -l works, but if I input -t it acts as if I didn't put it in at all. However, ls -t or ls -lt works perfectly fine when I'm not running it through exec, in others words simply typing ls -t works

Is this a glitch? or am I doing something wrong?

share|improve this question
up vote 3 down vote accepted

The -exec argument to find executes the specified command once for each file that find locates. So if you have files test_a, test_b, and test_c, your command will be equivalent to:

ls -lt test_a
ls -lt test_b
ls -lt test_c

In other words, each listing is performed independently per file, so the files will be listed in whatever order find locates them, rather than in order of modification time, like the -t option to ls specifies.

What you really want is something like find ./.BUBackups -name 'test*' | xargs ls -lt, which, with the example files I used above, would be equivalent to ls -lt test_a test_b test_c. Since you're now using one invocation of ls to list all of the files you're interested in, it can put them in the proper mtime order.

share|improve this answer
This is exactly what I needed! Thanks (to both of you) – Wipqozn Feb 11 '11 at 18:56

The reason -t isn't working is that find is executing ls once for each found file separately so the output is in the order the files are found. To make it work the way you expect:

find ./.BUBackups -name "test*" -exec ls -lt {} +


find ./.BUBackups -name "test*" -print0 | xargs -0 -exec ls -lt 
share|improve this answer
Does ls is guaranteed to be called just one time? Otherwise I guess you'll get multiple lists of sorted files. – cYrus Feb 11 '11 at 18:28
@cYrus: No, not guaranteed. It will be limited to the maximum argument length supported by your system. If you're using ls -lt with that many files, you should probably be using a different approach altogether. – Dennis Williamson Feb 11 '11 at 18:36
This is exactly what I needed! Thanks (to both of you) – Wipqozn Feb 11 '11 at 18:56

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.