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 |
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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
|
|
08-27-2003, 09:38 AM
|
#1
|
LQ Newbie
Registered: Jul 2003
Posts: 19
Rep:
|
How to filter files in files and files which are in a sub-directory with "grep"?
Eager to gain your favor!
Thanks!
|
|
|
08-27-2003, 09:46 AM
|
#2
|
Senior Member
Registered: Aug 2003
Location: Portland, Oregon
Distribution: Arch
Posts: 1,374
Rep:
|
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
|
|
|
08-27-2003, 10:01 AM
|
#3
|
LQ Newbie
Registered: Jul 2003
Posts: 19
Original Poster
Rep:
|
Mu......, I think you misunderstood my meaning.
|
|
|
08-27-2003, 11:08 AM
|
#4
|
Senior Member
Registered: Aug 2003
Location: Portland, Oregon
Distribution: Arch
Posts: 1,374
Rep:
|
I was simply stating the only usage of grep I know
|
|
|
08-27-2003, 03:00 PM
|
#5
|
LQ Newbie
Registered: Aug 2003
Location: Jamaica
Distribution: rh,mandrake
Posts: 6
Rep:
|
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?
|
|
|
08-28-2003, 02:52 AM
|
#6
|
LQ Newbie
Registered: Jul 2003
Posts: 19
Original Poster
Rep:
|
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.
|
|
|
08-28-2003, 03:04 AM
|
#7
|
Member
Registered: Jul 2003
Location: Göteborg
Distribution: Arch Linux (current)
Posts: 553
Rep:
|
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
|
|
|
08-28-2003, 03:11 AM
|
#8
|
Member
Registered: Apr 2003
Distribution: Debian Sarge
Posts: 259
Rep:
|
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
|
|
|
08-28-2003, 04:21 AM
|
#9
|
LQ Newbie
Registered: Jul 2003
Posts: 19
Original Poster
Rep:
|
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!
|
|
|
08-29-2003, 02:38 AM
|
#10
|
Member
Registered: Apr 2003
Distribution: Debian Sarge
Posts: 259
Rep:
|
Oops, misread the question! Sorry!
|
|
|
All times are GMT -5. The time now is 12:11 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|