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.

This question already has an answer here:

I have the below list of files

aro_tty-mIF-45875564pmo_opt
aro_tty-mIF-45875664pmo_opt
aro_tty-mIF-45875964pmo_opt
aro_tty-mIF-45875514pmo_opt
aro_tty-mIF-45875524pmo_opt

that I need to rename to

aro_tty-mImpFRA-45875564pmo_opt
aro_tty-mImpFRA-45875664pmo_opt
aro_tty-mImpFRA-45875964pmo_opt
aro_tty-mImpFRA-45875514pmo_opt
aro_tty-mImpFRA-45875524pmo_opt

Please help me out.

share|improve this question

marked as duplicate by terdon, slm, Bernhard, Joseph R., Anthon Nov 26 '13 at 18:53

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.

3 Answers 3

up vote 8 down vote accepted

Most standard shells provide a way to do simple text substitution within shell variables. http://tldp.org/LDP/abs/html/parameter-substitution.html explains as follows:

${var/Pattern/Replacement}

First match of Pattern, within var replaced with Replacement.

So use this script to loop through all the appropriate files and rename each of them:

for file in aro_tty-mIF-*_opt
do
    mv -i "${file}" "${file/-mIF-/-mImpFRA-}"
done

I have added a -i option so you have the chance to confirm each renaming operation. As always, you should make a backup of all your files before doing any large amount of renaming or deleting.

share|improve this answer
    
Thanks Mr.Mark. –  Udhayakumar Nov 26 '13 at 16:44

Before trying complex commands like the following, backup your files. You never know what a typo (mine or yours) can cause.

With mv (as you asked in comment --- rename as suggested in the other answer is probably safer, especially if you can have spaces or strange chars in your filenames) something of the style

for f in *_opt; do
    a="$(echo $f | sed s/-mIF-/-mImpFRA-/)"
    mv "$f" "$a"
done
share|improve this answer

If you don't have Perl's rename:

perl -e '
FILE:for $file (@ARGV){
        ($new_name = $file) =~ s/-mIF-/-mImpFRA-/
        next FILE if -e $new_name;
        rename $file => $new_name
}' *_opt

If you do have Perl's rename:

rename 's/-mIF-/-mImpFRA-/' *_opt
share|improve this answer
    
@Udhayakumar Why does the solution need to use mv? –  Joseph R. Nov 26 '13 at 17:07

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