LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-14-2009, 06:12 PM   #1
goltoof
LQ Newbie
 
Registered: Sep 2009
Posts: 7

Rep: Reputation: 0
find


Mind numbing simple question. how do I search all files, or a directory of files for a single line of text using. For example, I wnat to find every file that contains the term "user-increase"

Please note that telling me to "man find" will not help me at all
 
Old 09-14-2009, 06:16 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Welcome to LQ!
Quote:
Please note that telling me to "man find" will not help me at all
Indeed.

"man grep" will :)



Cheers,
Tink
 
Old 09-14-2009, 06:17 PM   #3
bartonski
Member
 
Registered: Jul 2006
Location: Louisville, KY
Distribution: Fedora 12, Slackware, Debian, Ubuntu Karmic, FreeBSD 7.1
Posts: 443
Blog Entries: 1

Rep: Reputation: 47
The command that you're looking for is 'grep'.

Code:
grep "user-increase" ./*
will search for all files that contain the string "user-increase" in the current directory.

Code:
grep "user-increase" ./asdf/*
Will search all files in the directory asdf.
 
Old 09-14-2009, 06:19 PM   #4
mrrangerman
Member
 
Registered: Oct 2007
Location: MI
Distribution: Debian Slackware
Posts: 528

Rep: Reputation: 59
You can use find, locate, ls and pipe it through grep

Code:
ls | grep user-increase
from with in the directory.

Look at sed also.
 
Old 09-14-2009, 06:37 PM   #5
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Quote:
Originally Posted by mrrangerman View Post
You can use find, locate, ls and pipe it through grep

Code:
ls | grep user-increase
from with in the directory.

Look at sed also.
Ummm ... no.

He wants files that contain "user-increase", not ones
that have it in their names.



Cheers,
Tink
 
Old 09-15-2009, 10:31 AM   #6
goltoof
LQ Newbie
 
Registered: Sep 2009
Posts: 7

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by bartonski View Post
The command that you're looking for is 'grep'.

Code:
grep "user-increase" ./*
will search for all files that contain the string "user-increase" in the current directory.

Code:
grep "user-increase" ./asdf/*
Will search all files in the directory asdf.


Thanks. Any way to search all of the files on the entire server? Not just a specified directory. The pickle here is that I don't know what directory the file containing the string is in.
 
Old 09-15-2009, 10:35 AM   #7
dwives
LQ Newbie
 
Registered: Sep 2009
Posts: 10

Rep: Reputation: 0
Either do the command at the root of the drive "/", or write it as:

ls / | grep "stringyouaresearching"

You can ls any directory by naming it explicitly. You could ls your etc directory from deep inside some other directory by using ls /etc, for example.

Edit: you will need to use the recursive switch to get ls to hit all the sub directories, as I just found in another relevant thread.

Edit edit: I am wrong, see this post about grep.

Last edited by dwives; 09-15-2009 at 11:14 AM.
 
Old 09-15-2009, 01:12 PM   #8
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Quote:
Originally Posted by goltoof View Post
Thanks. Any way to search all of the files on the entire server? Not just a specified directory. The pickle here is that I don't know what directory the file containing the string is in.
Of course.

man grep
for details.
 
Old 09-15-2009, 02:51 PM   #9
lutusp
Member
 
Registered: Sep 2009
Distribution: Fedora
Posts: 835

Rep: Reputation: 102Reputation: 102
Quote:
Originally Posted by goltoof View Post
Thanks. Any way to search all of the files on the entire server? Not just a specified directory. The pickle here is that I don't know what directory the file containing the string is in.
Yes. Like this (in a shell script):

Code:
#!/bin/sh

path="/"

search="my search string"

find $path -type f | while read filepath
do
   if grep -iqP "\b$search\b" "$filepath"
   then
      echo "String \"$search\" found in file \"$filepath\"."
   fi
done
NOTE: If there is any way to narrow the search, you would do well to use it, because the above search will take a very long time. Also, there are some directories you shouldn't enter and scan (and don't do this search as root!). You would be better off specifying "/home" as the search path.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
linux find to find files with multiple patterns subu_s Programming 6 12-15-2010 12:15 AM
Find with -exec argument - not giving proper output..how to find... hinetvenkat Linux - Server 4 01-25-2010 06:19 AM
Find/grep/wc command to find matching files, print filename and word count dbasch Linux - Newbie 10 09-14-2009 05:55 PM
Single find command to find multiple files? thok Linux - Newbie 7 01-31-2009 04:45 PM
Can`t find C-compiler in Debian ,or at least can`t find one that can make executables hemmelig Linux - Software 4 05-26-2008 03:07 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:11 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration