-   Linux - General (
-   -   Finding files by contents in BASH (

Last Attacker 01-04-2007 02:19 PM

Finding files by contents in BASH
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.

Help would be appreciated!


b0uncer 01-04-2007 02:43 PM

Since self-learning is the most effective way of learning, have a look at grep:

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,

man find
it makes some power already. Or if you just want to list stuff in certain directories, use ls:

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):

grep --with-filename iptables ./*
if it doesn't work, read the manpages, I may have made a mistake, I'm so tired.. :)

b0uncer 01-04-2007 02:44 PM

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)

Robert Diggs 01-04-2007 02:47 PM


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.



pixellany 01-04-2007 02:59 PM

Bash Guide for beginners by Machtelt Garrels---at

<<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.

MOS JEFF-INITELY 01-04-2007 03:00 PM

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.

Last Attacker 01-04-2007 03:01 PM

Thanks, will try it out.

bigrigdriver 01-04-2007 03:04 PM

find /etc -type f | xargs grep 'iptables'

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.

Last Attacker 01-05-2007 10:44 AM

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! :p

It worked BTW, I finally found what I was looking for, fortunately I could still remember my REGEX stuff from university.

Thanks again!

All times are GMT -5. The time now is 08:42 PM.