Bash: find with -depth and -prune to feed cpio
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.
Luis
Updated on June 04, 2022Comments
-
Luis almost 2 years
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.
-
Luis over 12 yearsI appreciate your answer but it doesn't solve my problem. I'll post my question in the Unix & Linux section.
-
DonGar over 10 years-prune is basically useless in combination with -depth, since it's only applied after you've already visited all of the children.