Operating System - HP-UX
1819967 Members
3556 Online
109607 Solutions
New Discussion юеВ

Re: Script to rename from *.abc to *.def

 
SOLVED
Go to solution
Pedro Cirne
Esteemed Contributor

Script to rename from *.abc to *.def

Hi,

I need to rename all *.abc files to the same name but with a diferent extension, let's say *.def, for example:

portugal.abc to portugal.def

Any ideias on how to do this with a script?

Thks,

Pedro
7 REPLIES 7
Pete Randall
Outstanding Contributor
Solution

Re: Script to rename from *.abc to *.def

I think this would do it:


for FILE in *.abc
do
mv $FILE `basename $FILE`.def
done


Pete

Pete
TwoProc
Honored Contributor

Re: Script to rename from *.abc to *.def

Providing you're just dealing with a single directory of files:

for i in *.abc
do
mv $i `sed -e "s/\.abc/\.def/"`
done
We are the people our parents warned us about --Jimmy Buffett
Rick Garland
Honored Contributor

Re: Script to rename from *.abc to *.def

for i in `ls *.abc`
do
mv $i $i.def
done
Rick Garland
Honored Contributor

Re: Script to rename from *.abc to *.def

Correction;

for i in `ls *.abc`
do
newname=`echo $i | awk -F. '{print $1}'`
mv $newname $newname.def
done

A. Clay Stephenson
Acclaimed Contributor

Re: Script to rename from *.abc to *.def

#usr/bin/sh

ls | grep -E -e '\.abc$' | while read FNAME
do
BASE=$(basename "${FNAME}" ".abc")
NEW_FNAME="${BASE}.def"
if [[ -e "${NEW_FNAME}" ]]
then
echo "Can't rename ${NEW_FNAME}; file exists." >&2
else
mv "${FNAME}" "${NEW_FNAME}"
fi
done

With a bit more work, you could pass in the old and new suffixes and shell arguments but I'll leave that as a student exercise. Man basename for details.

If it ain't broke, I can fix that.
TwoProc
Honored Contributor

Re: Script to rename from *.abc to *.def

Oops, had an error in there...
The following:

for i in *.abc
do
mv $i `sed -e "s/\.abc/\.def/"`
done

Should be:
for i in *.abc
do
mv $i `echo ${i}|sed -e "s/\.abc/\.def/"`
done

We are the people our parents warned us about --Jimmy Buffett
Pedro Cirne
Esteemed Contributor

Re: Script to rename from *.abc to *.def

Hi,

Thank you all !

Regards

Pedro