3

How can I find "-R" in files with grep? I tried grep "-R" *.GNU to get the lines that contain -R, but it returns nothing.

1

5 Answers 5

9

Try:

grep -- -R *.GNU

The -- indicates end of options so that -R is seen as an argument instead of an option to grep

5

A fair amount of the GNU tools support an option where you can tell the tool (rm, grep, ls, etc.) that you no longer want it to continue parsing for command line options to itself. This option is facilitated by marking the command line input with a double dash --.

You can read more about it in the GNU CoreUtils FAQ. Specifically this section, titled: 11 How do I remove files that start with a dash.

This allows for files that begin with a dash (i.e. -) to be passed as command line arguments:

examples

rm ./-stuff
rm /full/path/-stuff
rm -- -stuff

mv ./-stuff differentstuff
mv -- -stuff differentstuff
2
  • 1
    That's not GNU specific though. That's POSIX. It's not all the tools (see for instance test or echo). Also note that with find, -- stops filenames from being taken as options, but not from being taken as predicates. Jun 25, 2013 at 22:07
  • @StephaneChazelas - Yes I was afraid when I stated all that I was overstating it but I believe it to be most, or at least all that make use of the coreutils package. If I run the following command under Fedora, rpm -q --whatrequires coreutils -l I get a list of all the files that require it and it's a pretty large chunk of the GNU tools from what I can tell. Thanks for the clarification on find, yet another thing I wasn't aware of 8-).
    – slm
    Jun 26, 2013 at 0:43
3

The most common ways

grep -- -R *.GNU works with some variants of grep. The -- tells grep to stop looking for flags.

grep -e -R *.GNU works as well. The -e tells grep that a search pattern follows.

Alternatives

grep '[-]R' *.GNU

[-] is a character class. Since there's only one character in the class, it must match a hyphen (-).

grep '.\?-R' *.GNU

The question mark tells grep that the character prior to it is optional. The . matches any character.

grep '\(\)-R' *.GNU

The parenthesis make an empty group.

1
  • 2
    +1 for encouraging creativity. There is more than one way to do it!
    – ruakh
    Jun 26, 2013 at 5:52
3
grep -e -R ./*.GNU

is another possibility.

1
  • 1
    @stephane sure, but that's a separate issue.
    – Kevin
    Jun 26, 2013 at 1:21
0

Try escaping the dash like

grep "\-R" *.GNU
1
  • Technically, that works as long as \- is not recognised as an extended regexp operator by your grep implementation. The backslash doesn't escape the - as far as the regexp is concerned (neither - nor \- are special so the backslash is ignored), but because now the first argument to grep doesn't start with a -, it escapes the - in the sense that it's no longer causing the argument to be taken as an option. grep '[-]R' or grep '\(-R\)' would also work. You'd still need a -- with GNU grep if some filenames may start with -. Jun 25, 2013 at 22:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .