Home > Blockchain >  How do I rename a file using bash that keeps the first 15 characters of filename and replaces the re
How do I rename a file using bash that keeps the first 15 characters of filename and replaces the re

Time:04-07

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;

CodePudding user response:

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
  • Related