-   Programming (
-   -   Bash script - how to use output from diff and find in context with cpio (

Mogget 01-23-2009 01:15 AM

Bash script - how to use output from diff and find in context with cpio

What i want to do is use the output from find, diff and then pipe it to cpio. Somewhat like this


find . -print > homefolderMonday
find . -print > homefolderTuesday
diff homefolderMonday homefolderTuesday | cpio --create --format=newc > /mnt/backup/backup.cpio

Problem is that the output from diff can't be piped directly since the output contains other things in addition to the folder and file changes.


> ./.kde/share/apps/kopete/msnpictures/someuserblabla-com.png

So how do i remove the "2684a2685" and ">" part from diff output? I'm also open for other ways to do this so if you have sugestions tell me.

I've made a basic backup utility that backups every users /home folders to a specified mounted partition. It works somewhat like this. PS, this is not the complete code. I can provide it if necesary.



# What user should be backed up?
# Users home directory
# Where to backup to
# What date is it today?
DATE=`date -u +%m%d%y`
# Create users directory in backup directory?
# Create todays directory in users backup directory?
# If 0 the backup process will be aborted after directory checking

if [ -d $BACKUPDIR/$USER ]; then
    echo "$BACKUPDIR/$USER exist."
    echo "Creating $BACKUPDIR/$USER."
    mkdir $BACKUPDIR/$USER

if [ -d $BACKUPDIR/$USER/$DATE.bak
    echo "Backup already been done, exiting backup script."
    echo "Creating $BACKUPDIR/$USER/$DATE.bak."
    mkdir $BACKUPDIR/$USER/$DATE.bak

if [ $DOBACKUP = "0" ]; then
    cd $USERHOME
    echo "Making a list of $USERHOME."
    find . -print > $BACKUPDIR/$USER/$DATE.bak/$DATE.list
    echo "Using $BACKUPDIR/$USER/$DATE.bak/$DATE.list to backup with cpio."
    cat $BACKUPDIR/$USER/$DATE.bak/$DATE.list | cpio --create --format=newc > $BACKUPDIR/$USER/$DATE.bak/$DATE-backup.cpio
    echo "Updating last update date in $BACKUPDIR/$USER/.last-backup."
    date -u +%m%d%y > $BACKUPDIR/$USER/.last-backup
    echo "Done backing up for today."

The point of $BACKUPDIR/$USER/.last-backup is so that the script can retrieve the last backup list made so we can diff todays list and last backups list and run it through cpio

Any help and pointers will be gladly taken sorounding different ways to do this.

Thanks for the help in advance.

David the H. 01-23-2009 05:48 AM

You can pipe the output through egrep, using a regex to match only the filenames. Something like:


diff homefolderMonday homefolderTuesday | egrep -o "\./.+$" | cpio --create --format=newc > /mnt/backup/backup.cpio
This assumes the file names all start with ./ and aren't followed by anything else on the line.

Edit: It seems that "diff -n" will print the lines without the leading > character, so you could make the grep expression even simpler; something like egrep -o "^\." (matches any line starting with a period) would probably be all you need, to filter out non-file lines.

gnashley 01-23-2009 08:19 AM

I think diff is simply the wrong tool for the job. Either comm or cmp would probably be more suited, but there are surely still better ways to compare the contents of two directories, such as dircomp:

theNbomr 01-23-2009 09:16 AM

diff, the way you are trying to use it, is going to show you files that have been removed, and these are going to be difficult to backup. find, on the other hand, should be able to directly report those files which are new or modified in the most recent minutes or days:

find . -ctime -1 -print
prints the full filespec of all files created or modified in the last day.

--- rod.

Mogget 01-23-2009 11:38 AM

Thank you for the suggestions.
I knew that diff would output folders that has been removed to so that's a blunder from my side.

I'll look closer into the find -ctime function since that would make my script easier to work with. Thank you for your time.

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