xargs - if condition and echo {} xargs - if condition and echo {} unix unix

xargs - if condition and echo {}


Try to run the command through a shell like this:

$ find -iname file.xxx |> xargs -I {} bash -c 'if grep -Fq "string" {} ; then echo {} ; fi'

where the original command has been surrounded by quotes and bash -c.


Wrap the if-statement in a call to sh:

find -iname file.xxx | xargs -I {} sh -c 'grep -Fq "string" {} && { echo {}; }'

Use a while-loop instead of xargs

find -iname file.xxx | while read -r file; do  if grep -Fq "$file"; then    # do something    echo "$file"  fidone

I assume you want to do more than echo the filename. If that's all you're trying to do, use grep's -l option:

find -iname file.xxx | xargs grep -Fl "string"


First, if is a bash-command and no executable program. xargs on the other hand needs a distinct program.

Second, the ; characters are probably splitting the your command. You would have to escape them in order to get them throu to xargs.

To avoid all this I suggest the following:

for i in `find -iname file.xxx` ; do grep -Fq "string" $i && echo $i ; done