From my understanding, the following should mean exactly the same:
ls -1 | xargs file {}
ls -1 | xargs -I{} file {}
if -I option is not specified, it is default to -I{}.
I want to list all files in the current directory and run file
command on each of them. Some have spaces in their names. However, I noticed the difference. See below:
$ ls -1
Hello World
$ ls -1 | xargs file {}
{}: ERROR: cannot open `{}' (No such file or directory)
Hello: ERROR: cannot open `Hello' (No such file or directory)
World: ERROR: cannot open `World' (No such file or directory)
$ ls -1 | xargs -I{} file {}
Hello World: directory
With -I{} explicitly specified, blanks in file names are treated as expected.
xargs file
orxargs -I{} file {}
. Shouldn't bexargs file {}
. I guess when explicitly specifying -I{}, the bash will treat it asfile "Hello World"
. Without -I{}, it is treated asfile Hello World
.-1
option tols
, but it’s on by default when the output ofls
is a file or a pipe, so you don’t need it here. (3) You’re confusing-I
with the deprecated-i
(lower caseI
) option.-ifoo
is equivalent to-Ifoo
, but plain-i
is equivalent to-I{}
. But use-I{}
. (4) If you really want to do what you say you want to do, why not just sayfile *
?file
on them. But I came up with a simpler example usingls
. You are right. For that,file *
is the best.-exec
.