LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How do i grep file names in current directory? (http://www.linuxquestions.org/questions/linux-newbie-8/how-do-i-grep-file-names-in-current-directory-4175424860/)

Rogue45 08-30-2012 06:38 PM

How do i grep file names in current directory?
 
Ok i can grep within a file,
grep ming* output.txt

and i get any lines with string ming followed by anything

now i want to search a directory with a bunch of files
How do i do this with grep?

it seems like this should work but it doesn't
grep ming* ./

David the H. 08-30-2012 06:53 PM

For the most part, not "grep", but simple globbing.

If you need more advanced matching ability, use find

http://mywiki.wooledge.org/UsingFind
http://www.grymoire.com/Unix/Find.html

Edit: BTW, this is wrong:
Code:

grep ming* output.txt
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

chrism01 08-30-2012 06:56 PM

I'd think
Code:

cd dir
grep 'ming*' *

# or
grep 'ming*' ./dirname/*

or see see eg -d switch here http://linux.die.net/man/1/grep and/or -r (recursive) and/or ...

BTW, usually a good idea to put single quotes around regex patterns so they are processed by grep, not the shell.

Rogue45 08-30-2012 06:57 PM

I think i should be able to ls -1 mydirectory | grep ming*

still not working though:(

David the H. 08-30-2012 07:06 PM

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


KinnowGrower 08-30-2012 07:09 PM

Quote:

Originally Posted by Rogue45 (Post 4768682)
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

Rogue45 08-31-2012 10:49 AM

@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

salasi 08-31-2012 01:36 PM

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.)

suicidaleggroll 08-31-2012 01:48 PM

Quote:

Originally Posted by Rogue45 (Post 4769279)
@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.

David the H. 08-31-2012 04:19 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.

Rogue45 09-07-2012 12:38 PM

I got it fellas based on David the H and KG's responses. Thanks for everyone's help.


All times are GMT -5. The time now is 03:04 PM.