I have a main folder named "simulations". In this folder I have 250 different folders named with year-month-day as: "19902010" "20040512" etc... These dates are random without any specific pattern but always in the form "year-month-day". In each "year-month-day" folder I have a folder named "outfiles" in which are contained the files that I have to change automatically. How can I run this automatically? Hope to have a reply!! Thanks in advance! Silvia
Asked
Active
Viewed 4,219 times
2
-
Thanks for the reply! I have the main folder named "simulations". In this folder I have 250 different folders named with year-month-day as: "19902010" "20040512" etc... These date are random without any specific pattern but always in the form "year-month-day". In each folder I have a folder named "outfiles" in which are contained my files. – Silvia Massaro Feb 12 '20 at 00:33
-
1The files inside the folder "outfiles" are named as "c_001_000001.grd", "c_001_000002.grd", "c_001_000003.grd", and so on. So, I have to run my script automatically for each of thiese .grd files and save them as "ppm_c_001_000001.grd" and so on.. So it is quite complicate!! – Silvia Massaro Feb 12 '20 at 00:49
-
1Please edit your question and add all the new information in the question. – user68186 Feb 12 '20 at 01:07
1 Answers
2
If you want to run your Python script (let me call it myScript.py
) on each *.grd
file in all subfolders (recursively) of a given folder, then find
is the right command to do it:
find . -name '*.grd' -exec python myScript.py {} \;
This command recursively finds each file in the given path1 with the name matching *.grd
and calls python myScript.py <found .grd file>
2 for each found file. You can test this by adding echo
right after -exec
, then it will just print the commands.
1 I used .
in my example and you can use it, too, if you cd
to the specified folder first. Otherwise, you can use any appropriate relative or absolute path here.
2 If your script is executable and has the correct shebang, you can leave out python
and call myScript.py
directly.
See also

Melebius
- 11,431
- 9
- 52
- 78