I have a file that has terms I want to grep for, with each term being one line in the file. I was thinking I could do this with xargs. What I'm able to glean from examples from the man page like this
find ./work -print0 | xargs -0 rm
is that xargs appends the output of the pre-pipe command to the end of its arguments. So if the find returned report.doc
, then xargs would construct rm report.doc
. Is this understanding correct?
So since I want the values in my file to be in the middle of the grep command, I need to specify a placeholder. In playing around, I tried {}
, but it didn't work:
$> cat strings.txt | xargs grep {} subdirectory/*
grep: string1: No such file or directory
grep: string2: No such file or directory
Is xargs the right tool? If so, what is the syntax?
Best Answer
Yes,
find ./work -print0 | xargs -0 rm
will execute something likerm ./work/a "work/b c" ...
. You can check withecho
,find ./work -print0 | xargs -0 echo rm
will print the command that will be executed (except white space will be escaped appropriately, though theecho
won't show that).To get
xargs
to put the names in the middle, you need to add-I[string]
, where[string]
is what you want to be replaced with the argument, in this case you'd use-I{}
, e.g.<strings.txt xargs -I{} grep {} directory/*
.What you actually want to use is
grep -F -f strings.txt
:So
grep -Ff strings.txt subdirectory/*
will find all occurrences of any string instrings.txt
as a literal, if you drop the-F
option you can use regular expressions in the file. You could actually usegrep -F "$(<strings.txt)" directory/*
too. If you want to practicefind
, you can use the last two examples in the summary. If you want to do a recursive search instead of just the first level, you have a few options, also in the summary.Summary:
You may want to use the
-l
option to get just the name of each matching file if you don't need to see the actual line: