Rename files to match file list pattern

Hi All,

I have 100 folders with the first delimiter has a unique name i.e (123_hello and 575_hello) and each folder have atlist 1000 plus files with naming convention i.e (575_hello_1.iso ... 575_hello_1000.iso).

575_hello/575_hello_1.iso
575_hello/575_hello_2.iso
575_hello/575_hello_3.iso
...
575_hello/575_hello_1000.iso

and

123_hello/123_hello_1.iso
123_hello/123_hello_2.iso
123_hello/123_hello_3.iso
...
123_hello/123_hello_1000.iso

What I want to do is rename all the iso files in the folders to match the pattern in my file list. like

575_hello/8888_hello_1.iso
575_hello/8888_hello_2.iso
575_hello/8888_hello_3.iso
...
575_hello/8888_hello_1000.iso

and

123_hello/9999_hello_1.iso
123_hello/9999_hello_2.iso
123_hello/9999_hello_3.iso
...
123_hello/9999_hello_1000.iso

Here is my pattern list

#cat pattern.txt
575 8888
123 9999 

Thanks

Here is one approach:-

#!/bin/ksh

typeset -A ARR_ID

while read old_id new_id
do
        ARR_ID[${old_id}]=${new_id}
done < pattern.txt

for dir in 123_hello 575_hello
do
        d_id="${dir%%_*}"
        if [ ! -z "${ARR_ID[$d_id]}" ]
        then
                for file in ${dir}/*.iso
                do
                        fname="${file##*/}"
                        print "Renaming ${file} to ${dir}/${ARR_ID[$d_id]}_${fname#*_}"
                        #mv "${file}" "${dir}/${ARR_ID[$d_id]}_${fname#*_}"
                done
        fi
done

exit 0

Run it with mv commented, verify if you are getting desired output.

Mayhap sth. along this line (untested):

while read old_id new_id
  do    (cd $old_id
         for FN in ${old_id}_hello*
           do   echo mv "$FN" "${FN/${old_id]/${new_id}}"
           done 
        )
  done < pattern.txt

Test it and remove the echo if happy with the result.

Hi.

Other possibilities to investigate:

Rename multiple files, groups of files
        1) rename -- Debian version and RedHat version differ, q.v.
           (try package util-linux:
           http://en.wikipedia.org/wiki/Util-linux)

        2) ren -- RedHat relatives

        3) renameutils -- package contains qmv, imv, icp, qcp, and deurlname

        4) mved -- (circa 2006; good as of 2015.05), perl
           http://raf.org/mved/
           (An earlier shell version may be available.)

        5) rename -- perl builtin library routine (DIY)

        6) mmv -- move/rename/copy/append/link multiple files by wildcard patterns

        7) gprename - batch rename using a GUI

        8) krename - batch rename using a GUI

Best wishes ... cheers, drl

All posted is not working.
@Yoda can you make your script not specific to 2 folder, because we have thousand of folders.

@Rudic

when I run your script I got error message:
./test1.sh: line 4: unexpected EOF while looking for matching `"'
./test1.sh: line 8: syntax error: unexpected end of file

Thanks very much.

Substitute the highlighted `]' for a `}'

Please, remember that the ultimate goal is for you to learn how to make that change that you are asking Yoda to do for you. Instead, you could ask: Is this the way I should change your code to not be specific to 2 folders? Then, you post what you have tried!

2 Likes

My apologies for my question

Yes, sorry for the typo. Do as Aia said, replace old_id] with old_id} .

And, I fully second Aia saying that you need to put in some - if not serious - efforts to master problems by yourself, not just copy-and-paste ready-made solutions.

Yes thanks for the advice, really appreciated