Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to copy the latest file

Status
Not open for further replies.

andshr

Programmer
Mar 16, 2006
10
0
0
US
Hi:
I have a simple task - to copy, in Linux, the latest xml from one folder to another folder (or ftp to another server). Can't find the command to pick up the latest file. Can UNIX gurus help poor Windows guy?
 
cp $(ls -lt /source/directory | head -1) /destination/directory

Lists the source directory in descending date order, chops off the first file in the list, and inserts it into the cp command line.

In Unix they are not called folders. [smarty]

Annihilannic.
 
Oops:

[tt]cp $(ls -t /source/directory | head -1) /destination/directory[/tt]

Stuck in the 'l' by mistake, force of habit.


Annihilannic.
 
Thanks a lot. It does find the latest file, great! But complains that: cannot stat 'filenametocopy': No such file or directory. Funny, it does find filenametocopy right, maybe it's smth with single quotation marks?
 
Sorry, I should have thought of that, try:

[tt]cp $(ls -t /source/directory/* | head -1) /destination/directory[/tt]

That way the ls command displays the filenames with full paths. Alternatively if there are (or may be) many files in that directory it may be safer to do this:

[tt]cd /source/directory
cp $(ls -t | head -1) /destination/directory[/tt]


Annihilannic.
 
Thank a lot for your help, that worked nicely!
Andshr
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top