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.

  • A similar question was asked on Stackoverflow. You can find some useful answers and examples there. – Alex.K. Sep 25 '14 at 8:41


grep -- -R *.GNU

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

| improve this answer | |

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:


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

mv ./-stuff differentstuff
mv -- -stuff differentstuff
| improve this answer | |
  • 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. – Stéphane Chazelas Jun 25 '13 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 '13 at 0:43

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.


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.

| improve this answer | |
  • 2
    +1 for encouraging creativity. There is more than one way to do it! – ruakh Jun 26 '13 at 5:52
grep -e -R ./*.GNU

is another possibility.

| improve this answer | |
  • 1
    @stephane sure, but that's a separate issue. – Kevin Jun 26 '13 at 1:21

Try escaping the dash like

grep "\-R" *.GNU
| improve this answer | |
  • 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 -. – Stéphane Chazelas Jun 25 '13 at 22:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.