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.
|
 |
07-02-2003, 09:52 AM
|
#1
|
LQ Newbie
Registered: Jun 2003
Location: Sunny South Florida
Distribution: Slackware 9.0
Posts: 18
Rep:
|
Help me with grep please
Hi. I have read the man page for grep many times and it is very difficult to understand.
Here is what I would like to do: Many times I find myself searching for an executable file and have a hard time finding it. For example.. I just installed the shiny new mozilla 1.4 and would like to find the executable file for it (I know where it is..this is an example so I may learn this point.)
if I do:
$ locate mozilla
I get a gazillion results. So what I would like to do is filter it down by piping it through grep. Idealy something that would narrow it down to executable files only. Also good would be if I could narrow it down to files and not directories. Another good thing I would like to do ..is if I could filter out some specific directories (ie.. /home and /mnt becasue there are alot of results from there, but I know what I'm looking for is not there.)
Any help would be appreciated, thanks.
|
|
|
07-02-2003, 10:08 AM
|
#2
|
Moderator
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417
|
not really the best way to go about it...
try "whereis mozilla" - should tell you fine.
if you do want to grep, all you need is something as simple as
locate mozilla | grep bin
|
|
|
07-02-2003, 10:50 AM
|
#3
|
Member
Registered: Sep 2002
Location: Tulsa, OK
Distribution: Slack, baby!
Posts: 349
Rep:
|
whereis gives a little more info than necessary for finding a binary, but certainly does the job. try which mozilla to simplify it.
As for grep, here's a quick rundown of how to use it. (I am using grep 2.4, so some options or commands may not work as expected if using an older version).
let's take the output of ls -1 /usr/lib as our test case. This will produce a plethora of results, so we can show some various methods of using grep.
To find a filename (library in this case) that STARTS will a certain letter (we'll use the letter 'm'), use:
ls -1 /usr/lib | grep ^m
To find a filename that ENDS will a certain letter (we'll use the letter 'p'), use:
ls -1 /usr/lib | grep p$
To find a filename that contains the word "png" (for libpng), use:
ls -1 /usr/lib | grep png
To find a filename that DOES NOT contain the word "png", use:
ls -1 /usr/lib | grep --invert-match png
now, let's take grep one step further, and I'll show you how to use it to parse the contents of files, instead of just filenames.
We're going to look through the files in /etc for our purposes here. The way that we do this is with a combination of find and grep.
Firstly, let's just list all the files in /etc:
find /etc -type f
In order to cause find to run a grep on each file, you'll have to use the -exec option. From -exec, grep is handled just like you would normally, except that, in order to pass in the filenames from find, you'll have to add '{}' ';' to the end of the -exec statement. (those are single quotes, not backticks (`))
Let me demonstrate:
We want to find all files in the /etc directory that contain the PATH= statement, so we can find where all the path is being set:
find /etc -type f -exec grep -sl "PATH=" '{}' ';'
The -s tells grep to suppress error messages on files it can't read, and the -l (lowercase L) tells grep to return the filename of a matching file.
Now, find all files in the /etc directory that DO NOT contain the PATH= statement:
find /etc -type f -exec grep -sL "PATH=" '{}' ';'
The capital L is just the reverse of the -l, to return non-matching files.
One more, then I'm done.
Now, all files in the /etc directory that DO contain the PATH= statement, so we can find where all the path is being set, plus print out the line of the file that matches (and the filename), so we know what is in the file:
find /etc -type f -exec grep -sH "PATH=" '{}' ';'
-H simply tells grep to print the filename and matching line.
I hope that this stuff helps you understand grep better!! man pages can certainly be intimidating, if you're not familiar with a command. Maybe once you get these things down, you'll be able to take another peek at the man page and it will make more sense. (=
TLD
|
|
|
07-02-2003, 02:12 PM
|
#4
|
LQ Newbie
Registered: Jun 2003
Location: Sunny South Florida
Distribution: Slackware 9.0
Posts: 18
Original Poster
Rep:
|
Thanks for the great answers guys! This thread gets a bookmark from me. Grep has to be one of the coolest programs in linux.
I don't know about other newbies but I sure feel cool when I type a long command into the command line and it works..

|
|
|
All times are GMT -5. The time now is 12:20 PM.
|
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
|
|