LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 07-02-2003, 09:52 AM   #1
drfrankie
LQ Newbie
 
Registered: Jun 2003
Location: Sunny South Florida
Distribution: Slackware 9.0
Posts: 18

Rep: Reputation: 0
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.
 
Old 07-02-2003, 10:08 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
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
 
Old 07-02-2003, 10:50 AM   #3
TheLinuxDuck
Member
 
Registered: Sep 2002
Location: Tulsa, OK
Distribution: Slack, baby!
Posts: 349

Rep: Reputation: 33
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
 
Old 07-02-2003, 02:12 PM   #4
drfrankie
LQ Newbie
 
Registered: Jun 2003
Location: Sunny South Florida
Distribution: Slackware 9.0
Posts: 18

Original Poster
Rep: Reputation: 0
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..



 
  


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
grep ?? can grep us variables? DaFrEQ Linux - Software 4 09-14-2005 12:22 PM
help on grep!!!! sanjith11 Programming 5 01-20-2005 05:43 PM
What does rpm -qa |grep th* (as compared to rpm -qa |grep th) display? davidas Linux - Newbie 2 03-18-2004 01:35 AM
"Undeleting" data using grep, but get "grep: memory exhausted" error SammyK Linux - Software 2 03-13-2004 03:11 PM
ps -ef|grep -v root|grep apache<<result maelstrombob Linux - Newbie 1 09-24-2003 11:38 AM

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

All times are GMT -5. The time now is 03:51 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