• Uncategorized

About linux : loop-through-a-subfolder-in-bash

Question Detail

I am super fresh in the bash language and I am missing several key concepts.
Please excuse my ignorance.

My structure is

folder A
   |--- folderB1q/output/output.csv
   |--- folderB2q/output/output.csv
   |--- ...
   |--- folderB100q/output/output.csv

I want to read in bash from folder A each output.csv file and apply a function on it. I have tested my function and it works when I go to the directory of the file.
I have thousands of directories though…

for each in *q/output/ 
do
my function .......
done

Thank you for your time and sorry for my bad coding

Question Answer

each output.csv

Then loop over output.csv files.

for each in */output/output.csv
do
    my function "$each"
done

I have thousands of directories though…

With many directories, using a shell loop might be memory expensive, because shell. Use a find.

find . -type f -mindepth 3 -maxdepth 3 -path '.*/output/output.csv' |
xargs -d'\n' -n1 my_command

You may also like...

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.