LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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 05-23-2009, 04:41 AM   #1
scofiled83
Member
 
Registered: Nov 2008
Posts: 70

Rep: Reputation: 15
changing file names


Hi all


I have files like :


xakprd1_zuw1p1_0000006232_1_200905082202
xakprd1_zuw1p1_0000006233_1_200905082217
..
..
..



How can I change them and make them like :

_0000006232_1.arc
_0000006233_1.arc
..
..
..




I appreciate your help.

Many thanks
 
Old 05-23-2009, 04:50 AM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
if you have Python
Code:
#!/usr/bin/env python
import glob
for files in glob.glob("xakprd*"):
    newname="_"+'_'.join(files.split("_")[2:4])+".arc"
    try:
        os.rename(files,newname)
    except Exception,e:
        print e
    else:
        print "Renamed file:%s to %s"%(files,newname)
output
Code:
# ls -1 xakpr*
xakprd1_zuw1p1_0000006232_1_200905082202
xakprd1_zuw1p1_0000006233_1_200905082217
# ./test.py
Renamed file:xakprd1_zuw1p1_0000006232_1_200905082202 to _0000006232_1.arc
Renamed file:xakprd1_zuw1p1_0000006233_1_200905082217 to _0000006233_1.arc
# ls -1 *.arc
_0000006232_1.arc
_0000006233_1.arc
 
Old 05-23-2009, 04:53 AM   #3
scofiled83
Member
 
Registered: Nov 2008
Posts: 70

Original Poster
Rep: Reputation: 15
I dont have phyton.

Hp unix
 
Old 05-23-2009, 04:53 AM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Use parameter substitution. First remove the latest part, from the last underscore to the end of the file, then remove the front part twice, from the beginning to the first underscore and the trick is done:
Code:
#!/bin/sh
for file in *
do
  newname=${file%_*}       # xakprd1_zuw1p1_0000006232_1
  newname=${newname#*_}    # zuw1p1_0000006232_1
  newname=${newname#*_}    # 0000006232_1
  mv $file _${newname}.arc
done
this loop assumes that all the file contained in the current directory are those you want to rename, otherwise change the wild character * to match only the needed file.

Last edited by colucix; 05-23-2009 at 05:29 AM.
 
Old 05-23-2009, 04:56 AM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by scofiled83 View Post
I dont have phyton.

Hp unix
What shell are you using? /bin/sh, /bin/tcsh....?
 
Old 05-23-2009, 04:59 AM   #6
scofiled83
Member
 
Registered: Nov 2008
Posts: 70

Original Poster
Rep: Reputation: 15
Hi Colucix
I tried that:

It makes files like:


_zuw1p1_0000006470_1_200905130301.arc

However I like:

_0000006470_1.arc




I am not sure which shell I use..

Last edited by scofiled83; 05-23-2009 at 05:10 AM.
 
Old 05-23-2009, 05:19 AM   #7
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by scofiled83 View Post
I dont have phyton.

Hp unix
Code:
ls xakprd* | awk 'BEGIN{q="\047"}
{
 m=split($0,f,"_")
 newname= "_"f[3]"_"f[4]".arc"
 cmd="mv "q$0q" "q newname q
 system(cmd)
}'
 
Old 05-23-2009, 05:29 AM   #8
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by scofiled83 View Post
Hi Colucix
I tried that:
It makes files like:
_zuw1p1_0000006470_1_200905130301.arc
Yes, sorry... my mistake. I changed the code above.
Quote:
Originally Posted by scofiled83 View Post
I am not sure which shell I use..
Try echo $SHELL to find out. Or eventually grep $USER /etc/passwd.
 
Old 05-23-2009, 05:31 AM   #9
scofiled83
Member
 
Registered: Nov 2008
Posts: 70

Original Poster
Rep: Reputation: 15
it worked ghostdog

Many thanks
 
  


Reply



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
How to stop Linux changing the case of my file (build in Windows) names? zwtang Linux - Newbie 2 06-26-2008 06:00 AM
why is growisofs changing my file names with data dvd? kryptobs2000 Linux - Software 2 01-17-2008 12:57 PM
Weird, file names changing case after restore... s2cuts Linux - General 2 03-03-2007 01:39 PM
Changing link names Merlyn MEPIS 2 01-12-2006 10:37 AM
Changing global file names. Sapient Linux - General 6 03-31-2005 05:10 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:18 PM.

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
Open Source Consulting | Domain Registration