I have some files that contain a particular strings. What I want to do is, search a location for the file; if the file exists grep for the pattern; if true, do something.
find -iname file.xxx| xargs -I {} if grep -Fq "string" {} ; then echo {} ; fi
The problems are:
xargs
is not working with the if statement.echo {}
does not give the file name, instead gives {}
.How do I fix these?
The -c flag to sh only accepts one argument while xargs is splitting the arguments on whitespace - that's why the double quoting works (one level to make it a single word for the shell, one for xargs).
xargs (short for "extended arguments") is a command on Unix and most Unix-like operating systems used to build and execute commands from standard input. It converts input from standard input into arguments to a command.
xargs is a Unix command which can be used to build and execute commands from standard input. Importance : Some commands like grep can accept input as parameters, but some commands accepts arguments, this is place where xargs came into picture. -L max-lines : use at-most max-lines non-blank input lines per command line.
xargs reads items from the standard input, delimited by blanks (which can be protected with double or single quotes or a backslash) or newlines, and executes the command (default is echo) one or more times with any initial-arguments followed by items read from standard input.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With