what is diff bentween xargs with braces and without in linux

19

3

I want to know what is the difference between this

ls | xargs rm

ls | xargs -i{} rm {}

Both are working for me

user19140477031

Posted 2012-12-31T08:03:26.507

Reputation:

Answers

19

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.

Shiplu Mokaddim

Posted 2012-12-31T08:03:26.507

Reputation: 647

WHICH ONE IS BETTER – None – 2012-12-31T08:32:57.633

1@user19140477031 depends one what operation you are performing. for rm it does not matter – Shiplu Mokaddim – 2012-12-31T08:45:40.023

3

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 '{}'

kmkaplan

Posted 2012-12-31T08:03:26.507

Reputation: 334

2

-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.

aag

Posted 2012-12-31T08:03:26.507

Reputation: 121