Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.

share|improve this question

marked as duplicate by jasonwryan, Renan, Mat, manatwork, warl0ck Nov 28 '12 at 23:36

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

    
The regexp version of rename is a fairly short perl script. I found a copy of the Debian one on Github –  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 .<arch> 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
add comment

2 Answers 2

up vote 2 down vote accepted

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).

share|improve this answer
    
This is a very elegant solution :) –  KalenGi Nov 27 '12 at 19:27
add comment

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
share|improve this answer
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.