Take the 2-minute tour ×

I am using a command

find ../../ -type f -name <filename>.PDF -print0 | xargs -0 cp --target-dirctory=<path>;

the problem is it doesn't copy all files but only one file. I want to find a file abc.pdf which is present on different folders. I need the latest updated file from all these folders. How can I find it and copy to the target folder?

share|improve this question

migrated from stackoverflow.com Apr 23 '13 at 13:37

This question came from our site for professional and enthusiast programmers.

This is copying all the files, one after the other - but they all have the same name, so later copies overwrite earlier ones. Your command is basically doing cp dir1/abc.pdf target/ ; cp dir2/abc.pdf target/ - only the last copy will remain. –  Gabe Apr 26 '13 at 8:20

1 Answer 1

find /DIR -type f -name "WHATEVER.PDF" -exec stat -f "%m %N" {} \; | \
    sort -n | \
    tail -1 | \
    cut -f2- -d" " 

Gives you the latest WHATEVER.PDF (and it's path, via the stat output sorted and filtered). I'm leaving the copying part to you.

share|improve this answer
Thanks, Ok I want to copy all searched file in target directory. How can I do it? And which directory this file is storing? –  lalit Apr 23 '13 at 13:23
In this command I was using a variable file. But I forgot where was i using it. Actually I created some script for finding files but it was deleted by me via rm -rf command and it is unable to recover.find ../../ -type f -name <filename>.PDF -print0 | xargs -0 cp --target-dirctory=<path>; –  lalit Apr 23 '13 at 13:26

Your Answer

