Linux - General This 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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
01-04-2007, 02:19 PM
|
#1
|
|
Member
Registered: Jun 2004
Location: South Africa
Distribution: Ubuntu
Posts: 120
Rep:
|
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
|
|
|
|
01-04-2007, 02:43 PM
|
#2
|
|
Guru
Registered: Aug 2003
Distribution: CentOS, OS X
Posts: 5,131
Rep: 
|
Since self-learning is the most effective way of learning, have a look at 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,
it makes some power already. Or if you just want to list stuff in certain directories, use 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.. 
Last edited by b0uncer; 01-04-2007 at 02:50 PM.
|
|
|
|
01-04-2007, 02:44 PM
|
#3
|
|
Guru
Registered: Aug 2003
Distribution: CentOS, OS X
Posts: 5,131
Rep: 
|
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)
Last edited by b0uncer; 01-04-2007 at 02:45 PM.
|
|
|
|
01-04-2007, 02:47 PM
|
#4
|
|
Member
Registered: Dec 2006
Location: Florida
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.
Regards,
Brandon
|
|
|
|
01-04-2007, 02:59 PM
|
#5
|
|
LQ Veteran
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,797
|
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.
Last edited by pixellany; 01-04-2007 at 03:06 PM.
|
|
|
|
01-04-2007, 03:00 PM
|
#6
|
|
Member
Registered: Sep 2006
Distribution: Windows .. MUAHAHAHA
Posts: 66
Rep:
|
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.
|
|
|
|
01-04-2007, 03:01 PM
|
#7
|
|
Member
Registered: Jun 2004
Location: South Africa
Distribution: Ubuntu
Posts: 120
Original Poster
Rep:
|
Thanks, will try it out.
|
|
|
|
01-04-2007, 03:04 PM
|
#8
|
|
LQ Addict
Registered: Jul 2002
Location: East Centra Illinois, USA
Distribution: Debian Squeeze
Posts: 5,572
|
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.
|
|
|
|
01-05-2007, 10:44 AM
|
#9
|
|
Member
Registered: Jun 2004
Location: South Africa
Distribution: Ubuntu
Posts: 120
Original Poster
Rep:
|
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.
Thanks again!
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 02:15 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|