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

executing command recusive down directories 1

Status
Not open for further replies.

Larshg

Programmer
Mar 1, 2001
187
DK
Hi

I need to make a script that can go throu a file system an in every dir execute a command.

the script should have 3 parameters $1 the number of levels I need to gå throu $2 directory where it should start and $3 the command to execute(could be put into the script).

ex.
dirthrou.sh 2 /opt "ls > /ls_on2_levels_in_opt.txt"
the out put should be an ls in /opt, /opt/bin and /opt/var ect. but not in /opt/var/test


I made a script that takes 1 level under - but this needs to be more recusive, and go throu X levels

set -A dirs `ls -lrt |grep "^d"|awk '{print $9}'
length=${#dirs[*]}
#echo $length
loop=0

while [ $loop -lt $length ]
do
echo ${dirs[$loop]}
cd ${dirs[$loop]}
ls > test
cd ..
(( loop=$loop+1 ))
done


Hope some one can help.
 
look at man find

the find command allows you to search for a particular type of file (e.g. directory), and for each "record" found to execute a command on it.

You could also call the script with a parameter, for each directory found.

e.g.

first call
./script "firstpath"

within the script

...
./script "newly found directory"
...




Regards

Frederico Fonseca
SysSoft Integrated Ltd
 
In the
Code:
 man find
output take a look at the following options: -level, -type, -exec.

Hope This Help
PH.
 
I know that I can use the find / -type d

But that goes throu all the directories - and I cant find the -level (find: bad option -level), I've looked throu the man pages but I can't find anything that limits the number of levels it goes throu.

Regards
Larshg
 
What operating system?

On Linux -maxdepth is the find option you want.

On Solaris there doesn't seem to be an equivalent option, but you can limit it to the current directory using -prune.

Annihilannic.
 
Oh I working on a compaq T64 unix server.

I dont have any option for find that limits the number of levels that I go throu.

/Larshg
 
To limit number of levels to 3 for example you could use....

find /opt -type d -print|awk '{d=split($0,a,&quot;/&quot;);if(--d<=3) print $0}'


So your script might look like.....

#!/bin/sh
DEPTH=$1
START=$2
COMMAND=$3
ADD_DEPTH=`echo $START|awk '{gsub(&quot;[^/]&quot;,&quot;&quot;);print length}'`
REAL_DEPTH=`expr $DEPTH + $ADD_DEPTH`
find $START -type d -print|awk '{d=split($0,a,&quot;/&quot;);if(--d<='$REAL_DEPTH') print $0}'|while read DIR
do
cd $DIR && $COMMAND
done

 
Ygor, what are you thinking about this:|code]
case $2 in
/*) START=$2;;
*) START=`pwd`/$2;;
esac[/code]



Hope This Help
PH.
 
For simplicity, my example only works with absolute paths but if you're going to allow relative paths then you should also cater for any extra trailing / on the path too...

case $2 in
/*) START=${2%/};;
*) START=`pwd`/${2%/};;
esac


 
Why not make the script recursive (i.e. call itself):

Code:
#!/bin/ksh
# $1 is direcotry
# $2 is counter
# $3 is command

cd $1
if [[ &quot;$2&quot; -gt 0 ]]
then
        for dir in $(ls -l | awk '/^d/ {print $NF}' )
        do
                /usr/local/bin/dirthrou.sh $dir $(( $2 - 1 )) &quot;$3&quot;
        done
fi
$3

This presumes the script is in /usr/local/bin. And of course doesn't include any error checking.

Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top