Reputation: 9
I'm trying to create files with the name of it's antepenultimate directory:
Example: Directory: a/b/c/d/e/f/g/h/i/j
The name of folder h is different for each case.
So I created an array
array=(/ a / b / c / d / e / f / g / * / * / *)
len=${#array[@]}
for (( q=0; q<$len; q++ ));
do
cd ${array[$q]}
sleep 1
mri_convert 0001*.dcm RAW.nii.gz #--->this line is just converting the format of file 0001*.dcm in to file RAW.nii.gz
done
This code is working but I want the file RAW.nii.gz to be named h_RAW.nii.gz
I tried doing this:
s1="${array%/*/*}"
$ echo "${s1##*/}"
and then:
mri_convert 0001*.dcm ${s1##*/}_RAW.nii.gz
but it's not working.
Upvotes: 0
Views: 50
Reputation: 41
Let's see if I can help. I'm not exactly sure of the details of what you're trying to do (mainly because the code you posted:
for (( q=0; q do cd ${array[$q]} sleep 1 mri_convert 0001*.dcm RAW.nii.gz
is not syntactically correct. So, that can't be what you're actually doing.
Just a hint of how i would approach a problem like this:
for path6 in /a/b/c/*/*/*
do
path5="${path6##*/}"
path4="${path5##*/}"
name4="${path4%/*}"
echo "Processing ${path4}"
mriconvert "${path6}"/0001*.dcm "${path6}/${name4}_RAW.nii.gz"
done
Upvotes: 0
Reputation: 246744
How about
cd /a/b/c/d/e/f/g
for dir in *; do
[[ -d $dir ]] || continue
for subdir in "$dir"/*/*/; do (
# doing this in a subshell so we don't need to "undo" this cd
cd "$subdir"
mri_convert 0001*.dcm "${dir}_RAW.nii.gz"
); done
done
Upvotes: 2