Operating System - HP-UX
1753389 Members
7288 Online
108792 Solutions
New Discussion юеВ

renaming a bunch of files

 
John Kittel
Trusted Contributor

renaming a bunch of files

Using HP-UX 11i,

I want to rename a bunch of files. The filenames are of the form *.res.orig and I want to rename them all back to *.res

I found an example in the book UNIX Power Tools, and modified it slightly.

This works:
# ls -d *.orig | sed "s/\(.*\ \.orig$/cp '&' '\1'/" | sh -v

But if I try it with cp -i in order to get a prompt whether to overwrite an existing file, it fails. It prompts me, but then doesn't accept my answer, "yes", and go ahead and copy the file, and continue with the next file. Like so:
# ls -d *.orig | sed "s/\(.*\)\.orig$/cp -i '&' '\1'/" | sh -v
cp -i 'dev1_dev_fat.res.orig' 'dev1_dev_fat.res'
overwrite dev1_dev_fat.res? (y/n) # y
sh: y: not found.
#



6 REPLIES 6
Pete Randall
Outstanding Contributor

Re: renaming a bunch of files

John,

Somebody's got to ask the obvious - at the risk of sounding completely stupid, I'll volunteer.

If you're just going to reply "y" anyway, why are you using the "-i" paramater?


Pete


Pete
Alzhy
Honored Contributor

Re: renaming a bunch of files


for i in *.reg;do

oname=`echo $i|sed 's/\.orig//'`
cp -i $i $oname

done
Hakuna Matata.
H.Merijn Brand (procura
Honored Contributor

Re: renaming a bunch of files

perl -MFile::Copy -e'for(<*.res.orig>){($n=$_)=~s/.orig$//;move$_,$n}'

# csh
% foreach i (*.res.orig)
foreach? mv $i $i:r
foreach? end
%

Enjoy, have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Shannon Petry
Honored Contributor

Re: renaming a bunch of files

This loop is slightly different, but I have no problems with it.


for FILE in `ls *.orig` ; do
NEW=`echo $FILE|sed s/.orig//g`

echo "rename $FILE back to $NEW?"
cp -i $FILE $NEW
done

Regards,
Shannon
Microsoft. When do you want a virus today?
Jordan Bean
Honored Contributor

Re: renaming a bunch of files


$ sh
$ for f in *.res.orig; do mv $f ${f%.orig}; done
$ exit

John Kittel
Trusted Contributor

Re: renaming a bunch of files

Thanks everyone for your answers.

I used -i the first time I tried it because I didn't know if I had botched typing in the command correctly and whether it was going to do what I wanted. Of course, after I saw that it was going to do what I wanted, I was then confident doing without the -i, and I successfully renamed all the files.

The intent of my post to the forum was really to learn why the command was failing, for education purposes and professional/technical growth. It seems a nice solution to a somewhat common situation which I could remember how to do from the command line without having to have a script stored somewhere.

- John