cc211
cc211

Reputation: 397

Rename small part of multiple files in middle of name using Bash?

I'd just like to change this

cc211_AMBER_13062012i.II  cc211_GROMOS_13062012i.II
cc211_CHARM_13062012i.II  cc211_OPLS_13062012i.II

to

cc211_AMBER_15062012i.II  cc211_GROMOS_15062012i.II
cc211_CHARM_15062012i.II  cc211_OPLS_15062012i.II

I tried,

find -name "*.13 *" | xargs rename ".13" ".15"

There is normally no space between the 3 and the second asterix, thats just makes it italics on from what I can see. Basically there's a lot of answers for what to do when it's at the end of the filename, where asterix seem to work, but here I can't make it work.

Anything you've got would make my life a lot easier!

Edit 1: Trial

-bash-4.1$ ls

cc211_AMBER_13062012.II  cc211_GROMOS_13062012.II
cc211_CHARM_13062012.II  cc211_OPLS_13062012.II

-bash-4.1$ rename 's/_13/_15/' cc*
-bash-4.1$ ls

cc211_AMBER_13062012.II  cc211_GROMOS_13062012.II
cc211_CHARM_13062012.II  cc211_OPLS_13062012.II 

Upvotes: 22

Views: 21990

Answers (5)

Sepehr roosta
Sepehr roosta

Reputation: 607

In this case, you can use this command:

rename -v "_130" "_150" *.II

Upvotes: 0

jesramgue
jesramgue

Reputation: 21

I'm using a pure Linux solution:

### find all files that contains _DES in name and duplicate them adding _AUXLOCAL
for f in **/*_DES*; do
    cp "$f" "${f%.DES}_AUXLOCAL"
done 
###Rename all _AUXLOCAL files, removing _DES to _LOCAL
for f in **/*_AUXLOCAL*; do
  mv "$f" "${f/_DES/_LOCAL}"
done
###Rename all _AUXLOCAL files, removing _AUXLOCAL
for f in **/*_AUXLOCAL*; do
  mv "$f" "${f/_AUXLOCAL/}"
done

I hope it helps

Upvotes: 0

chepner
chepner

Reputation: 531165

A pure bash solution:

for i in cc*; do
  mv "$i" "${i/_13/_15}"
done

Upvotes: 21

John Lawrence
John Lawrence

Reputation: 2923

rename 's/_13/_15/' cc*

Should do what you want. The regular expression s/_13/_15/ replaces _13 by _15 in all files starting 'cc'.

$ ls
cc211_AMBER_13062012.II  cc211_GROMOS_13062012.II
cc211_CHARM_13062012.II  cc211_OPLS_13062012.II

$ rename 's/_13/_15/' cc*

$ ls
cc211_AMBER_15062012.II  cc211_GROMOS_15062012.II
cc211_CHARM_15062012.II  cc211_OPLS_15062012.II

This will only work with the newer perl version of rename. To check which version you have do man rename. If the top of the page says

Perl Programmers Reference Guide

you have the perl version. If it says:

Linux Programmer's Manual

you have the standard (older) version.

For the older version, the command should be:

rename _13 _15 cc*

Upvotes: 8

lobianco
lobianco

Reputation: 6276

How about this:

for i in *.II; do mv $i $(echo $i | sed 's/_13/_15/g'); done

This will replace _13 with _15 in all files with extension .II

More information on sed here.

Upvotes: 32

Related Questions