[SOLVED] How do i grep file names in current directory?
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!
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.
Introduction to Linux - A Hands on Guide
This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.
Click Here to receive this Complete Guide absolutely free.
Perhaps we need to clarify the request. Do you want to search for certain patterns in file names? Or to grep for patterns inside only certain files?
My previous post addresses the first, while chrism01 addresses the latter. In essence you can use globbing to select a list of files for grep to operate on.
But also have a look at the grep man page. There's a whole section on input file control.
Rogue45's technique is a quick&dirty way to search for certain filenames. But really, for the most part you should not try parsing ls. Just use globbing again, with echo or printf. You only need ls if you want to see long-format info:
echo ming* #prints a space-separated list
printf '%s\n' ming* #prints one filename per line
ls -l ming* #prints the long form info of the selected files
Last edited by David the H.; 08-30-2012 at 07:26 PM.
Reason: fixed careless typos
If you are happy to get files that have 'ming' anywhere in the file name (ming1234, 1234ming and 12ming34) and you are happy with the limitations of locate (you have to be running updatedb, which you probably have running from a cron job in most distros, and you only get files that were present the last time that updatedb ran) then there is an almost trivial solution
locate directoryname | grep -i ming
(the -i ignores case, if you know that you only want exactly 'Ming' you could, for example, 'grep Ming'). Also note that 'directoryname' could be an element anywhere in the path, or even an element thereof, so it could be 'home' for example and you'll get matching files within the home directories of all users.
(PS You should never use the word 'trivial' in an answer to this kind of question. Ho, hum.)
For that matter why don't you just use the solutions I gave you before? Use "echo ming*", or "printf '%s\n' ming*" (if you need them one per line). External commands like ls and grep are wholly superfluous when you only need to list out the matching filenames.
Also read through the links I gave on globbing and find to learn how to do more advanced searches.