LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   grep does not care about file names beginning with period. (https://www.linuxquestions.org/questions/linux-newbie-8/grep-does-not-care-about-file-names-beginning-with-period-684478/)

stf92 11-18-2008 11:24 PM

grep does not care about file names beginning with period.
 
Hi.
Why can't grep ((GNU grep) 2.5) find strings in files whose name begins with '.' (period)?
Thanks for reading.

vikas027 11-19-2008 12:30 AM

Quote:

Originally Posted by ENRIQUESTEFANINI (Post 3347055)
Hi.
Why can't grep ((GNU grep) 2.5) find strings in files whose name begins with '.' (period)?
Thanks for reading.

give an example, pls elaborate.

Wim Sturkenboom 11-19-2008 01:27 AM

I assume that OP means that grep abc * does not return results in files starting with a dot although one might expect that. If so, read on

That's not grep, but the shell. The shell interpretes the command that you have typed and will say:
hey, grep is the command, let's try to find it
hey, abc is an argument, just pass it
hey, * is a wildcard so I will try to expand it

When you pass the wildcard, the shell will expand it and pass all matching files. However, the shell does not include files starting with a dot.

You can test it by running echo * which will display a list of all files in a directory (except the dot files ;) ).

Possible other ways around it:
use grep abc .*
use find with the -exec option
influence bash's behaviour (see e.g. http://halisway.blogspot.com/2007/02...dot-files.html)

osamaensyviswinkel 11-19-2008 01:34 AM

try the following:

[09:28][root@francoisvn]~ # ls -al lllllll/ | egrep -e "^-.*[[:space:]]\."
-rw-r--r-- 1 root root 0 2008-11-19 07:56 .bla
-rw-r--r-- 1 root root 0 2008-11-19 07:56 .werwer

billymayday 11-19-2008 01:35 AM

You could use

grep blah .*

to cover those

DragonSlayer48DX 11-19-2008 04:17 AM

The reason is that the dot denotes a hidden file (or folder).
You also can't see it in your file manager, unless you:

A) select Show Hidden Files from the View menu, or

B) configure the file manager to Show Hidden Files by default.

Hope this helps

pixellany 11-19-2008 04:37 AM

Changing the settings of the GUI file manager does not change the behavior of BASH commands in the terminal--at least not on my system.

DragonSlayer48DX 11-19-2008 04:48 AM

Quote:

Originally Posted by pixellany (Post 3347288)
Changing the settings of the GUI file manager does not change the behavior of BASH commands in the terminal--at least not on my system.

Nor mine. Those were instructions to view them in the GUI, just to better explain that file and folder names beginning with a "." are hidden.

Cheers

jschiwal 11-19-2008 06:01 AM

It may be better to use the wildcard ".[^\.]*" to prevent a match of "..".
grep pattern .[^\.]*

If you also want to include non-hidden files, then you can use "grep pattern .[^\.]* *" for example.

i92guboj 11-19-2008 06:30 AM

You all are looking for shopt and the dotglob option (in bash anyway).

knudfl 11-19-2008 08:50 AM

ls -la | grep abc

seems to get the hidden abc's

osamaensyviswinkel 11-20-2008 12:09 AM

yup, ls's -a also works.

-a, --all
do not ignore entries starting with .

stf92 05-30-2009 02:42 AM

Thank you all, guys. And sorry for the delay.

knudfl 05-31-2009 03:50 AM

.
It's OK .... :p the blue "Thumbs Up" can be used too. :p

http://www.linuxquestions.org/questi...e-read-701224/

malekmustaq 05-31-2009 12:06 PM

ENRIQUE:

I have experienced the same. I think the reason for this is that bash treats "." period as the "current directory". So each time I use grep against a .file (hidden) I have to define its directory first. This manner: for example, against the file ~/.dmrc we do:

user@host-$ grep 'Session' ./.dmrc

Returns:
Session=Xfce

Hope it helps.

Goodluck Enrique, hasta la vista.


All times are GMT -5. The time now is 12:09 AM.