LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 08-30-2012, 06:38 PM   #1
Rogue45
Member
 
Registered: Jun 2012
Posts: 47

Rep: Reputation: Disabled
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* ./
 
Old 08-30-2012, 06:53 PM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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

Last edited by David the H.; 08-30-2012 at 06:57 PM. Reason: as stated
 
Old 08-30-2012, 06:56 PM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,360

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
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.
 
Old 08-30-2012, 06:57 PM   #4
Rogue45
Member
 
Registered: Jun 2012
Posts: 47

Original Poster
Rep: Reputation: Disabled
I think i should be able to ls -1 mydirectory | grep ming*

still not working though
 
Old 08-30-2012, 07:06 PM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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
 
Old 08-30-2012, 07:09 PM   #6
KinnowGrower
Member
 
Registered: May 2008
Location: Toronto
Distribution: Centos && Debian
Posts: 347

Rep: Reputation: 34
Quote:
Originally Posted by Rogue45 View Post
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
 
Old 08-31-2012, 10:49 AM   #7
Rogue45
Member
 
Registered: Jun 2012
Posts: 47

Original Poster
Rep: Reputation: Disabled
@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
 
Old 08-31-2012, 01:36 PM   #8
salasi
Senior Member
 
Registered: Jul 2007
Location: Directly above centre of the earth, UK
Distribution: SuSE, plus some hopping
Posts: 4,070

Rep: Reputation: 897Reputation: 897Reputation: 897Reputation: 897Reputation: 897Reputation: 897Reputation: 897
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.)
 
Old 08-31-2012, 01:48 PM   #9
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
Quote:
Originally Posted by Rogue45 View Post
@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.
 
Old 08-31-2012, 04:19 PM   #10
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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.
 
Old 09-07-2012, 12:38 PM   #11
Rogue45
Member
 
Registered: Jun 2012
Posts: 47

Original Poster
Rep: Reputation: Disabled
I got it fellas based on David the H and KG's responses. Thanks for everyone's help.
 
  


Reply

Tags
bash, bash scripting, grep



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Listing file names side by side with directory names anon84 Linux - Newbie 3 04-03-2012 10:13 AM
[SOLVED] How to changing same file names by their directory names? bayaraa_u Linux - General 3 04-09-2010 08:26 AM
grep does not care about file names beginning with period. stf92 Linux - Newbie 15 06-04-2009 07:27 PM
Getting file names in a directory using C on linux. rajsun Programming 4 06-10-2005 12:47 AM
Force grep NOT to print file names smart_sagittari Linux - Newbie 5 04-25-2005 01:20 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:05 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration