The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Need Help with -newer command bbbngowc Shell Programming and Scripting 1 01-12-2008 05:02 AM
To copy the files newer than specific date RAJESHKANNA SUN Solaris 3 08-07-2007 12:44 AM
cp work as xcopy. ajayyadavmca Shell Programming and Scripting 6 12-15-2006 01:57 AM
Find files newer than... vertigo23 UNIX for Dummies Questions & Answers 4 09-30-2005 11:47 AM
tar --newer = tar --newer-mtime ? jamesbond UNIX for Dummies Questions & Answers 1 10-16-2001 11:24 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 12-01-2005
Registered User
 

Join Date: Dec 2005
Posts: 3
copy only newer files? (xcopy equivalent)

Howdy folks.

I have a problem - I'm sure the answer is very simple, but I can't work it out.

I want to create a UNIX shell script that does what I've been doing in DOS batch files for years - that is, backing up files. By which I mean copying files from a source directory to a target directory, only if a) the file doesn't exist at the target, or b) the file does exist but is older than the source.

In DOS, I did something like this:

xcopy c:\path\directory\*.* x:\backup\ /d /e

Where x was a networked drive, /d meaning only copy files newer than the target, /e meaning recurse into subdirectories.

In UNIX, I'm close but no cigar yet... I have the following:

cp -r /testdir/source/ /testdir/target/

This works in that it copies files, leaving the originals behind and recursing into subdirectories, but it doesn't only copy source files if they're newer than the target. It copies eveything.

I have read in a few different places that cp accepts the -u ('update' I think) option, to make it only copy newer files, but I can't get this to work. If I write cp -u, I am told that 'u' is an 'illegal option' for cp.

So, is there a way to get the behviour I'm after? Am I right to be using cp, or is there a better function to do what I want?

Doing all this on Mac OS 10.3.9, using the terminal, tcsh.

Very grateful for any help.

Cheers.
Reply With Quote
Forum Sponsor
  #2  
Old 12-01-2005
Registered User
 

Join Date: Jun 2005
Location: Ireland
Posts: 61
gnu copy (as is on linux) has the -u option.
Also have a look at rsync
Reply With Quote
  #3  
Old 12-01-2005
linuxpenguin's Avatar
Registered User
 

Join Date: May 2002
Location: India
Posts: 295
hmm tcsh, tough, i didnt ever use it.
If you want to go with ksh tho, i ll use something like this
=================
tgt is the target dir

for i in *
do
newf=`find . -name $i -newer "../tgt/$i"`
if [ "x$newf" != "x" ]
then
cp $newf ../tgt/$i
fi
done

hope this gives you some pointers
__________________
War doesnt determine who is right, it determines who is left
Reply With Quote
  #4  
Old 12-02-2005
Registered User
 

Join Date: Dec 2005
Posts: 3
Thanks to both.

rsync seems to do the job, but I'm still having some issues, such as certain files not being copied - I get this:

mkstemp "path/filename.jNLmL7" failed: Invalid argument

...which is double dutch to me. Any ideas about this?

The files that do copy seem to lose their application association too, which is not a deal breaker, but is a bit of a PITA. I suspect this is something to do with me moving files around behind the Finder's back. I might chase this bit up on the Apple forums.

Cheers!
Reply With Quote
  #5  
Old 12-02-2005
Registered User
 

Join Date: Dec 2005
Posts: 3
Aha - After Googling to find out what mkstemp is, I've discovered that the error is due to the file name length. Shortening the file name has solved the problem - the temp file name with the .XXXXXX extension must have been pushing it over the OS's file name length limit. (I guess)
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 11:59 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0