LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 01-14-2005, 11:14 AM   #1
Transition
Member
 
Registered: Aug 2003
Distribution: Slackware 10
Posts: 48

Rep: Reputation: 15
Copying the newest file in a directory to another location


Hi all,

I'm trying to get a script to look at a directory and copy the newest file in that directory to another location. I only want the script to copy the newest file and nothing else. I'm googling like crazy but haven't come up on any leads.

Any help?
 
Old 01-14-2005, 11:26 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
Hi,

This works:

cp `ls -latr | grep '^-' | tail -1 | awk '{ print $NF }'` /tmp

It also takes in account the existence of none-file entries (dirs, links etc), they are ignored. 'dot' files are not ignored.

If there are only regular files present in a directory, it's simpeler:

cp `ls -1tr | tail -1` /tmp

Hope this helps.
 
Old 01-14-2005, 11:53 AM   #3
Transition
Member
 
Registered: Aug 2003
Distribution: Slackware 10
Posts: 48

Original Poster
Rep: Reputation: 15


It's so beautiful! Thank you!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Find newest file capella Linux - Software 9 04-28-2012 04:26 AM
Automatically Copying files from the ftp directory into the html directory swatward Linux - General 3 04-17-2005 10:55 PM
Copying a file into a directory Nizhni Linux - Newbie 3 01-24-2005 12:47 AM
Copying etc directory njschroe Suse/Novell 6 12-15-2004 01:45 AM
copying a CD to a directory MrGardenHoseMan Linux - General 2 07-03-2003 06:51 PM


All times are GMT -5. The time now is 08:37 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration