Friday, May 12, 2006

rename all the files that match a pattern

I happend to come across a situation where I got some java files but with extention .txt, and they are more than a hundred files. So I thought of renaming them with using a script. Here is the one I used. And it can be parameterized as well. Hope it would be a useful one.


x=`find . -name "*.txt" -print sed 's/txt/java/g'`
for xx in $x ; do
fileone="$(echo $xx sed 's/java/txt/g')"
filetwo=$xx
if [ "$fileone" != "$filetwo" ] ; then
echo "moving" \"$fileone" to \"$filetwo"
mv "$fileone" "$filetwo"
fi
done

No comments: