I have a folder that contains several sub-folders, and each sub-folder contains a *.nii
file.
The directory structure is:
parent_directory
- JACK (folders are titled as first-names)
- *.nii (each .nii file is named a random unique combination of letters&numbers)
- JILL
- *.nii
- JOE
- *.nii
- JANE
- *.nii
I want to execute this command:
fslreorient2std input output
The "input" must be the name of the .nii
file, and ideally I want the output to be named or_*.nii
.
How can I cd
to each subfolder in my directory, and execute this command where I place "input" as the unique name of the *.nii
file in each subfolder?
Thank you!
CodePudding user response:
Use nested loops. The outer loop iterates over the subdirectories. It performs cd
in a subshell so that it automatically returns to the original shell when done.
The inner loop iterates over the *.nii
files, adding the or_
prefix to produce the output filename.
for dir in */
do (
cd "$dir"
for file in *.nii
do
fslreorient2std "$file" "or_$file"
done
)
done
CodePudding user response:
Untested, using just one loop and bash parameter expansion:
cd parent_directory
for file in J*/*.nii; do
( cd $(dirname "$file") && fslreorient2std "${file##*/}" "or_${file##*/}" )
done
The ( )
create a subshell, that way, each call to cd
start from parent_directory