![]() |
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! Thanks |
Since self-learning is the most effective way of learning, have a look at grep:
Code:
man grepCode:
man findCode:
man lsAn 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 ./* |
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)
|
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. Regards, Brandon |
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.
|
Thanks, will try it out.
|
find /etc -type f | xargs grep 'iptables'
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! :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 12:39 PM. |