Unix and Linux network configuration. Multiple network interfaces. Bridged NICs. High-availability network configurations.


Reviews of latest Unix and Linux software. Helpful tips for application support admins. Automating application support.


Disk partitioning, filesystems, directories, and files. Volume management, logical volumes, HA filesystems. Backups and disaster recovery.


Distributed server monitoring. Server performance and capacity planning. Monitoring applications, network status and user activity.

Commands & Shells

Cool Unix shell commands and options. Command-line tools and application. Things every Unix sysadmin needs to know.

Home » Commands & Shells

A simple text string search script

Submitted by on December 13, 2007 – 1:52 pm 4 Comments

Sometimes you need to locate a text string in various configuration files. You don’t know which files. You can just use “grep *”, but the output is rather messy and you can only search in the current directory level. The script below uses “find” to locate all files under the specified directory. The script will only search ASCII text files, thus saving you time by not scanning through huge binaries. The output is neat and easy to read.


if [ -f /tmp/search.out ]
	rm /tmp/search.out
	touch /tmp/search.out
	touch /tmp/search.out

echo "Enter search string: c"
read STR

if [ ! $STR ]
	echo "You must provide a search string."
	exit 1

echo "Enter directory to search: c"
read DIR

if [ $DIR ]
	if [ ! -d "$DIR" ]
		echo "$DIR does not exist."
		exit 1
	echo "You must specify search directory."
	exit 1

find "$DIR" -type f -print | while read FILE
	if [ `file $FILE | grep -ci ASCII` -eq 1 ]
		echo "Searching file ${i}: $FILE"
		cat /tmp/search.out
		if [ `fgrep -c "$STR" "$FILE"` -gt 0 ]
			echo "<$STR> found in $FILE" >> /tmp/search.out
		(( i = i + 1 ))

echo "Results in $i files in $DIR: "
echo ""
if [ -f /tmp/search.out ]
	more /tmp/search.out
	echo "Nothing found."

Print Friendly, PDF & Email


  • Boo Cookie says:

    Hi, I’m trying to develop a script that will, given the friend id from a form input, get the html of the persons myspace profile, and serch through it to get their default picture, and display name. What could I use to do this? And if it’s simple enough, could someone develop this for me?


  • Goe122 says:

    I want to make a site that people send me stuff they are selling ( its for my school project). I want to:

    1. Be able to put this stuff ( whether automatically or manually) into a database
    2. I want it to be able to be searched by 6 things (type of stuff, date added, dorm etc.)
    3. I the search to have filters so people can find the right stuff.

    I have looked at database tutorials but a lot of them don’t show how to have a decent search module. If someone can give me a good tutorial(s), or can tell me how to do simple ( I know this isn’t simple, but at least easy directions,), or a site that can allow you to make your own database-type situation ( with search as well). A custom site would be really good ( like Pipes!), but any help will do.

    No one sentence replies unless it is the best sentence you ever wrote. Give me some content.

  • Rishi says:

    I have a string of letters. (They happen to be DNA, but that’s irrelevant.) For analysis purposes, I need to replace the information at some of the sites. I need to do this based on their position, not the information in that position.

    I also need to ignore differences at other sites, so I can’t do a simple “wild card search and replace.” I remember using awk scripts to do something like this about a decade ago, but I don’t remember how.

    For example, if you wanted to take the following strings,


    and in each case, turn the 10th letter into an “N,” without changing any of the other letters, how would you do that? (The actual files are about 500 characters long, and will have about 30 position-specific replacements.)

    I’m also willing to use a web tool or other editor….MacOS or Unix terminal on a Mac.

  • Kevin says:

    I have 50 some html files.
    I’m looking for a utility which allows me to search in those files, locate a piece of information in there and then delete that row in the html.
    The best so far in searching tens of text files is the Actual Search & Replace at
    The limitation there is that I can’t delete the entire row. It only allows me to delete the word or phrase.
    Does anyone have any idea which utility tool might allow me to perform my desired task?

Leave a Reply

%d bloggers like this: