creating pipes in find(1)

Fred Yankowski fcy at iham1.UUCP
Thu Oct 4 01:09:39 AEST 1984


In UNIX System V Release 1, 'find' processes its '-exec' arguments
with 'execvp(II)', not 'system(III)'.  This means that *no* shell
processing is done:  '|', '&', ';', '(', etc. are not interpreted by
the shell.  The command

	find / -type f -exec cat {} \| lpr \;

will then execute

	cat "file" "|" "lpr"

for each file "file" found by 'find' (got that?).
That is, 'cat' is executed with three arguments: "file", "|", and "lpr".

One handy idiom for processing files found by 'find' is:

	find dir <find arguments> -print | xargs <process>

For example:

	find / -type f -print | xargs -n1 lpr

does what seems to be desired in the first (faulty) 'find' command
above.  Even better is

	find / -type f -print | xargs lpr

in which 'xargs' repeatedly gathers as many file names as fit in an
internal buffer and executes 'lpr' against each such list of file
arguments.  The improvement is in execution time, since 'lpr' is
fork/execed once for each list of files, rather than for each file
alone.

Similarly, cleaning out a directory is better accomplished with

	find . -type f -print | xargs rm

rather than

	find . -type f -exec rm {} \;


    Fred Yankowski ::: AT&T Bell Laboratories ::: ihnp4!iham1!fcy
			  IH 6B-216  x6902



More information about the Comp.unix mailing list