The xargs manual says :
-I replace-str
–replace[=replace-str]
-i[replace-str]
Replace occurrences of replace-str in the initial arguments with names read from standard input.
I don't understand this part: with names read from standard input.
For example what is happening with:
find . -mindepth 1 -maxdepth 1 -print0 | xargs -0I{} echo | wc -l
The above piece of code counts the total files/directories inside a directory.
Could anybody explain this for me?
Best Answer
"with names read from standard input" means that
xargs
takes the data coming in on its standard input, splits it up, and uses it to run the command given in its arguments. By default, it splits on blanks or newlines, and runsecho
with as many arguments at a time as possible.The
-0
option in your example instructsxargs
to split its input on null bytes instead of blanks or newlines. Combined withfind
's-print0
, this allows filenames containing blanks or newlines to be handled properly.The
-I
option changes the way the new command lines are built. Instead of adding as many arguments as possible at a time,xargs
will take one name at a time from its input, look for the given token ({}
here) and replace that with the name.In your example,
{}
isn't present in the command template given toxargs
, so in effectxargs
is instructed to runecho
with no argument, once for every filename given to it byfind
. To see this, drop thewc
:You'll see a series of blank lines... Compare this with
and
and
to get a better understanding.