LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 06-26-2009, 08:03 PM   #1
scofiled83
Member
 
Registered: Nov 2008
Posts: 70

Rep: Reputation: 15
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
 
Old 06-26-2009, 08:25 PM   #2
billymayday
Guru
 
Registered: Mar 2006
Location: Sydney, Australia
Distribution: Fedora, CentOS, OpenSuse, Slack, Gentoo, Debian, Arch, PCBSD
Posts: 6,678

Rep: Reputation: 122Reputation: 122
Use rename?
 
Old 06-26-2009, 08:42 PM   #3
scofiled83
Member
 
Registered: Nov 2008
Posts: 70

Original Poster
Rep: Reputation: 15
I have to use it in for loop to rename these files as I want.
However I am little bit confused.
 
Old 06-26-2009, 08:51 PM   #4
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
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

Last edited by ghostdog74; 06-26-2009 at 11:02 PM. Reason: include nextfile
 
Old 06-26-2009, 09:09 PM   #5
scofiled83
Member
 
Registered: Nov 2008
Posts: 70

Original Poster
Rep: Reputation: 15
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
..
..
..

Last edited by scofiled83; 06-26-2009 at 09:12 PM.
 
Old 06-26-2009, 09:22 PM   #6
billymayday
Guru
 
Registered: Mar 2006
Location: Sydney, Australia
Distribution: Fedora, CentOS, OpenSuse, Slack, Gentoo, Debian, Arch, PCBSD
Posts: 6,678

Rep: Reputation: 122Reputation: 122
How about
Code:
for i in prd1* ; do mv $i `echo $i | sed 's/.*prd1_w1p1_\(.*\)_1_.*/w1p1_\1\.arc/'` ; done
 
Old 06-26-2009, 09:36 PM   #7
scofiled83
Member
 
Registered: Nov 2008
Posts: 70

Original Poster
Rep: Reputation: 15
Thanks for suggestions

It worked


Many thanks
Cheers

Last edited by scofiled83; 06-26-2009 at 09:38 PM.
 
Old 06-26-2009, 10:59 PM   #8
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
Quote:
Originally Posted by scofiled83 View Post
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
 
Old 06-26-2009, 11:03 PM   #9
scofiled83
Member
 
Registered: Nov 2008
Posts: 70

Original Poster
Rep: Reputation: 15
Thanks Ghost
 
Old 06-26-2009, 11:06 PM   #10
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
Quote:
Originally Posted by scofiled83 View Post
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.
 
Old 06-26-2009, 11:07 PM   #11
billymayday
Guru
 
Registered: Mar 2006
Location: Sydney, Australia
Distribution: Fedora, CentOS, OpenSuse, Slack, Gentoo, Debian, Arch, PCBSD
Posts: 6,678

Rep: Reputation: 122Reputation: 122
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.
 
Old 06-26-2009, 11:16 PM   #12
scofiled83
Member
 
Registered: Nov 2008
Posts: 70

Original Poster
Rep: Reputation: 15
Thanks all
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Trouble with making a bash script to read in different files and rename output files. rystke Linux - Software 1 05-07-2009 09:00 AM
Batch/bulk rename with ascending dates? athens101 Linux - Software 3 04-27-2009 07:32 PM
bulk rename files atiman Linux - Newbie 5 03-05-2009 01:42 AM
shell command for bulk 'rename' ashrafulkarim Linux - Newbie 5 12-17-2008 04:46 AM
Deleting files in bulk through ftp doctorwebbox Linux - Software 2 12-25-2004 01:43 PM


All times are GMT -5. The time now is 11:05 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration