Rename files to match file list pattern


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Rename files to match file list pattern
# 1  
Old 06-08-2016
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).

Code:
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

Code:
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

Code:
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

Code:
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

Code:
#cat pattern.txt
575 8888
123 9999

Thanks



Moderator's Comments:
Mod Comment Please use code tags as required by forum rules!

Last edited by RudiC; 06-08-2016 at 02:03 PM.. Reason: Added code tags.
# 2  
Old 06-08-2016
Here is one approach:-
Code:
#!/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.
# 3  
Old 06-08-2016
Mayhap sth. along this line (untested):

Code:
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.
# 4  
Old 06-08-2016
Hi.

Other possibilities to investigate:
Code:
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
# 5  
Old 06-08-2016
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.
# 6  
Old 06-08-2016
Quote:
Originally Posted by RudiC
Mayhap sth. along this line (untested):

Code:
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.
Quote:
Originally Posted by lxdorney
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!

Last edited by Aia; 06-08-2016 at 09:49 PM..
These 2 Users Gave Thanks to Aia For This Post:
# 7  
Old 06-09-2016
My apologies for my question
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Rename file using partial match to another

In the below I am trying to rename the contents within each data subfolder in a specific run, based on a partial match of the IonCode_0000_ in each file in the data subdirectory to $1 in f1. There will be multiple runs in f1 but each run in $uniq is unique and will be found in f1 and the rename... (27 Replies)
Discussion started by: cmccabe
27 Replies

2. Shell Programming and Scripting

Bash to rename portion of file using match to another

In the portion of bash below I am using rename to match the $id variable to $file and when a match (there will alwsys be one) is found then the $id is removed from each bam and bam.bai in $file and _test is added to thee file name before the extension. Each of the variables is set correctly but... (3 Replies)
Discussion started by: cmccabe
3 Replies

3. Shell Programming and Scripting

Rename specific file extension in directory with match to another file in bash

I have a specific set (all ending with .bam) of downloaded files in a directory /home/cmccabe/Desktop/NGS/API/2-15-2016. What I am trying to do is use a match to $2 in name to rename the downloaded files. To make things a more involved the date of the folder is unique and in the header of name... (1 Reply)
Discussion started by: cmccabe
1 Replies

4. Shell Programming and Scripting

Rearrange or replace only the second line after pattern match or pattern match

Im using the command below , but thats not the output that i want. it only prints the odd and even numbers. awk '{if(NR%2){print $0 > "1"}else{print $0 > "2"}}' Im hoping for something like this file1: Text hi this is just a test text1 text2 text3 text4 text5 text6 Text hi... (2 Replies)
Discussion started by: invinzin21
2 Replies

5. Shell Programming and Scripting

Help needed - Split large file into smaller files based on pattern match

Help needed urgently please. I have a large file - a few hundred thousand lines. Sample CP START ACCOUNT 1234556 name 1 CP END ACCOUNT CP START ACCOUNT 2224444 name 1 CP END ACCOUNT CP START ACCOUNT 333344444 name 1 CP END ACCOUNT I need to split this file each time "CP START... (7 Replies)
Discussion started by: frustrated1
7 Replies

6. Shell Programming and Scripting

Batch rename files that do not match pattern

Hi all, I do not have programming experience and mostly use one-liner or sometimes more to get the job done. I am having problem to batch rename the files that do not match a particular pattern. Sample file-names from directory: Meeting_Packages.pdf 13_textfile0 19_textfile0 ... (3 Replies)
Discussion started by: atulkakrana
3 Replies

7. Shell Programming and Scripting

AWK match $1 $2 pattern in file 1 to $1 $2 pattern in file2

Hi, I have 2 files that I have modified to basically match each other, however I want to determine what (if any) line in file 1 does not exist in file 2. I need to match column $1 and $2 as a single string in file1 to $1 and $2 in file2 as these two columns create a match. I'm stuck in an AWK... (9 Replies)
Discussion started by: right_coaster
9 Replies

8. Shell Programming and Scripting

Batch rename files after internal search pattern

Hello all I am trying to do a script that would allow me to mass rename files after a grep search within them. They are XML files and the pattern I would like to append to the file name is easy to find (e.g. <filename>foo</filename>), but I can't for the life of me find out how to do it and... (2 Replies)
Discussion started by: aavv
2 Replies

9. UNIX for Dummies Questions & Answers

List files that do not match the search pattern

I need to list the files that do not match the search pattern: Example: cat file1 This is how it should work cat file2 This is why I like Unix grep -option? Unix * (or some other command) returns file1 (7 Replies)
Discussion started by: olapxpert
7 Replies

10. IP Networking

List files that do not match the search pattern

I need to list the files that do not match the search pattern: Example: cat file1 This is how it should work cat file2 This is why I like Unix grep -option? Unix * (or some other command) returns file1 (1 Reply)
Discussion started by: olapxpert
1 Replies
Login or Register to Ask a Question