Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Listing files containing a string

Status
Not open for further replies.

lanoak

Technical User
Dec 10, 2002
1
GB
Hi,

I'm quite new to unix so apologies if this is a blatantly simple command that I just haven't found yet (which I expect it is). I need to list the files within a directory that contain a certain string of information. Is there any quick command line way of doing this? I've tried using ll and piping throught to cat & grep, but with no success. Any help would be appreciated.
 
If it's only the filenames you need, try:

grep '<your string>' * | cut -f1 -d':'

within the directory concerned. HTH.
 
Ok.

Let's say you have a directory with some file in -- you can list all of these files like this:

ls -l *.txt

what that does is search for all the filenames that match the pattern *.txt and give them to the ls program -- which lists them out, in the format specified by the '-l' option

so -- you need to pass grep a list of file-names and the string you want to search for in them

e.g. -- looking for the string 'hello' in the *.txt files

grep hello *.txt

this prints out the lines in the *.txt files containing the string 'hello'

to print out just the file-names you need to tell grep that's what you want, like this:

grep -l hello *.txt

Mike

&quot;Experience is the comb that Nature gives us, after we are bald.&quot;

Is that a haiku?
I never could get the hang
of writing those things.
 
oops, sorry ken Mike

&quot;Experience is the comb that Nature gives us, after we are bald.&quot;

Is that a haiku?
I never could get the hang
of writing those things.
 
No problem, Mike. As it happens I'd forgotten all about the -l option to grep, so it's a good catch! Cheers.
 
This one is free :)

We wrote this searchDir script for our Applications people so they could search files in a directory for a string:

text=$1
for file in `ls -atr | sed 's/^\.//'`
do
#echo &quot;Searching...&quot; $file
strings $file | grep $text > /dev/null
if [ $? -eq 0 ]; then
ls -al $file
strings $file | grep $text
echo &quot;&quot;
fi
done

So, you would just run searchDir sed

It will search the current directory and return information about the file containing the string and the line the string is on. Here's sample output:

$ searchdir sed
-rwxr-xr-x 1 user01 staff 203 Mar 19 2002 searchdir
for file in `ls -atr | sed 's/^\.//'`
 
another handy additional tip is to use grep in conjunction with the find command so that you can also search subdirs. Obviously you should be careful from where you initiate this so you don't scan giant portions of your drive.

Code:
grep string `find . -type f`

This doesn't work with an infinite number of files, so you might have to do something like this if you have thousands of files that you want to look through:

Code:
find . -type f -exec grep -l string {} \;

...and a hundred other ways. --
Andy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top