Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then 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.
Hey guys.
Sorry for the "newbie" question but...
How do you find files by its contents in BASH command line?
Lets say you want to find all the files containing "iptables", then it would list the files containing it.
Since self-learning is the most effective way of learning, have a look at grep:
Code:
man grep
it may have what you're asking for, with the right option which makes it not print the line in the file which has the asked word(s) inside, but rather the filename. Added with find to list the wanted files by type, date or something else,
Code:
man find
it makes some power already. Or if you just want to list stuff in certain directories, use ls:
Code:
man ls
Use find or ls to create a list of files, and then grep to "go through them" and report the filenames which have the wanted characters inside..
An example to look for iptables inside the files that reside in the current directory (just off the top of my head, I didn't test it yet):
Code:
grep --with-filename iptables ./*
if it doesn't work, read the manpages, I may have made a mistake, I'm so tired..
EDIT: sorry, it happened again; clicking on the post button leads to having browser think for a while and resulting in it actually posting the same post multiple times (yesterday it posted 5 identical posts at once...darn)
Distribution: The ones that come in magazines and books.
Posts: 136
Rep:
Hey,
Alternately, pick up a book. I got one called Suse Linux Bible. It covers GREP and a whole shitload of other commands. Very helpful. HOwever, if you have problems, it doesn't cover how to fix them. Just tells you how to do things the right way the first time.
Bash Guide for beginners by Machtelt Garrels---at tldp.org
<<edit: I had something here that did not work---sorry>>
To find by content more globally, first plan a nice evening out to dinner--you are going to be waiting a while. Also, look at the GUI tools--they may be easier than figuring out the bash equivalents.
if your gonna get a book I recommend RUTE .. you can download it for free off the net just google it. This book is made as a general reference to *nix systems rather than getting one specific for SUSE when you might be using Fedora Core. Very good and complete.
where:
find is what you want to do,
/etc is the directory to search in,
-type f is a regular file (as opposed to device or other type),
| to pipe the command through xargs and grep,
grep for pattern matching,
'iptables' the pattern to match.
su to root to use this command, because many files are closed to normal user, unless you are searching in your home folder.
Oh thanks bigrigdriver!
I have been wondering and struggling about that problem for months (well last year when I did Linux projects) and now when I wanted to do something in Linux again, I forgot where to look.
I want to revise on the Linux console commands, etc. as I want to work in Linux again, Windows just makes a man lazy!
It worked BTW, I finally found what I was looking for, fortunately I could still remember my REGEX stuff from university.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.