What is difference between xargs with braces and without in Linux?
Become or hire the top 3% of the developers on Toptal https://topt.al/25cXVn
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: Horror Game Menu Looping
--
Chapters
00:00 Question
00:22 Accepted answer (Score 34)
01:20 Answer 2 (Score 5)
02:04 Answer 3 (Score 4)
02:29 Thank you
--
Full question
https://superuser.com/questions/526352/w...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#linux #centos #xargs
#avk47
ACCEPTED ANSWER
Score 34
xargs rm
will invoke rm
with all arguments as parameter departed with spaces.
xargs -i{} rm {}
will invoke rm {}
for each of the argument and {}
will be replaced by the current argument.
If you have 2 arguments a.txt
and b.txt
, xargs rm
will call this
rm a.txt b.txt
But xargs -i{} rm {}
will call
rm a.txt
rm b.txt
This is because -i
option implies -L 1
option which mean the command rm
will take only 1
line each time. And here each line contains only 1 argument.
Check this Ideone link to get more idea about it.
ANSWER 2
Score 6
-i
option (equivalent to --replace
) creates a sort of placeholder where xargs stores the input it just received. In your second command, the placeholder is {}
, it works like find -exec
option. Once defined, xargs will replace this placeholder with the entire line of input. If you don’t like the {}
name, you can define your own:
ls | xargs -iPLACEHOLDER echo PLACEHOLDER
In your case, both commands are producing the same result. In the second form, you are just making explicit the default behaviour with the -I
option.
ANSWER 3
Score 4
With braces it will spawn one rm
process per file. Without the braces, xargs
will pass as many filenames as possible to each rm
command.
Compare
ls | xargs echo
and
ls | xargs -I {} echo {}