Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 12 Dec 1998 20:00:44 -0800 (PST)
From:      Evgeny Roubinchtein <eroubinc@u.washington.edu>
To:        "James A. Taylor" <jataylor@lundahl.com>
Cc:        freebsd-questions@FreeBSD.ORG
Subject:   Re: Recursing directories?
Message-ID:  <Pine.A41.4.05.9812121958030.100006-100000@dante20.u.washington.edu>
In-Reply-To: <199812111943.NAA05604@horton.iaces.com>

next in thread | previous in thread | raw e-mail | index | archive | help
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



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?Pine.A41.4.05.9812121958030.100006-100000>