0

I have a series of folders containing multiple files:

-Scenario_1
---- file_1.txt
---- file_2.txt
---- file_3.txt
-Scenario_2
---- file_1.txt
---- file_2.txt
---- file_3.txt

I wish to iterate over each folder, iterate through the files and apply a function to all of these files

build () {
    for folder in ./scenarios/*; do

        echo "Working on" $folder

        `function` (space separated list of files in the folder) -o $folder/output.txt

    done



}

build

As an example command, it would look like this: function file_1.txt file_2.txt file_3.txt -o $folder/output.txt

I've tried the following:

`$(find . iname $folder/*.txt)

2 Answers 2

2

find -iname only matches on basenames (leading directories for any files considered for a match are removed), so your expression never matches anything. Use -ipath instead.

Or, if you know these folders are never empty and don't contain subdirectories, simple globbing is good enough. There are plenty of variants, e.g. this should work:

for folder in ./scenarios/*; do
  (cd $folder && function * -o output.txt)
done
Sign up to request clarification or add additional context in comments.

Comments

-1

It seems you want to create a list of files space seperated, 'ls -1' will do this, and you can specify the criteria of the search i.e. .txt files. Then you have the list of files to use in your function.

build
{
   for folder in ./scenarios/*
   do
      filelist=`ls -1 *.txt`
      `function` $filelist ....
   done
}

1 Comment

Do not parse the output of ls, and do not use a regular variable as a list of words.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.