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.

marked as duplicate by jasonwryan, manatwork, rahmu, Renan, George M 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.


Not tried, but should work

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

rename man page


Here is a bash function that will do the trick:

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

   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

One way:


for num in 1 2
    read -p "Enter new val for files starting with $num :" val
    for i in ${num}*.[abc]
            mv $i "$val.$ext"
  • 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

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