1

I'm trying to escape characters within backticks in my bash command, mainly to handle spaces in filenames which cause my command to fail.

The command I have so far is:

grep -Li badword `grep -lr goodword *`

This command should result in a list of files that do not contain the word "badword" but do contain "goodword".

Ian McIntyre Silber
  • 5,553
  • 13
  • 53
  • 76

2 Answers2

3

Your approach, even if you get the escaping right, will run into problems when the number of files output by the goodword grep reaches the limits on command-line length. It is better to pipe the output of the first grep onto a second grep, like this

grep -lr -- goodword * | xargs grep -Li -- badword

This will correctly handle files with spaces in them, but it will fail if a file name has a newline in it. At least GNU grep and xargs support separating the file names with NUL bytes, like this

grep -lrZ -- goodword * | xargs -0 grep -Li -- badword

EDIT: Added double dashes -- to grep invocations to avoid the case when some file names start with - and would be interpreted by grep as additional options.

JaakkoK
  • 8,247
  • 2
  • 32
  • 50
  • Thanks for the tip! I'm still getting some weird behavior. When I try to run this command, I get the following output: root@domain [/path]# grep -lrZ goodword * | xargs -0 grep -Li badword grep: invalid option -- k Usage: grep [OPTION]... PATTERN [FILE]... Try `grep --help' for more information. (standard input) – Ian McIntyre Silber Aug 13 '10 at 19:12
  • @Ian: Do you have a file whose name starts with a `-` in the directory? In that case, remember to also add `--` before the actual arguments to `grep`. I've modified the answer accordingly. – JaakkoK Aug 13 '10 at 19:20
1

How about rewrite it to:

grep -lr goodword * | grep -Li badword
William Niu
  • 15,798
  • 7
  • 53
  • 93