2

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.

Sibi
  • 351

3 Answers3

3

Not tried, but should work

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

rename man page

mtk
  • 27,530
  • 35
  • 94
  • 130
1

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
Guru
  • 5,905
1

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
}
jordanm
  • 42,678