[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!
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.
grep uses regular expressions, and in regex, "*" means "zero or more of the previous character", so you'll only be matching "min, ming, mingg, mingggggggg", etc.
In regex, you need to use "." to mean "any character". Also, you need to quote the pattern to protect it from shell globbing.
Code:
grep "ming.*" output.txt
Last edited by David the H.; 08-30-2012 at 06:57 PM.
Reason: as stated
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:
Code:
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
I think i should be able to ls -1 mydirectory | grep ming*
still not working though
This command will list all the file in 'mydirectory' and pipe to grep. And finally display only the files have name contained word 'ming'. It wont actually look at the file contents
@David the H. I want to search for patterns within file names. In my example i want to list any file name that begins with ming followed by any characters. I don't care about file content.
I just found out the asterisk was what screwed me over. I just needed ls -1 mydirectory | grep ming
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
Code:
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.)
@David the H. I want to search for patterns within file names. In my example i want to list any file name that begins with ming followed by any characters. I don't care about file content.
I just found out the asterisk was what screwed me over. I just needed ls -1 mydirectory | grep ming
why not just "ls mydirectory/ming*" ?
Using grep at all is overkill for a query like this.
Last edited by suicidaleggroll; 08-31-2012 at 01:49 PM.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.