rsync -rlptD to copy a directory from another user. There are a few files (I have no way of knowing these in advance) which I don't have permission to copy. Is there a way have rsync ignore these. The trouble is that if rsync return non-zero my bash -x script will exit.
Rsync doesn't have an option for this. I see two solutions. One is to parse rsync error messages; this isn't very robust. The other is to generate a list of unreadable files to filter.
cd /source/directory exclude_file=$(mktemp) find . ! -readable -o -type d ! -executable | sed -e 's:^\./:/:' -e 's:[?*\\:\\1:g' >>"$exclude_file" rsync -rlptD --exclude-from="$exclude_file" . /target/directory rm "$exclude_file"
find doesn't have
-executable, replace them by the appropriate
This assumes that there are no unreadable files whose name contains a newline. If you need to cope with those, you'll need to produce a null-delimited file list like this, and pass the
-0 option to
find . \( ! -readable -o -type d ! -executable \) -print0 | perl -0000 -pe 's:\A\./:/:' -e 's:[?*\\:$1:g' >>"$exclude_file"
I made a simple workaround for this specific situation:
rsync --args || $(case "$?" in 0|23) exit 0 ;; *) exit $?; esac)
0 if the returned code was 0 or 23, and returns the exit code in all other cases.
It is important to note, however, that this would ignore all
Partial transfer due to error errors, not just permission ones, since it will catch everything that exits code
23. For more information about rsync status codes please refer to this link.