xargs -I replace-str option difference

Posted by foresightyj on Super User See other posts from Super User or by foresightyj
Published on 2013-10-07T01:14:31Z Indexed on 2013/11/08 22:00 UTC
Read the original article Hit count: 306

Filed under:
|

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.

© Super User or respective owner

Related posts about bash

Related posts about xargs