From owner-freebsd-questions Sat Dec 12 20:00:48 1998 Return-Path: Received: (from majordom@localhost) by hub.freebsd.org (8.8.8/8.8.8) id UAA07301 for freebsd-questions-outgoing; Sat, 12 Dec 1998 20:00:48 -0800 (PST) (envelope-from owner-freebsd-questions@FreeBSD.ORG) Received: from jason03.u.washington.edu (jason03.u.washington.edu [140.142.77.10]) by hub.freebsd.org (8.8.8/8.8.8) with ESMTP id UAA07296 for ; Sat, 12 Dec 1998 20:00:47 -0800 (PST) (envelope-from eroubinc@u.washington.edu) Received: from dante20.u.washington.edu (eroubinc@dante20.u.washington.edu [140.142.15.70]) by jason03.u.washington.edu (8.8.4+UW97.07/8.8.4+UW98.06) with ESMTP id UAA18456; Sat, 12 Dec 1998 20:00:45 -0800 Received: from localhost (eroubinc@localhost) by dante20.u.washington.edu (8.8.4+UW97.07/8.8.4+UW98.06) with ESMTP id UAA111328; Sat, 12 Dec 1998 20:00:44 -0800 Date: Sat, 12 Dec 1998 20:00:44 -0800 (PST) From: Evgeny Roubinchtein To: "James A. Taylor" cc: freebsd-questions@FreeBSD.ORG Subject: Re: Recursing directories? In-Reply-To: <199812111943.NAA05604@horton.iaces.com> Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII Sender: owner-freebsd-questions@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG On Fri, 11 Dec 1998, Paul T. Root wrote: >In a previous message, James A. Taylor said: >> First of all thanks to Paul for his help with the mv command. >> >> Is it possible to have a shell script recurse a directory tree? >> Still the same situation as my last email I have a directory tree >> with .shtml files. I want to recurse the directory tree renaming >> each .shtml to a .html file. Paul sent me the following script that >> allows me to mv all .shtml in a single directory: >> >> #!/bin/sh >> for i in *.shtml >> do >> j=`basename $i .shtml` >> mv $i $j.html >> done >> >> This script works and changes all of the .shtml in the current >> directory. Is their a way I can get the script to recurse my >> directory tree? > >sure, name it nos (or some such). > >#!/bin/sh > >for i in * >do > if [ -d $i ];then > cd $i > nos > cd .. > else > echo $i |grep ".shtml$" >/dev/null > if [ $? -eq 0 ]; then > j=`basename $i .shtml` > mv $i $j.html > fi > fi >done > > >Ok, that's a little crude but it works. >Be sure to have the script in your path and >be executable. Another solution along the same lines is: ---%<---- #!/bin/sh action() { # Put the code you would like to run in each subdir in the body of this # function (below this comment, but above the next "}") echo "I am in `pwd`" } make_dir_list() { unset mydirs for file in * ; do [ -d $file ] && mydirs="$mydirs $file" done echo $mydirs } traverse() { if [ $# -eq 0 ] ; then action return else # visit the root first (in pre-order) # can also move the action line to after the for loop to traverse in post-order, sort of action for subdir ; do cd $subdir traverse `make_dir_list` cd .. done fi } traverse `make_dir_list` -----%<--- -- Evgeny Roubinchtein, eroubinc@u.washington.edu ................... BOMB: Burn Out Memory Banks To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-questions" in the body of the message