Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm using 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.

share|improve this question
Rsync has well documented exit values. You shouldn't treat all of them as a failure if your situation doesn't call for it to be a failure. – jordanm Feb 1 '13 at 23:15
@jordanm But there's no error code that's specific enough to pinpoint this error only. – Gilles Feb 2 '13 at 0:24
up vote 5 down vote accepted

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
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"

If your find doesn't have -readable and -executable, replace them by the appropriate -perm directive.

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 rsync:

find . \( ! -readable -o -type d ! -executable \) -print0 |
  perl -0000 -pe 's:\A\./:/:' -e 's:[?*\\[]:$1:g' >>"$exclude_file"
share|improve this answer
This is probably his best bet. Hopefully, he only needs to run this once, as having to stat() every file twice (once via rsync and once via find) would be pretty bad. – jordanm Feb 2 '13 at 3:37
@Gilles this seems to work, except for hidden files. I'm assuming the same strategy will work with some minor tweaking. I'm not familiar with the \! (slash exclamation) could you explain that? – JeffCharter Feb 4 '13 at 5:23
@JeffC \! quotes the ! operator to protect it against shell expansion. The backslash isn't actually necessary here since no shell expands ! when it's followed by a space, but it doesn't hurt. What's wrong with hidden files? – Gilles Feb 4 '13 at 9:31

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.