LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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


Reply
  Search this Thread
Old 03-05-2013, 08:50 PM   #1
said76
Member
 
Registered: Aug 2011
Posts: 113

Rep: Reputation: Disabled
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
 
Old 03-05-2013, 09:41 PM   #2
slakster
LQ Newbie
 
Registered: Jan 2008
Location: Riverside IL
Distribution: Ubuntu/CentOS/Arduino
Posts: 7

Rep: Reputation: 2
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.
Old 03-05-2013, 10:26 PM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,356

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
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.
Old 03-05-2013, 11:24 PM   #4
lleb
Senior Member
 
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora/Pop!_OS
Posts: 2,983

Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
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?
 
Old 03-06-2013, 12:45 AM   #5
said76
Member
 
Registered: Aug 2011
Posts: 113

Original Poster
Rep: Reputation: Disabled
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 View Post
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.
 
Old 03-06-2013, 01:06 AM   #6
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
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.
 
Old 03-06-2013, 03:06 AM   #7
mandyapenguin
Member
 
Registered: Nov 2011
Location: India
Distribution: RedHat, Cent OS, Fedora, Debian, Ubuntu
Posts: 106

Rep: Reputation: Disabled
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
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] How do I copy root the file system of an internal hdd to a usb hdd completelinuxnube Slackware 8 08-06-2012 02:22 PM
How to learn Linux using external portable HDD stenir Linux - Newbie 10 11-01-2009 09:33 PM
how to copy data from system to external hdd gsnravi Linux - Newbie 4 02-28-2009 03:23 AM
portable external hdd installed openSUSE 11.1 hardware autodetection alexmann13 Linux - Hardware 1 01-27-2009 08:34 AM
Making Linux portable on External HDD sdexp Linux - Newbie 1 10-12-2007 09:47 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:58 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration