Home > Back-end >  Bash: Go up and down directories
Bash: Go up and down directories

Time:07-11

Dear stackoverflow community,

I am new to bash and I've got a problem regarding loops and directories (code below). So I am working in the opensmile directory and want to look for .wav files in the subdirectory opensmile/test-audio/. But if I change my directory in the "for" section to test-audio/*.wav, it probably could find the .wav-files but then the main-action does have access to the necessary config file "IS10_paraling.conf". Within the main-action the directories have to be written like after "-C", so without a "/" before the directory.

My loop works, if the wav files are inside the opensmile directory, but not inside a sub-directory. I would like to look for files in the subdirectory test-audio while the main-action still has access to all of the opensmile-directory.

So basically: How do I go up and down directories within a for loop?

Thank you very much in advance!

This works

#! /bin/bash
cd /usr/local/opensmile/

for f in *.wav; 
do
/usr/local/opensmile/build/progsrc/smilextract/SMILExtract -C config/is09-13/IS10_paraling.conf -I $f -D output/$f.csv ;
done

This does not work

#! /bin/bash
cd /usr/local/opensmile/

for f in test-audio/*.wav; 
do
/usr/local/opensmile/build/progsrc/smilextract/SMILExtract -C config/is09-13/IS10_paraling.conf -I $f -D output/$f.csv ;
done

CodePudding user response:

Saying "this does not work", doesn't tell us anything. What happens? Is there an error message?

Nevertheless, your question was "So basically: How do I go up and down directories within a for loop?"

If I'm tempted to go up and down directories within a loop, I'll do it in a subshell, so that I can be sure that the next time I enter the loop I'll be where I was originally. So I'll put all my commands in ( ).

#! /bin/bash
cd /usr/local/opensmile/
CONFIG=$PWD/config
OUTPUT=$PWD/output

for f in test-audio/*.wav; 
do
 (
  cd test-audio
/usr/local/opensmile/build/progsrc/smilextract/SMILExtract -C $CONFIG/is09-13/IS10_paraling.conf -I `basename $f` -D $OUTPUT/$f.csv
 )
done

though why one would need to to it for this case, I can't fathom

CodePudding user response:

Instead of using a for loop, could you use find for this:

find /usr/local/opensmile/ -type f -name "*.wav" -exec /usr/local/opensmile/build/progsrc/smilextract/SMILExtract -C config/is09-13/IS10_paraling.conf -I $1 -D output/$1.csv "{}" \;

  • Related