Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

subtle difference in using xargs and xargs -i

Tags:

grep

bash

xargs

Why does find . -name "*.xml" | xargs grep FOO returns matchs with filenames, while find . -name "*.xml" | xargs -i -sh -c "grep FOO {}" doesn't?

like image 413
shabunc Avatar asked Mar 04 '11 10:03

shabunc


1 Answers

Unless it's a typo in posting your question there shouldn't be a hyphen before sh:

The reason you don't get filenames in the output is that grep is being run with a single file as an argument. To force filename output use -H.

find . -name "*.xml" | xargs -I {} sh -c "grep -H FOO {}"

Also, -i for xargs was deprecated around version 4.2.9. You should use -I {}.

like image 113
Dennis Williamson Avatar answered Nov 10 '22 06:11

Dennis Williamson