Bill
Bill

Reputation: 1247

unix command to rename a group of files and take part of the name out

I have a bunch of files in a dir. I need to rename all of them with part of the name taken out. EXAMPLE:

old:

64sdfdf2_test.txt363qww6.dat.z
64cvxc65_test.txt36ntg44.dat.z
6jtyjj54_test.txt3as3463.dat.z

new:

64sdfdf2363qww6.dat.z
64cvxc6536ntg44.dat.z
6jtyjj543as3463.dat.z

NOTE: "_test.txt" is what i need remove and it is the same in all the files.

Upvotes: 0

Views: 371

Answers (2)

Arne
Arne

Reputation: 2674

See the manpage for mmv. Something like

mmv '*_test.txt*' '#1#2'

should do the trick.

Upvotes: 0

Vijay
Vijay

Reputation: 67221

for i in *_test.txt*
do
new_name=`echo $i|sed 's/\(.*\)_test.txt\(.*\)/\1\2/g'
mv $i $new_name
done

i tested only the sed part and its working fine:

> echo "64sdfdf2_test.txt363qww6.dat.z" | sed 's/\(.*\)_test.txt\(.*\)/\1\2/g'
64sdfdf2363qww6.dat.z

Upvotes: 2

Related Questions