LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-26-2006, 08:46 AM   #1
Snickar-Lasse
LQ Newbie
 
Registered: Nov 2005
Location: Boston, MA
Distribution: OpenSUSE 11.2
Posts: 19

Rep: Reputation: 0
A question about renaming files?


I've a bunch of files that are named in the following way:

...
MOD06_L2.A2005001.0920.005.2006202213150.V03_COT
MOD06_L2.A2005001.1100.005.2006202212812.V03_COT
MOD06_L2.A2005002.1005.005.2006202234011.V03_COT
MOD06_L2.A2005004.1130.005.2006203023906.V03_COT
MOD06_L2.A2005005.0855.005.2006203040307.V03_COT
...

which I would like rename to

...
MOD06_L2.A2005001.0920.005.V03_COT
MOD06_L2.A2005001.1100.005.V03_COT
MOD06_L2.A2005002.1005.005.V03_COT
MOD06_L2.A2005004.1130.005.V03_COT
MOD06_L2.A2005005.0855.005.V03_COT
...

Accordingly, I want to remove the 13 digits in front of the suffix. Can anybody tell me how do I do this in an easy way?

Thanks
 
Old 11-26-2006, 09:11 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974
Code:
for i in *
do
  mv $i $(echo $i | cut -c -26,41- )
done
assuming that all files are in the current directory, and ALL files are to be renamed, if not replace the * with $(ls *_COT) or something
 
Old 11-26-2006, 09:21 AM   #3
Snickar-Lasse
LQ Newbie
 
Registered: Nov 2005
Location: Boston, MA
Distribution: OpenSUSE 11.2
Posts: 19

Original Poster
Rep: Reputation: 0
Great, thanks!
 
Old 11-26-2006, 09:24 AM   #4
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 671Reputation: 671Reputation: 671Reputation: 671Reputation: 671Reputation: 671
You can use sed to remove the numbers:

Code:
for file in $(ls MOD06_*); do
   echo mv $file $(echo $file | \
   sed 's/\(.\{27\}\).\{14\}\(.*\)/\1\2/') -v
done
Oops, I was slow testing my solution.
 
Old 11-26-2006, 10:15 AM   #5
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974
Well i was heading in that sort of directoin, but as the format was so rigid, character locations was good enough it seemed.
 
Old 11-26-2006, 02:18 PM   #6
soggycornflake
Member
 
Registered: May 2006
Location: England
Distribution: Slackware 10.2, Slamd64
Posts: 249

Rep: Reputation: 31
Well, in zsh I'd just do

Code:
mmv  *.*.V03_COT \$1.V03_COT
(alias mmv='noglob zmv -w'; autoload zmv)

For non-zsh users, perl might be more appropriate:

Code:
perl -we 'for (@ARGV) { $old=$_; s/^(.*?\.)[^.]*\.(V03_COT)$/$1$2/ && rename $old, $_; }' *
Certainly beats counting characters!
 
Old 11-26-2006, 04:04 PM   #7
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974
Quote:
Originally Posted by soggycornflake
Code:
perl -we 'for (@ARGV) { $old=$_; s/^(.*?\.)[^.]*\.(V03_COT)$/$1$2/ && rename $old, $_; }' *
Certainly beats counting characters!
youre' joking right??? why on earth would you bother with a contrived regex when you can just count to 41???
 
Old 11-26-2006, 04:10 PM   #8
soggycornflake
Member
 
Registered: May 2006
Location: England
Distribution: Slackware 10.2, Slamd64
Posts: 249

Rep: Reputation: 31
Quote:
youre' joking right??? why on earth would you bother with a contrived regex when you can just count to 41???
Well, I typed the regex in 5 or 6 seconds, which is less time it takes to count characters on a screen. I suppose you could 'ls -1 *' into a file and use a text editor to display the character offsets, but that would still take longer than typing a perl one-liner. At least, for people familiar with typing perl one-liners.
 
Old 11-26-2006, 04:16 PM   #9
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974
5 seconds? sure... maybe you should call norris mcwhirter. well, if he was still alive that is.

Eschew Obfuscation!
 
Old 11-26-2006, 04:52 PM   #10
soggycornflake
Member
 
Registered: May 2006
Location: England
Distribution: Slackware 10.2, Slamd64
Posts: 249

Rep: Reputation: 31
Quote:
Originally Posted by acid_kewpie
5 seconds? sure... maybe you should call norris mcwhirter. well, if he was still alive that is.

