Sure thing!
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:
Code:
# we want to syncronize this file: /source/my/dir/testfile to /dest/my/dir/testfile
# so we'll change the script to look like this:
for file in `find /source/* -mtime 0 -name '*' -type f`; do
echo $file
cp $file `echo $file | sed 's/source/dest/'`
done;
The output of this program will be:
Code:
/source/my/dir/testfile
indicating that it copied that file (though it doesn't indicate where to) -- this behavior can easily be modified.
The end result is now you have a copy of the /source/my/dir/testfile in /dest/my/dir/.
So, I'll modify this script to be a little more verbose and a little more parameterized...
Code:
#!/bin/bash
#
# Script to synchronize files modified today from $SRC to $DEST as
# specified by the first and second command-line parameters
SRC = $1
DEST = $2
echo Synchronizing $SRC to $DEST...
for file in `find $SRC/* -mtime 0 -name '*' -type f`; do
dfile = `echo $file | sed 's/$SRC/$DEST/'`
echo copying $file to $dfile
cp $file $dfile
done;
echo Synchronization complete.
Now you can call the program thusly (saved as bsync.sh) using the previous example:
Code:
brion> sh bsync.sh /source /dest
Synchronizing /source to /dest...
copying /source/my/dir/testfile to /dest/my/dir/testfile
Synchronization complete.
brion>
sed is a Stream EDitor that permits you to, among other things, substitute text in a stream with other text -- in this case, the $SRC with the $DEST directory names in the source file path.
Does that help?