Elaina Gombos
Elaina Gombos

Reputation: 3

How do I rename a file using bash that keeps the first 15 characters of filename and replaces the rest with a single word?

I am trying to rename nifti files, aiming to keep the first 15 characters of filename (which are random and indicate subject id) and rename the rest of the file to "scan_name.nii.gz"?

for i in `ls ${dir}/nifti_loc`; do echo ${i};  
for j in `ls ${dir}/nifti_loc/*MPRAGE*.nii.gz | grep -v -i AXI`; do   
cp ${j} ${dir}/nifti_loc/rename/${j}\_mprage.nii.gz;  
done;  
done;

Upvotes: 0

Views: 57

Answers (1)

jared_mamrot
jared_mamrot

Reputation: 26515

Does this provide your expected outcome?

cd "${dir}"/nifti_loc

for f in *MPRAGE*.nii.gz
do
    mv "$f" "${f:0:15}_scan_name.nii.gz"
done

Upvotes: 1

Related Questions