21

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 
jww
  • 97,681
  • 90
  • 411
  • 885
cc211
  • 387
  • 1
  • 4
  • 12

5 Answers5

31

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.

lobianco
  • 6,226
  • 2
  • 33
  • 48
21

A pure bash solution:

for i in cc*; do
  mv "$i" "${i/_13/_15}"
done
chepner
  • 497,756
  • 71
  • 530
  • 681
  • 1
    I would suggest to add quotes like this so there is no problem with spaces in filenames: for i in cc*; do mv "$i" "${i/_13/_15}"; done – syss Nov 10 '15 at 08:17
  • @user234461 No, parameter expansions use glob patterns, not regular expressions. – chepner Nov 24 '16 at 12:59
8
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*
John Lawrence
  • 2,923
  • 17
  • 23
  • Didn't seem to work on my shell, see Edit 1? What did I do wrong? – cc211 Jun 15 '12 at 15:28
  • 1
    I think we have different versions of the `rename` command. I've edited my answer with more details, hope it helps! – John Lawrence Jun 15 '12 at 15:47
  • 3
    `rename` is a command line tool in the perl package, also known as `prename`.It can use infinitely complex perl expression, so `prename -n 'use LWP::Simple;my $http=get("http://airport-authority.com/browse-FR");while($http=~m/>([^<>]+)<\/a> \((\w\w\w)\)/gc){my $from=$2;my $to=$1;$to=~s/ /_/g;s/$from/$to/g};' MRS_DOLLY_PARTON.MP3 PUFF_DADDY.MP3` will rename three letter codes in file names to French air port names with underscores, in this case the two MP3s are renamed `Marseille_Airport_St_Gatien_AirportLY_PARTON.MP3` and `Uzein_AirportF_DADDY.MP3`. – David Andersson Jun 15 '12 at 16:04
0

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

jesramgue
  • 21
  • 3
0

In this case, you can use this command:

rename -v "_130" "_150" *.II
Sepehr roosta
  • 567
  • 1
  • 5
  • 11