Renaming part of a filename [duplicate] Renaming part of a filename [duplicate] bash bash

Renaming part of a filename [duplicate]


There are a couple of variants of a rename command, in your case, it may be as simple as

rename ABC XYZ *.dat

You may have a version which takes a Perl regex;

rename 's/ABC/XYZ/' *.dat


for file in *.dat ; do mv $file ${file//ABC/XYZ} ; done

No rename or sed needed. Just bash parameter expansion.


Something like this will do it. The for loop may need to be modified depending on which filenames you wish to capture.

for fspec1 in DET01-ABC-5_50-*.dat ; do    fspec2=$(echo ${fspec1} | sed 's/-ABC-/-XYZ-/')    mv ${fspec1} ${fspec2}done

You should always test these scripts on copies of your data, by the way, and in totally different directories.