Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place! |
| Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
03-05-2013, 08:50 PM
|
#1
|
|
Member
Registered: Aug 2011
Posts: 83
Rep: 
|
find latest file and copy it to external portable HDD
Hi,
I'm trying to back up some files to my external hard drive. My question is if I want to find the latest modified file and copy it to my external hard drive on my Ubuntu 12.04.
I have tried to following command on terminal but with no luck:
$ cp 'ls -tr | tail -1' /media/myHDD1/backup
Any help would be very much appreciated.
Thank you very much in advance
|
|
|
|
03-05-2013, 09:41 PM
|
#2
|
|
LQ Newbie
Registered: Jan 2008
Location: Durham NC
Distribution: Mandriva/Knoppix/Fedora
Posts: 1
Rep:
|
try this change
try this modification of your cmd line:
cp $(ls -rt |tail -1 ) > /media/HDD/backup
most folks use a variation of this to find all recently changed files in the current dir:
find . -mtime +1 -exec cp {} /media/HDD/backup \;
|
|
|
1 members found this post helpful.
|
03-05-2013, 10:26 PM
|
#3
|
|
Guru
Registered: Aug 2004
Location: Brisbane
Distribution: Centos 6.4, Centos 5.9
Posts: 14,980
|
The above should work. The mistake in OP was using single quotes (') instead of backquotes (`).
Actually that's not very clear (at least in my fonts); lets try code tags
Code:
single (')
back (`)
A bit clearer imho.
Last edited by chrism01; 03-05-2013 at 10:27 PM.
|
|
|
1 members found this post helpful.
|
03-05-2013, 11:24 PM
|
#4
|
|
Senior Member
Registered: Dec 2005
Posts: 1,369
Rep: 
|
a little off topic here, but instead of using cp, he could do the following with rsync yes:
Code:
find . -mtime +1 -exec rsync -aviS '{}' /media/myHDD1/backup \;
or
Code:
rsync -aviS `ls -tr | tail -1` /media/myHDD1/backup
will rsync function the same way as cp?
|
|
|
|
03-06-2013, 12:45 AM
|
#5
|
|
Member
Registered: Aug 2011
Posts: 83
Original Poster
Rep: 
|
Yes, it works fine. Thank you for letting me know that I should use (`) instead of ('). Obviously, I did not pay attention closely enough. Thank you once again.
And thank you everyone for their comments as well.
Quote:
Originally Posted by chrism01
The above should work. The mistake in OP was using single quotes (') instead of backquotes (`).
Actually that's not very clear (at least in my fonts); lets try code tags
Code:
single (')
back (`)
A bit clearer imho.
|
|
|
|
|
03-06-2013, 01:06 AM
|
#6
|
|
Senior Member
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,615
|
For latest modified files, use -1 instead of +1 with find cmd:
Code:
~$ find . -mtime -1 -print
~$ find . -mtime -1 -exec rsync -aviS '{}' /media/myHDD1/backup \;
-1 option will list out files modified within last 1 day, whereas +1 option will list out files modified before 1 day.
|
|
|
|
03-06-2013, 03:06 AM
|
#7
|
|
Member
Registered: Nov 2011
Location: India
Distribution: RedHat, Cent OS, Fedora, Debian, Ubuntu
Posts: 96
Rep: 
|
You can use rsync. Rsync is a fast and extraordinarily versatile file copying tool. It can copy locally, to/from another host. Rsync finds files that need to be transferred using a "quick check" algorithm (by default) that looks for files that have changed in size or in last-modified time.
Example usages are:
Code:
rsync -avz --progress /source/directory /destination/directory/
When you run the same command again, it sends only those that changes are made in the source.
if you wanted logs of rsync you can try
Code:
rsync -avz --progress --log-file=/tmp/rsynclog /source/directory /destination/directory/ && echo && cat /tmp/rsynclog
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 08:18 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|