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.

4 Answers 4


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

  • 1. If ... is -or ... -print, one can drop the -prune before -or: they won't be printed anyway. Jan 27, 2022 at 0:29

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
  • 1
    I've seen the -xdev option before but it doesn't fully suits my needs. Thank you for this idea anyway.
    – Luis
    Jan 26, 2012 at 15:36

As a first step we reject the specific directories and then call in find with the -depth on the remaining to get the required format:

cd / && \
find . -maxdepth 1 -type d \
   \( -name sys -o -name dev -o -name proc -o -name media -o -name mnt \) -prune -o \
   ! -name . -exec sh -c 'find "$1" -depth' {} {} \;

Using -regex if it's supported by your find:

find . -depth [...] -regex '\./\(sys\|dev\|proc\|media\|mnt\)\(/.*\)?' -prune -o -print

Note that -regex is not POSIX and might not be necessarily supported everywhere. Also the content of the directories will still be traversed.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .