Luis
Luis

Reputation: 1348

Bash: find with -depth and -prune to feed cpio

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.

Upvotes: 2

Views: 14086

Answers (1)

jaypal singh
jaypal singh

Reputation: 77095

I am quoting a passage from this tutorial which might offer better understanding of -prune option of find.

It is important to understand how to prevent find from going too far. The important option in this case is -prune. This option confuses people because it is always true. It has a side-effect that is important. If the file being looked at is a directory, it will not travel down the directory. Here is an example that lists all files in a directory but does not look at any files in subdirectories under the top level:

find * -type f -print -o -type d -prune

This will print all plain files and prune the search at all directories. To print files except for those in a Source Code Control Directories, use:

find . -print -o -name SCCS -prune

If the -o option is excluded, the SCCS directory will be printed along with the other files.

Source

Upvotes: 1

Related Questions