1834287 Members
1885 Online
110066 Solutions
New Discussion

Re: rename files script

 
walid salah faroun
Occasional Advisor

rename files script

I have hundreds of files that I would like to rename to such as 2004010000 - 2004010999.
these file names have spcaces in them such as xxx 01 xx 02.dat

what would be a script that i can use that will achieve that?

thanks for the help in advance.
5 REPLIES 5
A. Clay Stephenson
Acclaimed Contributor

Re: rename files script

You need to be much more specific about what you want the rename patterns to be. For example, is there only I file that contains "01" and should it become the 2004040001 file. Are all the files in the same directory? Do all the files start with "xx"? The spaces are not a problem. Spaces are legal (if dumb) UNIX pathname characters but should be avoided because most scripts assume no whitespace. In your case, it just means that careful attention must be paid to quoting.

Give 5 or 6 very specific examples and this should be fairly easy -- especially in Perl but also in the shell.

If it ain't broke, I can fix that.
walid salah faroun
Occasional Advisor

Re: rename files script

thanks for you quick replay;
here is a list of some files:
2004-SEP-04_02-43-22_ 0.DAT
2004-SEP-04_03-18-44_ 34.DAT
2004-SEP-04_02-44-24_ 1.DAT
2004-SEP-04_03-19-44_ 35.DAT
2004-SEP-04_02-45-28_ 2.DAT
2004-SEP-04_03-20-44_ 36.DAT
2004-SEP-04_02-46-31_ 3.DAT
2004-SEP-04_03-21-44_ 37.DAT

so what I would like to do is rename all files such as 2004120001 through 2004129999.
file pattern is yyyymm0001 - yyyymm9999.
thanks again for the help.
A. Clay Stephenson
Acclaimed Contributor

Re: rename files script

This still isn't enough to work with. You've now listed some input patterns (but I don't see any spaces so include those examples) but what is missing are the desired output names.

File111_xxx.DAT --> 2004120111
File112_ DAT --> 2004120012

or whatever.
If it ain't broke, I can fix that.
walid salah faroun
Occasional Advisor

Re: rename files script

the space is after the second underscore
"2004-SEP-04_02-43-22_ 0.DAT"
the desired output is such as
20041200001 through 2004129999
or really depending on how namy files I have in the directory.

thanks again.
A. Clay Stephenson
Acclaimed Contributor

Re: rename files script

Okay this is pretty easy; I'm going to suggest that you copy these files to another directory and then remove the originals and then you mv the copies to the original directory. This will give you a method of fixing any problems while you perfect your script:

#!/usr/bin/sh

typeset DESTDIR=/xxx/yyy

if [[ ! -d ${DESTDIR} ]]
then
echo "Copy directory ${DESTDIR} not found" >&2
exit 255
fi

typeset -i I=0
typeset -Z4 Z=0
typeset DESTNAME=""
ls | while read X
do
Z=${I}
DESTNAME="${DESTDIR}/200412${Z}"
echo "File \"${X}\" --> \"${DESTNAME}\""
# cp "${X}" "${DESTNAME}"
# leave above line commented while testing
((I += 1))
done

-------------------------
This should be very close.
If it ain't broke, I can fix that.