Joana Carvalho
Joana Carvalho

Reputation: 99

run a script in multiple folders in bash

I have a folder Cabo_Verde, and inside this folder I have several folders (001 to 300) with several files each, like this:

Filename: Cabo_verde

     001
     2008.001.00.00.CVBR1.LHZ.SAC
     2008.001.00.00.CVBR2.LHZ.SAC
     ...

     002
     2008.002.00.00.CVBR1.LHZ.SAC
     2008.002.00.00.CVBR2.LHZ.SAC
     ...

I want to run a script in each folder and this is what I did:

for dir in `ls $Cabo_verde`;
do
   for subdir in `ls $Cabo_Verde/$dir`;
   do
      $(for file in *SAC; do
sac <<EOF
echo on
read $file
chnhdr KCMPNM LHZ
write over
quit
EOF
done)
done;
done

In the end I got

ls:cannot access /001: No such file or directory
ls:cannot access /002: No such file or directory

Can anyone help me please?

Thanks

Upvotes: 0

Views: 1611

Answers (1)

just somebody
just somebody

Reputation: 19247

as a reliability aid, i suggest writing and running all your scripts with set -u, your parameter name typo would be immediately obvious.

you say you want to run a script in each directory, but is it actually true? anyway...

you can either nest two loops like this:

for d in Cabo_Verde/*/; do
  cd $d
  for f in *.SAC; do
    sac ... $f
  done
done

or you can do it with a single loop like this:

for f in Cabo_Verde/*/*.SAC; do
  cd ${f%/*}
  sac ... $f
done

of course, you don't need any loops at all:

find Cabo_Verde -name \*.SAC -execdir tool

where tool is a script containing sac ... < $1

Upvotes: 1

Related Questions