Help for applying same actions to different directories with identical sub-directories

Hello all

I have a situation and would be grateful if you will help me... I am not sure whether it is even possible

I have few folders/directories ---e.g. A - B - C -in my MAIN directory
for each of them I would get the data from folder called DATA and go through a long list of actions which involves creating new sub-directories.
So e.g. will run a program for A -> creating 5 new sub-directories - doing some calculations and getting some results out
the same happens for B and C
I have tried to put an FOR loop around it which seems to work if I only have one of them there as the address for the directories will get confusing as my program refers to sub-directories in the A,B,C

###for A
FILES="A/*"
for X in $FILES
do
FILES="data/*"
for X in $FILES
.
.
.
done
##for B
FILES="B/*"
for X in $FILES
do
FILES="data/*"
for X in $FILES
.
.
.
done

Now my problem is how to adapt it so when I am on MAIN, I can run all of them at the same time and related directories would be created inside each of them separately ...
the Path seems something like this

MAIN/A/ ->  data, id, count, count/result
MAIN/B/ ->  data, id, count, count/result
MAIN/C/ ->  data, id, count, count/result

I am not sure whether something like this is possible ... or they might be a very simple solution but my mind is not supporting now
Thank you in advance
Anna

Is this what you are trying to acheive?

for dir in A B C
do
    mkdir -p ${dir}/data; mkdir -p ${dir}/count; mkdir -p ${dir}/count/result;
done

I will try to explain with an example
I have one main folder with unknown number of folder in them, e.g. A,B,C, ...
but all of them have the same sub-folder structure in them
like each them have a folder called Basic which has the original data in it
I have the following code for sentence count ---

FILES="basic/*"
for X in $FILES
do
	name=$(basename $X) 
		#sentence-count
	awk '{ total+=gsub(/[\.]/," ", $0); next}
	END{print FILENAME,",",total} ' $X 
done > sentence-count.csv

now instead of calling this from each A,B,C path I want to call this from the main folder to automatically do it for all of them

FILES="Main/*"
for X in $FILES
do
#### the problem is here ######
FILES="basic/*"
for X in $FILES
do
	name=$(basename $X) 
		#sentence-count
	awk '{ total+=gsub(/[\.]/," ", $0); next}
	END{print FILENAME,",",total} ' $X 
done > sentence-count.csv ### and here as each folder should has its own file
done

I dont know how I should address the PATHS to read all of the folders after main folder

FILES="*/basic/*" 

Seems to work but when it comes to I don't know what to do to have one per folder

Main/A/sentence-count.csv
Main/B/sentence-count.csv
....

Use find command to gather the list of all sub-directories or files:

for dir in $( find . -type d )
do
   ....
done

OR

for file in $( find . -type f )
do
   ....
done

I know all of them because I created them
Basically I have to repeat same actions for few folders with same structure
So I want to put them all in one directory and put a loop around the code to run all of them together to easy my job
or at least thats what I wish for

see modification below ...

cd /main
for DIR in */basic
do
    for FILE in $DIR/*
    do
         NAME=$(basename $FILE)
         awk '{ total+=gsub(/[\.]/," ", $0); next}
         END{print FILENAME,",",total}' $FILE
    done > $DIR/sentence-count.csv
done
1 Like