[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [grep] Searching with star and files with a name starting with a das
From: |
Davide Brini |
Subject: |
Re: [grep] Searching with star and files with a name starting with a dash |
Date: |
Wed, 26 May 2010 09:23:23 +0100 |
User-agent: |
KMail/1.12.4 (Linux/2.6.31-gentoo-r10; KDE/4.3.5; x86_64; ; ) |
On Tuesday 25 May 2010 15:50:54 Pierre Morin wrote:
> Hello.
>
> I think I found a "bug" with grep in the way I use it.
> I use grep to search a string recursively on files.
>
> For exemple if I search the string "Unix" in all the files of /etc, I go
> under "/etc" in my terminal and then I run the command "grep -r 'Unix'
> *".
> It runs just fine so far.
> But I had the mad idea to name a file with a dash at the beginning of
> the name (exemple : "-test").
> And there, I can see that the name of the file "-test" is interpreting
> by the command as an option.
>
> It's not a real big bug but I think it's strange to have a file name
> interpreting as an option of a command.
>
> Here is how to have the bug :
> - create a file with name beginning with a dash ("-test" for exemple)
> - in the folder containing the file, run the command "grep -r
> 'WhatEverYouWant" *"
The usual way to avoid that is to use the -- separator that tells the command
where the options end, so
grep -r -- somepattern *
should work for you. This is a generic convention for most tools, not just
grep. A workaround could have been
grep -r somepattern ./*
and also note that in this specific case you could also have done
grep -r somepattern .
to get a similar result (and possibly include more files/directories in the
search). Using * also has the potential problem of triggering an "argument
list too long" error if * expands to a huge number of arguments, although that
limit should be fairly high these days (or even no longer present in Linux
ISTR).
--
D.