Renaming files

Hi

i have to achieve the following

i have files as xyz001.csv, xyz002.csv.......xyz0025.csv in a folder, i need to keep xyz001.csv as it is
but want to remove the extra zero on filename from 10 say
xyz0010 should be renamed to xyz010
xyz0025 should be renamed as xyz025

Note xyz can be anything xyz1 or xyz2 or abc .......

Please advise

What have you tried so far?

don't have much idea on how this can be achieved

You can have a loop read all file names starting with xyz and ending with .csv

Now, if you find xyz00 then replace it as xyz0, can be achieved by mv command :slight_smile:

Thanks but i dont know if the file will start with xyz or abc .....or anything else it a * for me so cant replace

So it means that you have to replace say *00nn.* as *0nn.*, correct?

Consider the following ksh script:-

#!/bin/ksh
typeset -Z3 i=0           # Define as three digit with leading zeros
typeset -i j=0            # Define as a plain integer

until [ $i -gt 25 ]
do
   ((i=$i+1))
   j=$i
   mv xyz000$j.csv xyz$i.csv
done

I hope that this helps.

Robin
Liverpool/Blackburn
UK

Thats right PiKK45 *00??.csv to be replaced by *0??.csv

i have tried this and it seems like working

ls *00??.csv > temp.out
sed 's/00/0/g' temp.out > temp1.out
paste temp.out temp1.out > temp2.out
sed 's/^/mv /g' temp2.out > temp3.out
while read line
do
$line
done < temp3.out
rm temp1.out temp1.out temp2.out

Or this can be done with a find command :slight_smile:

find directory_with_out_sub_directory -type f -name "*00??.csv" -exec sh -c 'mv $1 $(echo "$1" | sed 's/00/0/g')' dummy {} \;

Hope this helps :slight_smile: