Renaming part of a filename


Question

I have loads of files which look like this:

DET01-ABC-5_50-001.dat
...
DET01-ABC-5_50-0025.dat

and I want them to look like this:

DET01-XYZ-5_50-001.dat
...
DET01-XYZ-5_50-0025.dat

How can I do this?

1
81
8/21/2018 3:30:48 PM

Accepted Answer

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
154
9/8/2009 8:56:56 AM

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

No rename or sed needed. Just bash parameter expansion.


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon