What's the usage of -exec xargs and -print0?
Solution 1:
First things to type:
man find
man xargs
The find
command prints results to standard output by default, so the -print
option is normally not needed, but -print0
separates the filenames with a 0 (NULL) byte so that names containing spaces or newlines can be interpreted correctly.
The -exec
option is something you can use instead of xargs - the find command executes a command for each item it finds.
The xargs
command reads space- or newline-separated strings (typically from the find command, but they could come from anywhere) and executes some command for each string.
If xargs is run with a -0 option, it'll expect NULL-separated strings as output by find ... -print0
The advantage of xargs is that it can group the strings together, so that it only executes a command once or twice instead of n times.
So in the normal usage:
find start_directory -name '*.txt' | xargs ls -l
find would list the filenames, and xargs would issue commands like:
ls -l file1.txt file2.txt file3.txt ... fileN.txt
which is faster than having your find command issuing:
ls -l file1.txt
ls -l file2.txt
ls -l file3.txt
ls -l ...
ls -l fileN.txt
Solution 2:
Note that xargs is no more needed with current find implementations that probably all support this POSIX syntax:
find directory -name '*.txt' -exec ls -l {} +
which is simpler and slightly faster than the xargs variant.
find directory -name '*.txt' | xargs ls -l
Solution 3:
See the following articles:
Linux and Unix find command tutorial with examples
xargs: How To Control and Use Command Line Arguments