I've got a script that will get all the files modified today and I want to copy them to my webserver which has the same directory structure. The only problem I'm having it cp isn't copying it to the directory, its just copying to the root and if I use -r it copies the entire directory. Here's my script so far

That's a compound line - it's running two commands at once through use of the backticks "`".

Like order of operations - the script will execute what's in the backticks first, and then send its output to the containing statement.

So first it executes

Code:

echo $file | sed 's/DIR1/DIR2/'

which is a non-elegant way to substitute all the occurances of "DIR1" in the $file (path) with "DIR2". In this case, DIR1 and DIR2 would be replaced with your source and target root folders.

The output of that statement is the modified file path to point to DIR2/path instead of DIR1/path, so the subsequent statement becomes:

Code:

cp DIR1/path DIR2/path

The end result is that a file from the source directory that was found by your 'find' command will be copied to the target directory in the same location. Here's an example of a "real" file as it's transformed by this script:

I found a couple of issues. For some reason the $dfile ends up the same as $file so obviously, it doesn't get copied. The other issue I found is if the file name has a space in it. I'll probably just rename those files as file names should probably not have a space in them.

You're right - it's got all sorts of problems which frankly I don't quite understand. However, I'm pretty sure if you hardcode the source and dest directories you can get it to work (instead of using variables).

Also in sed, to use a '/' character, you need to escape it with '\' such as:

Code:

sed 's/\/source/\/dest/'

Another thing wrong with my second script is the SRC and DEST need to remove the spaces

Code:

SRC=$1DEST=$2

As I get time I'll debug the rest - maybe Crouse or jbsnake will pop by and correct my syntax.

Daily, this backs up ONLY the CHANGED files from certain directories.... notice the --exclude stuff.
On initial run... it backs up EVERYTHING ....then it only "syncs" the new data from there on out..... very cool, and when used in conjunction with cron, makes for a hands free painless backup setup.

I guess was just hoping for some pointers as to why my script didn't work - rsync aside. I'm not completely familiar with sed yet and from a theoretical standpoint I don't see why this script doesn't at least copy the files it finds.

Consider it practice in script writing - just like writing a compiler to learn how one works, it's not like the world needs another compiler.

Last edited by BrionS on Wed Aug 23, 2006 1:41 pm, edited 1 time in total.

the reason I didn't want to use rsync is somebody in a previous post said that rsync doesn't work well syncing two windows boxes (can somebody confirm this?). Another reason is that this isn't a backup script, this is an upload script to my webserver. This lets me quickly get the files I've changed today and upload them.

Who is online

Users browsing this forum: No registered users and 1 guest

You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forumYou cannot post attachments in this forum