copy only newer files? (xcopy equivalent) | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


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

copy only newer files? (xcopy equivalent)

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 12-01-2005
Chomps Chomps is offline
Registered User
 
Join Date: Dec 2005
Last Activity: 21 January 2006, 9:20 PM EST
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
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.
Sponsored Links
    #2  
Old 12-01-2005
pixelbeat pixelbeat is offline
Registered User
 
Join Date: Jun 2005
Last Activity: 12 January 2006, 7:33 AM EST
Location: Ireland
Posts: 61
Thanks: 0
Thanked 0 Times in 0 Posts
gnu copy (as is on linux) has the -u option.
Also have a look at rsync
Sponsored Links
    #3  
Old 12-01-2005
linuxpenguin's Avatar
linuxpenguin linuxpenguin is offline Forum Advisor  
Registered User
 
Join Date: May 2002
Last Activity: 28 March 2014, 4:09 PM EDT
Location: India
Posts: 350
Thanks: 0
Thanked 5 Times in 5 Posts
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
    #4  
Old 12-02-2005
Chomps Chomps is offline
Registered User
 
Join Date: Dec 2005
Last Activity: 21 January 2006, 9:20 PM EST
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
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!
Sponsored Links
    #5  
Old 12-02-2005
Chomps Chomps is offline
Registered User
 
Join Date: Dec 2005
Last Activity: 21 January 2006, 9:20 PM EST
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
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)
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
ls files newer than 6 hours locoroco Shell Programming and Scripting 2 03-30-2011 02:53 PM
Find files newer than X that contain string ABC Adamm Shell Programming and Scripting 2 03-24-2011 06:47 AM
Copy newer files without create mrxrsd Shell Programming and Scripting 2 06-09-2009 10:55 AM
To copy the files newer than specific date RAJESHKANNA Solaris 3 08-07-2007 03:44 AM
Find files newer than... vertigo23 UNIX for Dummies Questions & Answers 4 09-30-2005 02:47 PM



All times are GMT -4. The time now is 05:12 PM.