Dropping file extensions

Randomly, for those people who do a lot of manipulation of file names, it can be painful to constantly use sed or awk or basename to change the filename without the extension.  Here are three options to copy a file via 3dcopy (part of AFNI, works just like cp, but for image files, will also convert them to different formats).  Assume I have the following files and want to convert them to uncompressed format NIFTI.

Subject001_run1.nii.gz Subject001_run2.nii.gz Subject001_run3.nii.gz

Here are three options:

1. basename

for aFile in *.nii.gz
do
   newName=`basename $aFile .nii.gz` #returns subject001
   3dcopy $aFile $aFile.nii #effectively: 3dcopy subject001.nii.gz subject001.nii
done

2. sed

for aFile in *.nii.gz
do
   newName=`echo aFile | sed 's/.nii.gz/.nii` #returns subject001.nii
   3dcopy $aFile $newName #effectively: 3dcopy subject001.nii.gz subject001.nii
done

3. The old % trick

for aFile in *.nii.gz
do
   3dcopy ${aFile} ${aFile%.nii.gz}.nii #effectively: 3dcopy subject001.nii.gz subject001.nii
done

 

Comments are closed.