find utility
-print == default
If you just want to print the path and file names, you need to abandon exec echo because -print by default .:
find . -name '*.html' -or -name '*.xml'
Depends on the order
Otherwise, find is read from left to right, the order of the arguments is important!
So, if you want to indicate something, observe and and or priority:
find . -name '*.html' -exec echo ">"{} \; -o -name '*.xml' -exec echo "+"{} \;
or
find . -maxdepth 4 \( -name '*.html' -o -name '*.xml' \) -exec echo {} \;
Expression -print0 and -print0 command.
But for most cases, you can consider -print0 with the -print0 command, for example:
find . \( -name '*.html' -o -name '*.xml' \) -print0 | xargs -0 printf -- "-- %s -\n"
The advantage of this is as follows:
Only one (or several) plugs for thousands of records found. (Using -exec echo {} \; implies that one subprocess is started for each entry, and xargs build a long line with as many arguments as one command line could contain ...)
To work with file names containing a special character or space, -print0 and -print0 xargs -0 will use the NULL character as a file name separator.
find ... -exec ... {} ... +
A few years ago, the find adopted a new syntax for the -exec switch.
Instead of \; The -exec switch may end with a plus sign + .
find . \( -name '*.html' -o -name '*.xml' \) -exec printf -- "-- %s -\n" {} +
Using this syntax, find will work as an xargs command, creating long command lines to reduce forks.
source share