2

Possible Duplicate:
How can I rename a lot of files using a regex?

I've got some files named

abc-10.5.3.1-1.x86_64.rpm
abc-compiler-10.5.3.1-1.x86_64.rpm
abc-10.5.3.1-6.x86_64.rpm   
abc-compiler-10.5.3.1-6.x86_64.rpm

I'd like to remove the minor version numbers (1-1 and 1-6) whenver the files are built. I can't figure out how to achieve this with the rename command on Redhat linux. The resulting filename should for example be:

abc-10.5.3.x86_64.rpm

The command doesn't take regular expressions.

KalenGi
  • 123
  • 3
  • The regexp version of rename is a fairly short perl script. I found a [copy of the Debian one on Github](https://raw.github.com/gist/883553/01bb2c924a5724ea2b6804886c1a54329473fefa/prename.pl) – derobert Nov 27 '12 at 17:30
  • 1
    Be careful about removing the minor version. Even the build number (the part between last dash and `.` specifier) can have significance in some cases, but removing part of version number is usually not really a good idea - the version number is there for some reason. :) – peterph Nov 27 '12 at 17:36
  • @peterph The renamed files are for internal use and not to be distributed – KalenGi Nov 27 '12 at 17:57
  • @kalengi some distributions strip the version numbers from the filenames completely. It's just that you can mess up easily by mixing various patch levels, e.g. by not noticing during a copy/move operation, that only some of the packages got overwritten and end up with mismatching `abc` and `abc-compiler` packages. Which on install might be caught by `rpm`, but on extra cost. – peterph Nov 27 '12 at 18:23

2 Answers2

2

If your rename(1) is anything like this Fedora one I don't think it will work for what you want to do.

Here is one approach which requires bash:

( shopt -s extglob
for f in ./*-+([.[:digit:]])-+([[:digit:]]).x86_64.rpm; do
    mv -v -- "$f" "${f%-+([[:digit:]]).x86_64.rpm)}.x86_64.rpm"
done )

The +([.[:digit:]]) part is to match the major/minor version number, and the +([[:digit:]]) after the - is to match the patch number which you are trying to remove.

You could also do something less accurate with POSIX sh, which lacks extglob

for f in ./*-*-*.x86_64.rpm; do
    mv -v -- "$f" "${f%-*.x86_64.rpm}.x86_64.rpm"
done
jw013
  • 50,274
  • 9
  • 137
  • 141
2

In case you want to consider alternatives to rename, with zsh, I'd write:

autoload -U zmv
zmv -n '(*).<->-<->(.[^.]##.rpm)' '$1$2'

(and remove -n when happy).

Stéphane Chazelas
  • 522,931
  • 91
  • 1,010
  • 1,501