Eschew Obfuscation!
Heh.

Obfuscation? As opposed to what? I am of the opinion that the term 'obfuscated perl' is redundant.

Who the fsck is norris mcwhirter anyway? Rings a bell, but...

FYI, I can type raw alphanumeric text at about 70-80 words a minute (with a following wind), though lots of punctuation slows me down to 50 or so. Must practice more...

The perl thing was just a suggestion, as I said, personally I'd use the zsh zmv function. With the above mmv alias, it's a mere 29 characters including the return.
 
Old 11-26-2006, 11:07 PM   #11
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,356

Rep: Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367
First hit on google: Norris McWhirter, co-founder of the Guinness Book of Records,
 
Old 11-27-2006, 12:24 PM   #12
soggycornflake
Member
 
Registered: May 2006
Location: England
Distribution: Slackware 10.2, Slamd64
Posts: 249

Rep: Reputation: 31
Quote:
First hit on google: Norris McWhirter, co-founder of the Guinness Book of Records,
Ah, cheers. Too lazy to google last night...

Back on topic, another point is that a regex is more general. If, next week, the OP has some similar files to rename that are a few characters longer or shorter, a pattern will still work, but hard-coded values will need to be recounted.

Perhaps the primary advantage of the perl method is that only 1 program is spawned. With the other methods (admittedly, including zsh's zmv), an instance of mv is spawned for each file. For a few dozen, or even a hundred or so, files this is no big deal, but if the OP has 10,000 files to rename, it becomes a significant factor.

My feeling is that my computers exist to do the laborious work for me. I didn't spend hundreds of pounds on all this fancy electronic equipment just I could sit in front of it counting characters on a screen. Better to exercise those regex skills.

Your main argument, acid_kewpie, seems to be that the main goal is simplicity. But it is not so! The main goal (for me, anyway) is elegence. Certainly, simplicity is a large factor in elegence, but as Einstein said, things should be "as simple as possible, but no simpler". Manually counting characters is just a little too simple, IMO.
 
Old 11-27-2006, 12:37 PM   #13
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974
nah, you're just totally missing the point. a dude wanted to achieve a specific task. the very simplest way i could imagine is to use cut. you'd cover tools like cut at about, hour 3 of "learning unix" you wouldn't get on to regular expressions till, oooh, hour 10 i reckon. what use is an "elegant" solution if the OP doesn't understand it (which may or may not be the case). you can't learn from it if you've no idea at all what it's all about. do you not relasie how horrible regular expressions are to a newbie?
 
Old 11-27-2006, 01:12 PM   #14
soggycornflake
Member
 
Registered: May 2006
Location: England
Distribution: Slackware 10.2, Slamd64
Posts: 249

Rep: Reputation: 31
Well, OK, but if you achieve a specific task, all you've done is achieve a specific task. Regular expressions open the door to further possibilities, isn't that part of what LQ is about? Yes, your original solution solved the immediate problem, simply and succinctly, as the OP ackowledged. The following posts are just looking at "other ways", and the OP is free to explore those at his/her leisure, or not.

Quote:
do you not relasie how horrible regular expressions are to a newbie?
Nope. I found them fascinating from the first moment I saw one. Maybe I should get out more?
 
Old 11-27-2006, 01:34 PM   #15
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974
of course regular expressions are more useful, but only if you understand them enough to benefit from them. if you don't understand regular expressions and have a problem you need to fix somehow, anyhow, then you're going to be more inclined to use an alternative method than to go off and learn about regular expressions for 5 hours. sure it's good to know they exist, but more often than not it's about solving the problem in hand, not so much the learning. and it's still chuffing useful to learn ho to use cut anyway.

Since when were fascinating and horrible mutually exclusive? you don't think erm... Jordan and Peter Andre are fascinating? OK bad example...
 
  


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
renaming files starwarsfan982 Linux - Software 7 10-30-2006 02:06 PM
a question about renaming files in mass! zeltak Linux - Software 1 05-24-2006 06:12 AM
renaming files TomalakBORG Linux - Newbie 4 12-24-2005 10:14 AM
Renaming files TomalakBORG Linux - Newbie 2 12-05-2005 04:13 PM
Renaming files in one go saurya_s Linux - Software 1 01-12-2004 01:16 PM


All times are GMT -5. The time now is 07:30 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration