From owner-freebsd-questions Mon Jan 27 00:46:50 1997 Return-Path: Received: (from root@localhost) by freefall.freebsd.org (8.8.5/8.8.5) id AAA26273 for questions-outgoing; Mon, 27 Jan 1997 00:46:50 -0800 (PST) Received: from foo.primenet.com (ip195.sjc.primenet.com [206.165.96.195]) by freefall.freebsd.org (8.8.5/8.8.5) with ESMTP id AAA26268 for ; Mon, 27 Jan 1997 00:46:47 -0800 (PST) Received: (from bkogawa@localhost) by foo.primenet.com (8.8.2/8.6.12) id UAA11606; Sun, 26 Jan 1997 20:41:52 -0800 (PST) Date: Sun, 26 Jan 1997 20:41:52 -0800 (PST) Message-Id: <199701270441.UAA11606@foo.primenet.com> To: robert@nanguo.chalmers.com.au Subject: Re: how do I get sed to search sundirs? Newsgroups: localhost.freebsd.questions References: <199701270326.NAA04282@nanguo.chalmers.com.au> From: "Bryan K. Ogawa" Cc: freebsd-questions@freebsd.org (bsd) X-Newsreader: NN version 6.5.0 #1 (NOV) Sender: owner-questions@freebsd.org X-Loop: FreeBSD.org Precedence: bulk In localhost.freebsd.questions you write: >I have a little sed script that will search through all files in a >directory, replacing one phrase with another, and writing it back. >How do I get it to step down through all the sub-directories as well? Hm. I would do it in perl, like this: #!/usr/bin/perl require "find.pl"; &find("."); sub wanted { if($_ =~ /\.html$/) { rename $_, "$_.bak"; open(IN, "$_.$$.bak"); open(OUT, ">$_"); while() { s/OLDSTRING/NEWSTRING/g; print OUT; } close(IN); close(OUT); } } This will do a search and replace of OLDSTRING with NEWSTRING with every file ending in .html in the tree starting with the current directory, first making a backup of each .html file to .html.bak . I have not tested this, though, so please take it with a grain of salt. If you want to call some script on every file in a directory (and on all of the subdirectories), you can also do this from perl: #!/usr/bin/perl require "find.pl"; &find("."); sub wanted { if (-f $_) { system("/path/to/script $_"); } } This will call /path/to/script once on every plain file in the tree rooted at . . I hope this helps. # Usage: # require "find.pl"; # # &find('/foo','/bar'); # # sub wanted { ... } # where wanted does whatever you want. $dir contains the # current directory name, and $_ the current filename within # that directory. $name contains "$dir/$_". You are cd'ed # to $dir when the function is called. The function may # set $prune to prune the tree. # >ta >bob >-- >Triple-W: P.O. Box 2003. Mackay. 4740 +61-0412-079025 >robert@chalmers.com.au for Whirled Peas http://www.chalmers.com.au >Location: Whitsunday Web Works. 21'7" S, 149'14" E. -- bryan k ogawa http://www.primenet.com/~bkogawa/