LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
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


Reply
  Search this Thread
Old 01-04-2007, 02:19 PM   #1
Last Attacker
Member
 
Registered: Jun 2004
Location: South Africa
Distribution: Ubuntu
Posts: 120

Rep: Reputation: 15
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
 
Old 01-04-2007, 02:43 PM   #2
b0uncer
LQ Guru
 
Registered: Aug 2003
Distribution: CentOS, OS X
Posts: 5,131

Rep: Reputation: Disabled
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..

Last edited by b0uncer; 01-04-2007 at 02:50 PM.
 
Old 01-04-2007, 02:44 PM   #3
b0uncer
LQ Guru
 
Registered: Aug 2003
Distribution: CentOS, OS X
Posts: 5,131

Rep: Reputation: Disabled
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.
 
Old 01-04-2007, 02:47 PM   #4
Robert Diggs
Member
 
Registered: Dec 2006
Location: Florida
Distribution: The ones that come in magazines and books.
Posts: 136

Rep: Reputation: 15
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
 
Old 01-04-2007, 02:59 PM   #5
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
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.
 
Old 01-04-2007, 03:00 PM   #6
MOS JEFF-INITELY
Member
 
Registered: Sep 2006
Distribution: Windows .. MUAHAHAHA
Posts: 66

Rep: Reputation: 15
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.
 
Old 01-04-2007, 03:01 PM   #7
Last Attacker
Member
 
Registered: Jun 2004
Location: South Africa
Distribution: Ubuntu
Posts: 120

Original Poster
Rep: Reputation: 15
Thanks, will try it out.
 
Old 01-04-2007, 03:04 PM   #8
bigrigdriver
LQ Addict
 
Registered: Jul 2002
Location: East Centra Illinois, USA
Distribution: Debian stable
Posts: 5,908

Rep: Reputation: 356Reputation: 356Reputation: 356Reputation: 356
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.
 
Old 01-05-2007, 10:44 AM   #9
Last Attacker
Member
 
Registered: Jun 2004
Location: South Africa
Distribution: Ubuntu
Posts: 120

Original Poster
Rep: Reputation: 15
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!
 
  


Reply

Tags
bash, contents, file, find



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
synchronize contents two files with a bash script paul_mat Linux - Software 5 01-17-2007 06:31 AM
bash variable loses contents in mysql statement thedude2010 Programming 3 06-02-2006 04:15 AM
Bash script to compare dir contents Boffy Programming 2 08-02-2005 06:08 AM
Retrieving variables from contents of files using awk or any other bash tool genderbender Programming 8 01-21-2005 03:27 AM
get file contents with newlines into bash variable otoomet Linux - Software 2 01-06-2005 01:23 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 05:00 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration