Ubuntu – Rename all file extension in directory

bashrename

I am new to scripting, and I have a directory with all files named num.pdb.ostat. I would like to rename all num.ostat (that is deleting the .pdb in all). For a single file this works:

mv 2.pdb.ostat 2.ostat

but when I try to do it for all files in folder with this script

for num in ./*; do mv ${num}.pdb.ostat ${num}.ostat; done

nothing happens

Can anyone tell me, where I went wrong?

Best Answer

${num} takes the whole file name. You need to get filename without extension and add your new extension. You can make a string formatting. Use the following command:

for num in ./*; do mv ${num} ${num%.*.*}.ostat ; done

% deletes shortest match of $substring from back of $string.

Related Question