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.
Greetings, I just started learning the Linux command-line, I am reading a book on it and today I learned a little about pipelines, redirection and a little about grep. I tried the example out of the book and it worked as expected but as I tried an example for myself, the results were not as I had expected. In the following I tried to filter out the results of the “ls” command with “grep” to only show files containing the characters '.c',
In my folder /home/linus/prog where I keep my C programming source-files, amongst other things, I ran the following command:
First off I am sure there are dozens of more efficient and better ways to accomplish this simple task but I am trying to learn the basics, in particular "grep". Anyway why does booksrc,booksrc.zip, project, and testc all show up? Does grep ignore non-alphabetic characters? Or maybe '.' is some wild card I am unaware of?
grep uses regular expressions for pattern matching, and in regex, a '.' has the special meaning of 'any single character'.
To get the list you want, use a bracket expression to isolate the character and treat it as a literal value, or backslash it, or use the '-F' option to force it to be treated as a literal string.
Code:
ls | grep '[.]c'
ls | grep '\.c'
ls | grep -F '.c'
Also, don't forget to always quote your expressions. This is vitally important when working with strings that can contain shell-reserved characters.
In any case, while the above may be good for practice, file listing and processing should almost always be done with globbing patterns instead, or find, if you need more advanced matching (and be sure to use null separators when you do).
You should never depend on parsing ls for lists of files or their metadata.
Last edited by David the H.; 03-20-2013 at 05:57 AM.
Reason: fixt links
David the H, clocker, thanks for the answer and especially David for the information and the links you've provided. I've bookmarked that website and will use it for further reference.
Except that (sorry to say) clocker's command is completely wrong, for the reasons explained before. grep does not support simple globbing patterns like that, only regex or fixed strings.
And again, you shouldn't be using ls for things like this anyway.
Other than that, good for you. Greg's wiki is perhaps the very best scripting site I've ever found, with bash-hackers.org coming in a close second.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.