Unix Shell - Finding and copying the last modified file in a directory

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > October 2006 > Finding and copying the last modified file in a directory





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Finding and copying the last modified file in a directory
mark.pollard@cwfi.co.fk

2006-10-24, 1:17 pm

I am hoping to be able to find the last modified file in a directory
and copy it to another directory under a different name. I have so far
been trying to find a way to use the 'find' command followed by the
'-exec'. Unfortuately I cannot find an option that finds the last
modified file.

I would be grateful to hear any suggestions.

Scott McMillan

2006-10-24, 7:17 pm

On 24 Oct 2006 10:01:49 -0700, mark.pollard@cwfi.co.fk wrote:

>I am hoping to be able to find the last modified file in a directory
>and copy it to another directory under a different name. I have so far
>been trying to find a way to use the 'find' command followed by the
>'-exec'. Unfortuately I cannot find an option that finds the last
>modified file.
>
>I would be grateful to hear any suggestions.


If subdirectories aren't a concern, I would probably do something
like:

ls -1at /directory_in_question | while read a
do
[ -f /directory_in_question/"$a" ] && {
cp -p /directory_in_question/"$a" /wherever/whatever
exit 0
}
done

Note the numeral one, not an "el" in ls -1at.


Scott McMillan
Chris Mattern

2006-10-24, 7:17 pm

In article <1161709309.276545.280600@m7g2000cwm.googlegroups.com>,
mark.pollard@cwfi.co.fk wrote:
>I am hoping to be able to find the last modified file in a directory
>and copy it to another directory under a different name. I have so far
>been trying to find a way to use the 'find' command followed by the
>'-exec'. Unfortuately I cannot find an option that finds the last
>modified file.
>
>I would be grateful to hear any suggestions.
>


cp "$(ls -tr | tail -1)" /destination/dir

Breaks if the most recent file has double quotes in its name.

--
Christopher Mattern

"Which one you figure tracked us?"
"The ugly one, sir."
"...Could you be more specific?"
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com