LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to filter files in files and files which are in a sub-directory with "grep"? (http://www.linuxquestions.org/questions/linux-newbie-8/how-to-filter-files-in-files-and-files-which-are-in-a-sub-directory-with-grep-86255/)

Piero 08-27-2003 09:38 AM

How to filter files in files and files which are in a sub-directory with "grep"?
 
Eager to gain your favor!

Thanks!

exodist 08-27-2003 09:46 AM

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

Piero 08-27-2003 10:01 AM

Mu......, I think you misunderstood my meaning.

exodist 08-27-2003 11:08 AM

I was simply stating the only usage of grep I know

matronyx 08-27-2003 03:00 PM

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?

Piero 08-28-2003 02:52 AM

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.

Bebo 08-28-2003 03:04 AM

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

:)

footfrisbee 08-28-2003 03:11 AM

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

Piero 08-28-2003 04:21 AM

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!

footfrisbee 08-29-2003 02:38 AM

Oops, misread the question! Sorry!


All times are GMT -5. The time now is 03:27 AM.