LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   rename bulk files (https://www.linuxquestions.org/questions/programming-9/rename-bulk-files-735922/)

scofiled83 06-26-2009 07:03 PM

rename bulk files
 
Hello


I have bulk files like:

prd1_w1p1_0000009319_1_200906270750
prd1_w1p1_0000009320_1_200906270750
prd1_w1p1_0000009321_1_200906270750
...
...
....



I want to rename them to

w1p1_0000009319_1.arc
w1p1_0000009320_1.arc
..
..
..


What is the best technique to perform this action?
I want to learn rather than copying pasting the script.
HP unix.

Thanks in advance

billymayday 06-26-2009 07:25 PM

Use rename?

scofiled83 06-26-2009 07:42 PM

I have to use it in for loop to rename these files as I want.
However I am little bit confused.

ghostdog74 06-26-2009 07:51 PM

Code:

awk 'BEGIN{FS="_"}
{
 m=split(FILENAME,file,"_")
 newfile = file[2] FS file[3] FS file[4]".arc"
 cmd="mv "FILENAME" "newfile
 system(cmd)
 nextfile
}' prd*

of if you don't mind a ready script (python), see my sig last link called File renamer
Code:

# filerenamer.py -p "prd1_(.*?)_\d+$" -e "\1.arc" -l "*"
==>>>>  [ /home/prd1_w1p1_0000009320_1_200906270750 ]==>[ /home/w1p1_0000009320_1.arc ]
==>>>>  [ /home/prd1_w1p1_0000009319_1_200906270750 ]==>[ /home/w1p1_0000009319_1.arc ]
==>>>>  [ /home/prd1_w1p1_0000009321_1_200906270750 ]==>[ /home/w1p1_0000009321_1.arc ]

remove -l option to commit changes

scofiled83 06-26-2009 08:09 PM

Your ready script is little bit complicated,hard to understand :)
I tried your first suggestion:
After I run the script, it modified first file (2466) and then it gives error like:
It didnt modified the rest of the files.It only modified one file


mv: prd1_w1p1_0000002466_1_200906170433: cannot access: No such file or directory
mv: prd1_w1p1_0000002466_1_200906170433: cannot access: No such file or directory
mv: prd1_w1p1_0000002466_1_200906170433: cannot access: No such file or directory
mv: prd1_w1p1_0000002466_1_200906170433: cannot access: No such file or directory
..
..
..

billymayday 06-26-2009 08:22 PM

How about
Code:

for i in prd1* ; do mv $i `echo $i | sed 's/.*prd1_w1p1_\(.*\)_1_.*/w1p1_\1\.arc/'` ; done

scofiled83 06-26-2009 08:36 PM

Thanks for suggestions

It worked


Many thanks
Cheers

ghostdog74 06-26-2009 09:59 PM

Quote:

Originally Posted by scofiled83 (Post 3587662)
Thanks for suggestions

It worked


Many thanks
Cheers

and you understand that sed statement??anyway, you can use bash without external tools
Code:

for i in prd1*
do
    newname=${i/prd1_/}
    newname=${newname%_*}".arc"
    echo "mv $i $newname "
done


scofiled83 06-26-2009 10:03 PM

Thanks Ghost

ghostdog74 06-26-2009 10:06 PM

Quote:

Originally Posted by scofiled83 (Post 3587642)
Your ready script is little bit complicated,hard to understand :)

you don't have to understand the inside of the script. Just run it like i showed you.

Quote:

I tried your first suggestion:
After I run the script, it modified first file (2466) and then it gives error like:
It didnt modified the rest of the files.It only modified one file
i edited the awk script to include nextfile statement. it should work now.

billymayday 06-26-2009 10:07 PM

If you want to understand the sed statement, have a look at http://www.ibm.com/developerworks/li...ry/l-sed1.html

The use of \(.*\) is covered on page 2.

scofiled83 06-26-2009 10:16 PM

Thanks all


All times are GMT -5. The time now is 10:48 PM.