Operating System - HP-UX
1838571 Members
3186 Online
110128 Solutions
New Discussion

Re: Rename all the files in a directory?

 
SOLVED
Go to solution

Rename all the files in a directory?

Hi:

Our company is preparing some combined financial/sales reports. I sent out a memo last year indicating
that all files are to be placed in a specific directory and be given filenames like "dd-month-yyyy". For example, 01-March-2002, 13-July-2003. Unfortunately, I did not specify that the monthnames should be English month names. You can guess what happened to our submittals from Madrid and Paris. I now have directories with 15-Diciembre-2002 and 01-Julio-2002. Does anybody know of a UNIX utility that will convert the filenames?

Thanks,
Steve


7 REPLIES 7
Caesar_3
Esteemed Contributor

Re: Rename all the files in a directory?

Hello!

You can use sed,
write small script that move files
take the orig. name and move to new name
that generated by sed.
examp: sed /Julio/July/

Check the man of sed to understand.

Caesar
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: Rename all the files in a directory?

Well, let's see. This here is a date problem (kinda, sorta), I wonder what tool I'll use?

We can take advantage of the unix LANG settings (specifically LC_TIME).

Do a locale -a and pick a likely suspect. Next,
LC_TIME=yourpick locale LC_TIME - if your monthnames match - this here's your boy. If you already know the LANG settings this ignore this step.

lang=es_ES.iso88591
# my Spanish guess
#
ls mydir | while read DT
do
DT2=$(caljd.sh -O -S "-" -e $(LC_TIME=${lang} caljd.sh -c -e -I -S "-" ${DT}))
echo "${DT} -> ${DT2}"
done

I'll let you do the mv or cp commands from ${DT} to ${DT2} but this should be very close.

I've not tested this but it looks okay to me. This will learn you to carefully craft specifications next time.

Search the forums for caljd.sh and make sure you find version 2.1. Invoke as caljd.sh -u to get a clue as to what is happening.

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

Re: Rename all the files in a directory?

first make a list
cd /dir


ls *Julio* > juliolist

then run a script containing:




cat juliolist |while read line
do
DAY=$(echo $line|awk '{FS="-";print $1}')
MONTH=$(echo $line|awk '{FS="-";print $2}')
YEAR=$(echo $line|awk '{FS="-";print $3}')
mv $DAY-$MONTH-$YEAR $DAY-july-$YEAR
done



This would take care of july
modify/repeat for the other 11 months.

Re: Rename all the files in a directory?

Hi:

Thanks. A. Clay's method worked the first time. I already knew the LANG settings for Madrid and Paris. I don't quite know how it works but the names go in in French and come out in English. I first did an ls to a temporary file then read it. A mv $DT $DT2 did the rest. This was even easier than multiple passes with sed after I looked up all the possible month names.

Thanks to all,
Steve

Re: Rename all the files in a directory?

Hi A. Clay,

I noticed that sometimes the files read in are "Julio" , sometimes they are "JULIO", and sometimes they are "julio". Your script converts all of these to the correct version "July". We don't have any filenames that are duplicates but I don't under stand how the month names were matched. That must be some complicated sed.

Thanks again,
Steve
A. Clay Stephenson
Acclaimed Contributor

Re: Rename all the files in a directory?

Hi Steve:

Actually the matchings was easy. Everything is matched as lowercase. This does indicate a potential problem. If you have 04-Julio-1776 and 04-JULIO-1776 then both will map to 04-July-1776 and the last one wins. As long as there are no case-insensitive duplicates then no worries.

Since you indicated that you have at least 2 languages, I would suggest that you modify your script passing in lang as an argument.

Regards,
Clay
If it ain't broke, I can fix that.

Re: Rename all the files in a directory?

Good idea! I'll do that.