Find: combine -depth with -prune to feed cpio

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


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

Tags:

Find