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:
Batch renaming files

Let us assume that I have six files named:

1.a, 1.b, 1.c
2.a, 2.b, 2.c

where a,b,c are file extensions like sh, txt etc.

Now, I want to rename the files 1.a, 1.b, 1.c with say 5.a, 5.b, 5.c and 2.a, 2.b, 2.c with 6.a, 6.b and 6.c. Here both 2 and 6 are user supplied inputs.

share|improve this question

marked as duplicate by jasonwryan, manatwork, rahmu, Renan, uther Jan 15 '13 at 14:14

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.

add comment

3 Answers 3

up vote 3 down vote accepted

Not tried, but should work

$ rename 's/1/5/' 1.*
$ rename 's/2/6/' 2.*

rename man page

share|improve this answer
add comment

Here is a bash function that will do the trick:

do_rename() {
   oldnum=$1 # assign parameters for clarity
   newnum=$2

   for f in "$oldnum".*; do # get all files matching the old number
       mv "$f" "$newnum"."${foo##*.}" # use a parameter expansion to get the exetension of the current filename
   done
}
share|improve this answer
add comment

One way:

#!/bin/bash

for num in 1 2
do
    read -p "Enter new val for files starting with $num :" val
    for i in ${num}*.[abc]
    do
            ext=${i##*.}
            mv $i "$val.$ext"
    done
done
share|improve this answer
    
You can use read -p to combine the read and the prompt in the same command. –  jordanm Jan 15 '13 at 6:27
    
@jordanm : thanks for your input, updated the solution –  Guru Jan 15 '13 at 6:31
add comment

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