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 building a backup script where some directories should not be included in the backup archive.

cd /;
find . -maxdepth 2 \ 
    \( -path './sys' -o -path './dev' -o -path './proc' -o -path './media' -o -path './mnt' \) -prune \
-o -print

This finds only the files and directories I want.

Problem is that cpio should be fed with the following option in order to avoid problems with permissions when restoring files.

find ... -depth ....

And if I add the -depth option, returned files and directories include those I want to avoid.

I really don't understand these sentences from the find manual:

-prune True;  if  the  file is a directory, do not descend into it. If
              -depth is given, false; no  effect.   Because  -delete  implies
              -depth, you cannot usefully use -prune and -delete together.
share|improve this question
up vote 2 down vote accepted

The explanation of what the manual says is this:

When find gets to a directory that matches your -path expression, -prune will avoid going inside it. So find will go

/             ok, go inside
/home         ok, go inside
/home/xxxx    ok, go inside
/tmp          don't go inside
/var          ..etc...

But when you use -depth, it processes the inside of directories before the directories themselves. So it will match the path when it's too late:

/home         ok, go inside (it already went)
/tmp/zzzz     didn't match "-path /tmp", so it's ok
/tmp          don't go inside (too late!)
/var          ..etc...

To solve this problem you can try:

  1. Just add new -path expressions with wildcards. This has the disadvantage that those subdirectories will be traversed anyway, just not printed (and their traversal will maybe trigger warnings)

    find ... \( -path './sys' -o -path './sys/*' -o -path './dev' -o -path './dev/*' ... \) -prune ...

  2. Don't enumerate the directories to avoid, enumerate the ones to print!

    find /bin /boot /etc /home /lib ...

share|improve this answer
+1 very nice explanation – user unknown Jan 26 '12 at 1:11

angus's answer explains why -depth doesn't work for you and proposes solutions.

It looks like you want to traverse a whole installation, but omit special filesystems like /proc and /sys and external devices. There's a better way to do this: use the -xdev primary on find to tell it not to descend into mount points. If you want to include some mounted filesystems in your backup, list them explicitly.

find / /home -xdev -depth -print
share|improve this answer
I've seen the -xdev option before but it doesn't fully suits my needs. Thank you for this idea anyway. – Luis Jan 26 '12 at 15:36

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.