Reputation: 183
can you please tell me what is wrong with my simple code? (Mac Bash)
I am trying to copy all files that are called vol_0000.nii from one directory to another. I replaced the variable file name with '*'. All files (vol_0000.nii) have the same name but they are in different folders (Indicated by '*'). Im not sure whether when they are being copied they are replacing each other since they have the same name of the cp creates, for example, vol_00001.nii, vol_00002.nii and so on..?
cp /Users/dave/biomkr/dat/*/rs/orig/vol_0000.nii /Users/dave/Documents/MIT_Har_stu/rsfmri
Upvotes: 2
Views: 862
Reputation: 42766
Something like this should do the job, using a similar numbering behaviour to that which you mentioned in your question.
#!/bin/bash
i=0
for src in /Users/dave/biomkr/dat/*/rs/orig/vol_0000.nii
do
dest=$(basename "$src")
dest=${dest/.nii/_$i.nii}
cp "$src" "/Users/dave/Documents/MIT_Har_stu/rsfmri/$dest"
let i++
done
Another option is to create a subdirectory, based on the directory name substituted for *
in the original glob, which we can get with a little string replacement.
#!/bin/bash
for src in /Users/dave/biomkr/dat/*/rs/orig/vol_0000.nii
do
srcdir=${src#/Users/dave/biomkr/dat/}
srcdir=${srcdir%/rs/orig/vol_0000.nii}
srcdir="/Users/dave/Documents/MIT_Har_stu/rsfmri/$srcdir/"
mkdir -p "$srcdir"
cp "$src" "$srcdir"
done
Upvotes: 1