LinuxQuestions.org
Visit Jeremy's Blog.
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-27-2003, 10:38 AM   #1
Piero
LQ Newbie
 
Registered: Jul 2003
Posts: 19

Rep: Reputation: 0
Cool How to filter files in files and files which are in a sub-directory with "grep"?


Eager to gain your favor!

Thanks!
 
Old 08-27-2003, 10:46 AM   #2
exodist
Senior Member
 
Registered: Aug 2003
Location: Portland, Oregon
Distribution: Gentoo
Posts: 1,372

Rep: Reputation: 46
uhm I think the only use of grep is to only show you lines of output with a the stuff you specify:

dmesg | grep hda

that will show you kernel output that mentions hda
 
Old 08-27-2003, 11:01 AM   #3
Piero
LQ Newbie
 
Registered: Jul 2003
Posts: 19

Original Poster
Rep: Reputation: 0
Mu......, I think you misunderstood my meaning.
 
Old 08-27-2003, 12:08 PM   #4
exodist
Senior Member
 
Registered: Aug 2003
Location: Portland, Oregon
Distribution: Gentoo
Posts: 1,372

Rep: Reputation: 46
I was simply stating the only usage of grep I know
 
Old 08-27-2003, 04:00 PM   #5
matronyx
LQ Newbie
 
Registered: Aug 2003
Location: Jamaica
Distribution: rh,mandrake
Posts: 6

Rep: Reputation: 0
You need to be more specific about what you are trying to accomplish.
Do not assume that we have the same thought.

Now say you have a directory with various files that are text based, and you need to search in all those specific files for a certain pattern, quickly.

for i in `ls -1 /dir/*.txt`;do cat $i | grep "pattern";done

The above command looks in the dir directory for all files ending with .txt extension, places them in a list in memory, then for each file found, it runs the cat command on it.
The output of the cat command is then piped ( | ) to grep, which searches for pattern specified.

Alternatively, if it is a long output list being generated you could send all of that information to a separate file to be later parsed like so:

for i in `ls -1 /dir/*.txt`;do cat $i | grep "pattern" >> resultfile.txt;done

Are we speaking the same language?
 
Old 08-28-2003, 03:52 AM   #6
Piero
LQ Newbie
 
Registered: Jul 2003
Posts: 19

Original Poster
Rep: Reputation: 0
Otherwise, I should put it this way.
I want to find the files whose content include the string "*.png".
And I don't exactly know which directory the files belong. So I should search the files in all the files in root "/".
Now I have no idea how to use "grep" in this way.

Can you catch my idea?
Thanks for your patience, zest and kindness.
 
Old 08-28-2003, 04:04 AM   #7
Bebo
Member
 
Registered: Jul 2003
Location: Göteborg
Distribution: Arch Linux (current)
Posts: 553

Rep: Reputation: 30
grep -R .png <dir>

You can also use the c option to just count the occurences of .png in a file. Then you just make an inverse grep on ":0" to get the files which have the string you searched for.

grep -Rc .png <dir> | grep -v :0

 
Old 08-28-2003, 04:11 AM   #8
footfrisbee
Member
 
Registered: Apr 2003
Distribution: Debian Sarge
Posts: 259

Rep: Reputation: 30
Yet another way to do this is to use find

$ find /dir -name '*.png'

This will recursively search all the subdirectories of /dir. To count the number of png files pipe that into wc,

$ find /dir -name '*.png' | wc -l
 
Old 08-28-2003, 05:21 AM   #9
Piero
LQ Newbie
 
Registered: Jul 2003
Posts: 19

Original Poster
Rep: Reputation: 0
I think Mr.Bebo is right.
And what Mr.footfrisbee said is only to find the file name including string "*.png".
But I want to find the file content including "*.png".

All in all.
Thank you all!
 
Old 08-29-2003, 03:38 AM   #10
footfrisbee
Member
 
Registered: Apr 2003
Distribution: Debian Sarge
Posts: 259

Rep: Reputation: 30
Oops, misread the question! Sorry!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
VMware & "directory of C header files" pacranch Amigo 12 05-14-2005 04:39 PM
add "Artist" and "Album Title" to mp3 files powah Linux - Software 2 04-05-2005 04:04 AM
"X-MS" cant open because "x-Multimedia System" cant access files at "smb&qu ponchy5 Linux - Networking 0 03-30-2004 12:18 AM
"batch files" in red hat, similar to .bat files in dos shycalais Linux - Newbie 2 10-12-2003 09:34 AM
Gnome "Search for Files..." not into hidden directory max74 Linux - Software 2 09-11-2003 07:53 PM


All times are GMT -5. The time now is 05:46 PM.

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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration