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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Check contents of files for a string?

Status
Not open for further replies.

TheDust

Programmer
Aug 12, 2002
217
US
I'm brand new to UNIX scripting and I'm a little lost here. I am trying to search my server for the instances of a certain string within the files.

I'm using this command but when I use it it never returns any result. It just runs forever:

grep -c "stringhere"

I run that from inside the folder I want to search. What am I doing wrong?
 
OK... how would you search all files on the server? I've tried this but it seems to run forever without stop:

grep -l -r "stringhere" /*

Also, I get many errors like "grep: /dev/sdbv15: No such device or address" and "grep: warning: /awstatsicons/icon: recursive directory loop". Anyone know what I might be doing wrong?
 
Try this...
Code:
find / -type f -exec grep -c "stringhere" \{\} \; 2>/dev/null
That will search all normal files (skipping directories and such), looking for "stringhere". It will throw away any error messages cause by files and directories you don't have access to.

Hope this helps.
 
Tried that command and let it run for an hour... it just hangs and doesn't return a result. Not sure what I'm doing wrong... I am trying to search the server and have it list all files that contain the string "inbox" (no quotes) on the server. I'm trying to nail down where qmail creates its default configuration when creating a new mailbox. Anyone have any ideas on the command I might be able to use to get this listing?
 
Another alternative would be to execute the find with the "-print" option. And you can "Tee" the output to a text file. That way you can see that there is activity. And when it's done, you can grep the resulting text file for the string you're searching for. It's kind of the long way around, but at least you'll know somethings going on. (you may see a whole bunch of permission denied errors - but just ignore them and let it run) The resulting text file will be huge (hundreds of thousands of lines long - 10-30 MB in size or more). Don't edit it, just use grep to search the resulting text file for what you're looking for and redirect the output to a second smaller file.

find / -type f -print | tee resulting_file.txt

Then:

grep mystring resulting_file.txt > smaller_file.txt

or

grep mystring resulting_file.txt | tee smaller_file.txt

Like I said, it's the long way around, but at least you have the comfort of knowing somethings happening.
 
Save time with selectivity.I use a shell script which prompts for a search string, makes a list of candidate files and only searches those.

# Prompt for search string variable:
clear
read string
# If your files are in certain subdirectories, limit
# your search. Make a list of candidate files only, e.g.:
cd /
find /usr/src -type f -print|sort > list
find /usr2/src -type f -print|sort >> list
# Narrow down your list using include/exclude rules' e.g.:
grep '.s' list > list2;mv list2 list
grep -v junk list > list2;mv list2 list
# Search your narrowed down list for the search string
rm list2
for i in `cat list`
do
grep $string $i >> list2
done
# /list2 will contain your search results.
 
imFrank, that will only look for the search string in the file names, not the files themselves. The [tt]-print[/tt] option just prints the filenames.

Using the same idea though, how about something like...
Code:
#!/bin/ksh

find / -type f -print 2>/dev/null | while read FILE
do
    print -n "Searching: ${FILE} "

    FOUND=$(grep -c inbox ${FILE})

    if (( $FOUND ))
    then
        print "<--- ${FOUND} occurrences found here!!!"
    else
        print
    fi
done
Hope this helps.
 
The following is from the link to the Configuring Qmail site I gave above...

By default, Qmail is set up to put incoming messages into [tt]$HOME/Mailbox[/tt]. So a user [tt]joe[/tt] would get his incoming messages stored in [tt]/home/joe/Mailbox[/tt]. I don't like this default location, I want messages to go into [tt]$HOME/mail/inbox[/tt] instead, so I made these changes. If you want to use the default [tt]$HOME/Mailbox[/tt] file, you can ignore this section. Note that Qmail also supports a Maildir format, read the Qmail docs for more information.

[tt]/etc/qmail/dot-qmail

| dot-forward .forward
./mail/inbox[/tt]

The [tt]dot-forward .forward[/tt] command tells Qmail to forward emails if a .forward file exists in a user's home directory.

The [tt]./mail/inbox[/tt] line tells Qmail to store incoming messages in [tt]$HOME/mail/inbox[/tt], so if your username was joe, your incoming messages would go into [tt]/home/joe/mail/inbox[/tt].
[tt]
/etc/profile.d/qmail.csh
/etc/profile.d/qmail.sh
[/tt]
These files define the MAIL and MAILDIR environment variables. Some programs, such as mailx and the shell, look at these variables to see where your inbox is. Change all occurrences of [tt]$HOME/Mailbox[/tt] to [tt]$HOME/mail/inbox[/tt].

You may also want to check your [tt]/etc/profile[/tt] file to see if it defines MAIL. If so, you can remove the declaration because they are overwritten by the declarations in [tt]/etc/profile.d/qmail.csh[/tt] or [tt]/etc/profile.d/qmail.sh[/tt] (depending on which shell you are using).
 
Perhaps you may refine your search path.
Do you need to search /home/* ?
If yes, do you need to search /usr/* ?

Can you reduce the search to a certain filename-pattern like "*.txt" or the file-size or the age of the file?

Did you monitor 'find' or 'grep' to see, if it's really running for so long, or whether it just sits there, waiting for some input?

seeking a job as java-programmer in Berlin:
 
If you need to do ths sort of thing regularly, you might think about using Glimpse.

[tt]glimpse[/tt] is to [tt]grep[/tt] as [tt]locate[/tt] is to [tt]find[/tt].
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top