jxh461
March 31, 2003, 6:49pm
1
Can someone please tell me how I can rename a bunch of files at a time. I hava a directory that has 700+ files that are named
*.xyz and I would like to rename them to *.abc . How can I do that with a simple command ?
mv *.xyz *.abc did not work.
Thanks in advance
There are a lot of ways, for example you can try with:
cd /<your_directory>
for myfile in *.xyz
do
file_name=`echo $myfile |awk '{print substr($1,1,length($1)-4)}'`
echo "mv $file_name.xyz $file_name.abc"
done
Note: when you are sure remove the echo and the quotes.
from: echo "mv $file_name.xyz $file_name.abc"
to: mv $file_name.xyz $file_name.abc
Regards. Hugo
Originally posted by hugo_perez
There are a lot of ways, for example you can try with:
cd /<your_directory>
for myfile in *.xyz
do
file_name=`echo $myfile |awk '{print substr($1,1,length($1)-4)}'`
echo "mv $file_name.xyz $file_name.abc"
done
Note: when you are sure remove the echo and the quotes.
from: echo "mv $file_name.xyz $file_name.abc"
to: mv $file_name.xyz $file_name.abc
Regards. Hugo
why all the substr biz?
why not just: file_name=`echo $myfile|awk -F. '{ print $1 }'`
or even
for i in `ls *.xyz`;do
mv $i $i.abc
done
That would rename file.xyz to file.xyz.abc which is (probably) not what we want here.
I use ksh (I may have mentioned that before). In ksh, you can use ${i%.*} to strip off the the file suffix.
So if ksh is your interactive shell, this command line will do it:
for i in .xyz ; do mv $i ${i%. }.abc ; done