[net.unix-wizards] -depth option for find

wsh@hou5f.UUCP (Willie Heck) (08/10/83)

     Could somebody explain what the -depth option does to find(1)?  It
seems to reorder the directory search pattern for the benefit of cpio(1),
but I don't know exactly how.  It is not documented in the 5.0 manual,
although it is used (without explanation) in that manual in an example
in the cpio man pages.
                                        Thanking you in advance,
                                        Willie Heck
                                        AT&T Information Systems - holmdel
                                        ariel!hou5f!wsh

gwyn@brl-vld@sri-unix.UUCP (08/11/83)

From:      Doug Gwyn (VLD/VMB) <gwyn@brl-vld>

I seem to remember reading that the -depth option disables the limit
otherwise imposed on the depth of tree recursion in System V "find".
However, I can't find where I read this.

P.S.  If this really is how it works then I don't like it.  The default
behavior should be to traverse the entire directory tree.

ka@spanky.UUCP (08/15/83)

The -depth option always returns true, but if it is present the find
command will skip directories.  Thus "find -depth -print" will only
print the names of files which are not directories.  I think that the
inclusion of this kluge in find was a mistake; the same effect can be
obtained with "find -type d -o -print".  The fact that they didn't
document it gives me some hope that it may eventually go away.
					Kenneth Almquist

jhh@ihldt.UUCP (08/16/83)

The -depth option for find does not cause it to skip directories,
as some earlier articles stated, it causes it to do a depth
first search of the tree, i.e. the output looks like

	./foo
	./bar
	.

Presumably, this can be used to change permissions on a directory
after all the files and subdirectories have been created.  This
allows a mode 555 directory to be properly copied:

-r--r--r--	./foo
-r--r--r--	./bar
dr-xr-xr-x	.

			John Haller

tsclark@ihnp4.UUCP (08/16/83)

Ok...here's what "find -depth" REALLY does, and why it is a good thing:

Normally, if you use find on a directory tree, it prints the name of the
directory followed by the contents of that directory (recursively). That
is, it prints as it goes down the tree. For example:
top
top/second
top/second/third
top/second/file
top/file
etc.

This is good if you are copying files since you would like the directory
created before you try to put anything in it.

What the -depth option does is list the contents of the directory first,
and then later give the name of the directory, such as:
top/second/third
top/second/file
top/second
top/file
top

This is good if you don't want to change the directory until you've changed
everything inside it. For instance:

	find . -depth -exec rm {} \;   (same as rm -r .)
	find . -depth -exec chown otheruser {} \;

I know it's not documented in the System V manual -- I submitted an MR on this
and it will be documented in the next issue (I believe).

andrew@orca.UUCP (Andrew Klossner) (08/17/83)

	"This option causes find to perform a 'form' of depth first
	search through the directory tree; all entries in a directory
	will be expanded before the directory name is found."

It needs to be said.  Both methods of walking the directory tree, where
directories are visited before or after their files, are forms of
depth-first search.  The complement to depth-first is breadth-first,
where you open all the top-level directories, followed by all the files
named in those directories, followed by all the files named in all the
directories named in those directories, etc.

Visiting directories before their descendants is called a "pre-order
walk" of the directory tree.  Visiting directories after their
descendants is called a "post-order walk".  There's also an "in-order"
walk, where you visit the first file in the directory, then the
directory, then the rest of its files, but that's not very useful
unless the tree is binary.

  -- Andrew Klossner   (decvax!tektronix!tekecs!andrew)  [UUCP]
                       (andrew.tektronix@rand-relay)     [ARPA]