LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   A question about renaming files? (https://www.linuxquestions.org/questions/linux-newbie-8/a-question-about-renaming-files-504971/)

Snickar-Lasse 11-26-2006 08:46 AM

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

acid_kewpie 11-26-2006 09:11 AM

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

Snickar-Lasse 11-26-2006 09:21 AM

Great, thanks!

jschiwal 11-26-2006 09:24 AM

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.

acid_kewpie 11-26-2006 10:15 AM

Well i was heading in that sort of directoin, but as the format was so rigid, character locations was good enough it seemed.

soggycornflake 11-26-2006 02:18 PM

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!

acid_kewpie 11-26-2006 04:04 PM

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???

soggycornflake 11-26-2006 04:10 PM

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.

acid_kewpie 11-26-2006 04:16 PM

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

Eschew Obfuscation!

soggycornflake 11-26-2006 04:52 PM

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.

chrism01 11-26-2006 11:07 PM

First hit on google: Norris McWhirter, co-founder of the Guinness Book of Records, :)

soggycornflake 11-27-2006 12:24 PM

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. :)

acid_kewpie 11-27-2006 12:37 PM

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?

soggycornflake 11-27-2006 01:12 PM

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?

acid_kewpie 11-27-2006 01:34 PM

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...


All times are GMT -5. The time now is 12:04 AM.