Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

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

You must log in to answer this question.