Linux - NewbieThis 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.
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.
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.)
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. (=
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.