what is diff bentween xargs with braces and without in linux

I want to know what is the difference between this

ls | xargs rm

ls | xargs -i{} rm {}

Both are working for me


Solution 1:

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.

Solution 2:

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