Search and Copy Files

Hi All

Need your help, I am looking for a script to search for files with specific extension as .log and then copy the latest one to a different folder.

Here is the scenario

/dev/abc/xyz/a_2_122920131.log
/dev/abc/xyz/a_2_123020131.log
/dev/abc/xyz/b_2_12302013.log
/dev/abc/xyz/b_2_12312013.log
/dev/abc/xyz/c_2_12312013.log

I want to search for all such files for all folders under home and then copy the latest one for each one of them

So for above scenario I should be able to copy follwoin

/dev/abc/xyz/copy/a_2_12302013.log
/dev/abc/xyz/copy/b_2_12312013.log
/dev/abc/xyz/copy/c_2_12312013.log

Any inputs will help

Thanks...

Once you get to the directory where there are .log files, you can do

file=`ls -tl *.log| head -1|sed "s/.* //"`
cp $file copy

That creates a list of all ".log" files, latest first.

The above assumes (probably incorrectly) there are no directories under the one you are currently in. In that case another sed command immediately follow the "ls -tl" command to remove all lines starting with total or the letter d.

A driver script would start the above code starting with "find .. -name '*.log', redirecting its output (full path to each log file). Lines containing "/copy/" would first need to be taken out. A dirname or other utility would be run on the resulting file to leave only the path to each file and it would then be sorted uniquely to provide a list of all directories where the script described in the first paragraph would be run.

Also, the code in the first paragraph should probably start with "cd $1" with each directory where this is to be done since this parameter will be provided by your driver script.

I'm not sure if there should be ANY regular files in the /dev directory...
And, how can /dev/abc/xyz/a_2_123020131.log result in a copy /dev/abc/xyz/copy/a_2_12302013.log ?

Latest compared by files' time stamps or file name elements? Can there be duplicate file names across directories, and what to do in such case?