LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 11-18-2008, 11:24 PM   #1
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 3,118

Rep: Reputation: 46
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.
 
Old 11-19-2008, 12:30 AM   #2
vikas027
Senior Member
 
Registered: May 2007
Location: Sydney
Distribution: RHEL, CentOS, Debian, OS X
Posts: 1,266

Rep: Reputation: 99
Smile

Quote:
Originally Posted by ENRIQUESTEFANINI View Post
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.
 
Old 11-19-2008, 01:27 AM   #3
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Slackware 10.1/10.2/12, Ubuntu 12.04, Crunchbang Statler
Posts: 3,786

Rep: Reputation: 282Reputation: 282Reputation: 282
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)
 
Old 11-19-2008, 01:34 AM   #4
osamaensyviswinkel
LQ Newbie
 
Registered: Sep 2007
Location: Stellenbosch, South Africa
Distribution: Ubuntu 12.10 at home, Mac OS X on laptop
Posts: 20

Rep: Reputation: 0
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
 
Old 11-19-2008, 01:35 AM   #5
billymayday
Guru
 
Registered: Mar 2006
Location: Sydney, Australia
Distribution: Fedora, CentOS, OpenSuse, Slack, Gentoo, Debian, Arch, PCBSD
Posts: 6,678

Rep: Reputation: 122Reputation: 122
You could use

grep blah .*

to cover those
 
Old 11-19-2008, 04:17 AM   #6
DragonSlayer48DX
Registered User
 
Registered: Dec 2006
Posts: 1,454
Blog Entries: 1

Rep: Reputation: 74
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
 
Old 11-19-2008, 04:37 AM   #7
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
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.
 
Old 11-19-2008, 04:48 AM   #8
DragonSlayer48DX
Registered User
 
Registered: Dec 2006
Posts: 1,454
Blog Entries: 1

Rep: Reputation: 74
Quote:
Originally Posted by pixellany View Post
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
 
Old 11-19-2008, 06:01 AM   #9
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
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.
 
Old 11-19-2008, 06:30 AM   #10
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,036

Rep: Reputation: 373Reputation: 373Reputation: 373Reputation: 373
You all are looking for shopt and the dotglob option (in bash anyway).
 
Old 11-19-2008, 08:50 AM   #11
knudfl
LQ 5k Club
 
Registered: Jan 2008
Location: Copenhagen, DK
Distribution: pclos2014, Slack14.1 DebWheezy, +50+ other Linux OS, for test only.
Posts: 13,536

Rep: Reputation: 2506Reputation: 2506Reputation: 2506Reputation: 2506Reputation: 2506Reputation: 2506Reputation: 2506Reputation: 2506Reputation: 2506Reputation: 2506Reputation: 2506
ls -la | grep abc

seems to get the hidden abc's
 
Old 11-20-2008, 12:09 AM   #12
osamaensyviswinkel
LQ Newbie
 
Registered: Sep 2007
Location: Stellenbosch, South Africa
Distribution: Ubuntu 12.10 at home, Mac OS X on laptop
Posts: 20

Rep: Reputation: 0
yup, ls's -a also works.

-a, --all
do not ignore entries starting with .
 
Old 05-30-2009, 02:42 AM   #13
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 3,118

Original Poster
Rep: Reputation: 46
Thank you all, guys. And sorry for the delay.
 
Old 05-31-2009, 03:50 AM   #14
knudfl
LQ 5k Club
 
Registered: Jan 2008
Location: Copenhagen, DK
Distribution: pclos2014, Slack14.1 DebWheezy, +50+ other Linux OS, for test only.
Posts: 13,536

Rep: Reputation: 2506Reputation: 2506Reputation: 2506Reputation: 2506Reputation: 2506Reputation: 2506Reputation: 2506Reputation: 2506Reputation: 2506Reputation: 2506Reputation: 2506
.
It's OK .... the blue "Thumbs Up" can be used too.

http://www.linuxquestions.org/questi...e-read-701224/
 
Old 05-31-2009, 12:06 PM   #15
malekmustaq
Senior Member
 
Registered: Dec 2008
Location: /root
Distribution: Slackware & BSD
Posts: 1,206

Rep: Reputation: 230Reputation: 230Reputation: 230
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.
 
  


Reply

Tags
grep


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Truncate from beginning of the file lvprabhu Programming 12 05-25-2010 09:43 PM
renaming file names beginning with "-" prashln Linux - General 5 05-01-2010 01:47 AM
Add text to the beginning of file names xebix Linux - Newbie 5 10-02-2008 12:43 AM
!? element names beginning with number break javascript??? UMG:Chicken_Soüp Programming 1 01-27-2008 11:12 PM
Force grep NOT to print file names smart_sagittari Linux - Newbie 5 04-25-2005 01:20 AM


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

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration