Operating System - HP-UX
1843342 Members
3467 Online
110214 Solutions
New Discussion

Renaming multiple files within a script / batch file

 
SOLVED
Go to solution
Simon R Wootton
Regular Advisor

Renaming multiple files within a script / batch file

Hi Folks,
I'm running HP-UX 11i v1.
I am trying to rename multiple files within a directory.
The files are of the form :

'STRyymmdd.' and need to be renamed to 'STRyymmdd.csv'

e.g.
STR080806.
STR080807.

need to become
STR080806.csv
STR080807.csv

is there an easy way to do this?

Any help greatfully received and rewarded!

Thanks

Simon
4 REPLIES 4
Pete Randall
Outstanding Contributor
Solution

Re: Renaming multiple files within a script / batch file

A simple for loop would do:

for FILE in STR*
do
mv $FILE $FILE.csv
done


Pete

Pete
Simon R Wootton
Regular Advisor

Re: Renaming multiple files within a script / batch file

Sorted - thanks Pete
Eric Raeburn
Trusted Contributor

Re: Renaming multiple files within a script / batch file

A tip from the paranoid. It is very easy to make a mistake with these types of commands, with possibly disastrous results. Whenever I do this type of thing, I first echo it to the display. So for example, in the proposed solution, I would do

echo mv ${FILE} ${FILE}.csv

If the output is correct, recall the command from the history buffer and remove "echo". If the output is too long, you can always pipe it through 'head'.

Note also the curly braces. I prefer to play it safe if one of the adjacent characters is non-alphanumneric.

-Eric
Dennis Handly
Acclaimed Contributor

Re: Renaming multiple files within a script / batch file

>Eric: I prefer to play it safe if one of the adjacent characters is non-alphanumeric.

Except for "_", those are the cases where it is safe to leave out the {}. :-)