Take the 2-minute tour ×

Trying to solve this problem I've come up with solutions that are not compatible between ubuntu and osx. I wish to make one replacement command for the following two, which differ in two characters:




find . -type f -exec stat -c "%Y {}" {} \;| sort -n | tail -n 1 | cut -d ' ' -f 2
share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

The stat command is not part of POSIX, so systems are allowed to have differing implementations.

Assuming Perl is available, the following should work:

-exec perl -MFile::stat -e 'print stat($_)->mtime, " $_\n" for @ARGV' {} \;

-exec perl -e 'print ((stat($_))[9], " $_\n") for @ARGV' {} \;

The version with -MFile::stat is more readable, but unfortunately several times slower.

The complete script would look like:

find . -type f -exec perl -e 'print ((stat($_))[9], " $_\n") for @ARGV' {} + |
  sort -n | tail -n 1 | cut -d " " -f 2-

(note the 2- at the end)

share|improve this answer
Had to change \t to a space to fit the example, but the rest works great on both systems. Thanks! –  audio.zoom May 22 '12 at 19:22
@audio.zoom: I used \t intentionally since file names very often have spaces and your cut -d ' ' -f 2 would have discarded everything after the first space. –  grawity May 22 '12 at 20:56
@audio.zoom: Also, does OS X find support using -exec ... {} + instead of -exec ... {} \;? The + form would be several times faster. –  grawity May 22 '12 at 20:57
Yes osx, at least 10.6, supports -exec + ...and thanks for catching that space bug! –  audio.zoom May 23 '12 at 9:24
grawity, I'm wondering if you could update your script for using the + notation because I'm not sure if the current script will handle spaces correctly with it anymore –  audio.zoom May 25 '12 at 10:42
show 1 more comment

Your Answer


