Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
To display lines with the first char of that line being a d, which are the directories. However
why is it that when I use
Code:
ls -l | grep ^d*
does it display lines that do NOT include a 'd' as the first char of the line.
Yet this command shows all lines with 'd' being the first char which is not making sense.
Code:
ls -l | grep ^dZ*
The 'Z' is not even on any of the lines. Is it because the asterisk looks for zero or more Z's? Is that the same why 'grep ^d*' grabs zero or more d's in which case any character would return TRUE to the match?
A little off-topic, but a suggestion that may help in the future:
Whenever you use a regular expression at the command line, I highly recommend enclosing the regular expression in single quotes (').
Why? The asterisk, period, question mark, and other punctuation have different meanings between the shell and regular expression.
The shell can (and will) intercept the characters of your regular expression for filename expansion/globbing--which will mutilate your regular expression before it gets to the program you're launching and can cause very unexpected results.
If you enclose the regular expression in single quotes, the shell will not perform expansion/globbing. The regular expression will be passed to the program unmodified (with the single quotes removed).
It didn't happen in this case because there were no filenames that matched the wildcard. The shell tried to expand the asterisk, but because there were no matches, the shell passed the expression to grep unmodified.
I only post this suggestion because the tension between shell expansion and regular expressions caused me grief for a long, long time.
Hmm, seems like I'm getting a the expected output with the asterisk.
Actually, you're not. Look closely--specifically the first line of your output in each example. The example with the asterisk has this in its output:
Code:
total 40
whereas the other command doesn't. That output line certainly does not start with a 'd' and shouldn't be displayed if the regular expression is working properly.
EDIT: And by "properly," I mean producing the result that the OP expected (which was a mix-up between how the asterisk works in the shell and how it works in a regular expression)
That line of output gets through grep because the asterisk is being interpreted by grep as "zero or more of the preceding item." That basically turns the regular expression into "match any line that starts with a 'd' or not" -- which will match every line of output.
Last edited by Dark_Helmet; 09-15-2010 at 03:02 PM.
Reason: Clarification
does it display lines that do NOT include a 'd' as the first char of the line.
I took that to mean that in his case, the output excluded lines that began with "d."
That is why I wrote that I was getting the expected output. By that I meant that I was also getting all the lines that began with "d." No mystery here.
I'm reasonably good with regular expressions. I know perfectly well the difference between
$ ls -l | grep ^d*
and
ls -l | grep ^d.*
The second one will return all the lines that begin with "d," but also have at least one character more than "d."
I guess an "also" right before "display" in the quotation above would have made the question clearer to me.
I took that to mean that in his case, the output excluded lines that began with "d."
That is why I wrote that I was getting the expected output. By that I meant that I was also getting all the lines that began with "d." No mystery here.
I'm reasonably good with regular expressions. I know perfectly well the difference between
$ ls -l | grep ^d*
and
ls -l | grep ^d.*
The second one will return all the lines that begin with "d," but also have at least one character more than "d."
I guess an "also" right before "display" in the quotation above would have made the question clearer to me.
You didn't actually quote correctly, intentionally or not.
Quote:
However why is it that when I use
Code:
ls -l | grep ^d*
does it display lines that do NOT include a 'd' as the first char of the line.
To me, though not a native speaker, this indicates
that he expects to ONLY see lines beginning with a
"d", which means that he hasn't grasped the regex
syntax (yet) to mean that 0 "d"s matches, in other
words ANY line will.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.