Sunday 27 February 2022

Renaming all files within a directory - Terminal (.sh) linux

The following script renames a file. It keeps the 7th till 88th letter and add a ".tif" at the end of the files.


mv ./"$file" "${file:7:88}.tif"&lt 

The following script renames all files within the current directory. For example, if I copy this into a .sh file and place it within a directory then it will rename all the files (including the .sh file).


for file in * ; do
    mv ./"$file" "${file:7:88}.tif"
done &lt 

This is particularly useful for massive processing of (e.g., images), since there names needs to be consistent for the script to be able to find the acquired date and/or other information that may be stored in the file name.