Page 1 of 2 12 LastLast
Results 1 to 10 of 14

Thread: rsync --delete with confirmation?

  1. #1
    Join Date
    Dec 2010
    Beans
    31
    Distro
    Ubuntu 10.10 Maverick Meerkat

    rsync --delete with confirmation?

    I want to synchronize sets of files (e.g. from or to flash memory). rsync is powerful, but --delete option is dangerous.

    Anyone know whether there's a way to do --delete interactively, i.e. get rsync or some near equivalent to ask (y/n, in a console window) before deleting?
    Last edited by EntropyReduction; February 22nd, 2011 at 05:58 PM.

  2. #2
    Join Date
    Jul 2006
    Location
    Los Angeles
    Beans
    1,310
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: rsync --delete with confirmation?

    Code:
    man rsync
    doesn't show an interactive delete option. But what you can do is use a -n option, for a simulated run, and a -v option for verbose output, and then pipe that output to a file. eg
    Code:
    rsync -avn source target 1>rsync-dryrun.txt 2>rsync-dryrun-errors.txt
    That way you could check whether it plans on deleting too much.

  3. #3
    Join Date
    Feb 2010
    Location
    Silicon Valley
    Beans
    1,898
    Distro
    Xubuntu 12.04 Precise Pangolin

    Re: rsync --delete with confirmation?

    I usually throw in a "--max-delete=20" (or some other reasonable value) to limit potential damage.

  4. #4
    Join Date
    Dec 2010
    Beans
    31
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: rsync --delete with confirmation?

    I'm working on this: work in progress....anyone more expert on bash scripting thn me (which would be just about everyone, comments/changes welcome
    Code:
    #!/bin/bash
    
    function processFilesInFolders
    {
    #$1 is source $2 is destination 
    
    #bit of sanity checking
    
    ARGS=2 # Number of arguments expected.
    E_BADARGS=85 # Exit value if incorrect args passed.
    
     if [ $# -ne $ARGS ]
     then
      echo "usage: rsync_wrap souc_folder destination_folder"
      return E_BADARGS
     fi 
    
     if [ -z "$2" ]  ||  [ -z "$1" ]
     then
      echo "empty or missing parameters"
      echo "usage: rsync_wrap souc_folder destination_folder"
      return E_BADARGS
     fi 
    
     if [! -d "$2"] ||   [ ! -d "$1" ]
     then
      echo "parameter not a folder"
      echo "usage: rsync_wrap souc_folder destination_folder"
      return E_BADARGS
     fi 
    
     destLen=`expr length $2  + 2`
    # destLen=`expr $destLen+ 2`
    #echo path len $pathLen
    
     dirlist=$(find $2 -type d)
     for aDirec in $dirlist ; 
     do
    
       aDirecExclBase=`expr  substr $aDirec $destLen 200`
       fileList=$(ls $aDirec)
    
       for aFile in $fileList ;
        do
          if [  -f "$aDirec/$aFile" ] # exclude folders
            then
            if [ ! -e "$1/$aDirecExclBase/$aFile" ] # is it not in source?
              then
              read -p "$aDirecExclBase/$aFile is in destination but not source. Delete in destination (y/n)?"
              [ "$REPLY" == "y" ] || rm  -i "$aDirecExclBase/$aFile"
            fi  # if [ ! -e "$1/$aDirecExclBase/$aFile" ]
    #     echo $aDirecExclBase/$aFile
          fi # if [  -f "$aDirec/$aFile" ] # exclude folders
        done  # after for aFile in $fileList 
     
     done # after for aDirec in $dirlist 
    }
    Last edited by EntropyReduction; March 2nd, 2011 at 05:39 AM. Reason: didn't use code tags

  5. #5
    Join Date
    Feb 2006
    Location
    uk
    Beans
    Hidden!

    Re: rsync --delete with confirmation?

    you should wrap your code in [code] tags, or [PHP] tags maybe to make it more readable.

    i like the idea of doing a dry run first. and then using that output in a rm -i command

    how's this for a one liner:
    Code:
    rm -ri `rsync -anv --delete $source/ $destination/ | grep deleting | sed 's/deleting /$destination/\/g'`

  6. #6
    Join Date
    Dec 2010
    Beans
    31
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: rsync --delete with confirmation?

    Quote Originally Posted by aeiah View Post
    you should wrap your code in [code] tags, or [PHP] tags maybe to make it more readable.

    Sorry. Newbie. Learning. Done.

    > i like the idea of doing a dry run first. and then using that output in a rm -i command

    how's this for a one liner:
    Code:
    rm -ri `rsync -anv --delete $source/ $destination/ | grep deleting | sed 's/deleting /$destination/\/g'`
    Neat.

    I'll be back to you once I've taught myself enough sed to understand it.

  7. #7
    Join Date
    Dec 2010
    Beans
    31
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: rsync --delete with confirmation?

    i like the idea of doing a dry run first. and then using that output in a rm -i command

    how's this for a one liner:
    Code:
    rm -ri `rsync -anv --delete $source/ $destination/ | grep deleting | sed 's/deleting /$destination/\/g'`
    Okay, fiddled with the sed replacement. Need "" instead of '' to get the variable parsed,
    and since it's possible file names have spaces, need (?) to quote them; leaving out the rm -i :

    Code:
    rsync --delete --dry-run  --recursive --verbose  $source/   $destination/ | grep deleting | sed "s:deleting :$destination/:g" |  sed "s/\(^.*\)$/\'\1\'/g"
    Okay, that gets me output on stdout like

    '/home/alan/Documents/temp/dest/test (copy).txt'

    and

    Code:
     rm -i '/home/alan/Documents/temp/dest/test (copy).txt'
    works

    But when I tuck rm -i in front of rsync etc:

    Code:
      rm -i `rsync --delete --dry-run  --recursive --verbose  $source/   $destination/ | grep deleting |   sed "s:deleting :$destination/:g" | sed "s/\(^.*\)$/\'\1\'/g" `
    somehow rm is getting the file names broken at white space ( it can't find files

    /home/alan/Documents/temp/dest/test
    (copy).txt
    )

    I tried wrapping file names in double quotes, same result.

    Any ideas what I'm doing wrong?

  8. #8
    Join Date
    Feb 2010
    Location
    Silicon Valley
    Beans
    1,898
    Distro
    Xubuntu 12.04 Precise Pangolin

    Re: rsync --delete with confirmation?

    Don't use backticks. Use a pipeline into xargs.

    Code:
    .... | xargs -d '\n' rm -ri

  9. #9
    Join Date
    Dec 2010
    Beans
    31
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: rsync --delete with confirmation?

    Quote Originally Posted by gmargo View Post
    Don't use backticks. Use a pipeline into xargs.

    Code:
    .... | xargs -d '\n' rm -ri
    Doesn't work for me:

    (a) I need to not add my own quotes with sed:
    Code:
    .... | sed "s/\(^.*\)$/\'\1\'/g"  | ....
    because it looks like xargs adds its own quoting, and

    (b) Looks like all files are sent to rm without waiting for interactive response from user.

  10. #10
    Join Date
    Dec 2010
    Beans
    31
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: rsync --delete with confirmation?

    This works:

    Code:
    rsync --delete --dry-run  --recursive --verbose  $source/   $destination/ | \
         grep deleting | sed "s:deleting :$destination/:g" |  \
         sed "s/\(^.*\)$/\'\1\'/g"  |  \
         xargs -n 1 -p -r  rm -i
    xargs -n 1: "Use at most max-args arguments per command line"
    - p: "Prompt the user about whether to run each command line and read
    a line from the terminal"
    -r "If the standard input does not contain any nonblanks, do not run
    the command. Normally, the command is run once even if there is
    no input. This option is a GNU extension."

    Thanks for pointing me in the right direction.

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •