1848002 Members
8161 Online
104022 Solutions
New Discussion

rename file script

 
SOLVED
Go to solution
Duffs
Regular Advisor

rename file script

Hi,

I need to rename multiple files within a dir whilst maintaining the old filename also. All files are of the format ORD?????? and I need to rename these to ORD??????K.

Since there are hundreds of files in the dir I need to script this but unfortunately Iam not having much luck. Anybody any ideas?

D.
6 REPLIES 6
Pete Randall
Outstanding Contributor
Solution

Re: rename file script

Something like this?

cd /target_dir
for FILE in ORD*
do
ln -s $FILE $FILEK
done

I used the link command because you said you wanted to retain the original - normally I would have renamed with mv.


Pete

Pete
TwoProc
Honored Contributor

Re: rename file script

for i in ORD??????
do
mv $i ${i}K
done
We are the people our parents warned us about --Jimmy Buffett
Laurent Menase
Honored Contributor

Re: rename file script

#!/usr/bin/ksh
for i in ORD??????
do
mv $i ${i}K
Laurent Menase
Honored Contributor

Re: rename file script

done
of course
A. Clay Stephenson
Acclaimed Contributor

Re: rename file script

You could do something like this:

#usr/bin/sh

ls | grep -E -e '^ORD.{6}$' | while read FNAME
do
NEW_FNAME="${FNAME}K"
if [[ -e "${NEW_FNAME}" ]]
then
echo "Can't copy ${NEW_FNAME}; file exists." >&2
else
cp "${FNAME}" "${NEW_FNAME}"
fi
done
If it ain't broke, I can fix that.
Duffs
Regular Advisor

Re: rename file script

Thanks a millions lads, great help!
Cheers,
D.