Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.

share|improve this question
    
Related: unix.stackexchange.com/q/50572/22142 –  don_crissti Jun 25 '13 at 21:36

5 Answers 5

up vote 8 down vote accepted

Try:

grep -- -R *.GNU

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

share|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:

examples

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

mv ./-stuff differentstuff
mv -- -stuff differentstuff
share|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.

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.

share|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.

share|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
share|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
    
I see. Thanks for explaining that. –  William Everett Jun 25 '13 at 22:25

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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