LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   mv command (https://www.linuxquestions.org/questions/linux-newbie-8/mv-command-581003/)

hpladd 08-30-2007 11:54 AM

mv command
 
I have a directory called command_expansion_test.

The directory contains the files: test.ogg, test2.ogg, test3.ogg.

Using the mv command, how can I change the filenames to test.mp3, test2.mp3, test3.mp3 ?

colucix 08-30-2007 12:09 PM

If you have the rename command, it straightforward as
Code:

rename .ogg .mp3 *.mp3
otherwise you can use sed to substitute the extension as in
Code:

for i in `ls *.mp3`
do
mv $i $(echo $i | sed s/.ogg$/.mp3/)
done


hpladd 08-30-2007 01:01 PM

Code:

for i in `ls *.mp3`
do
mv $i $(echo $i | sed s/.ogg$/.mp3/)
done

Thanks colucix:

rename would be the easy way to do it. I never seem to want to do things the easy way however.

This is mostly an academic exercise for me. I was at a LUG last night where the presenter used a string very similar to the one you provided to perform a *.ogg to *.mp3 name change to files in a directory.

I've been banging my head against a wall trying to come up with a single string that can do it, but now I realize that it probably was a script.

Junior Hacker 08-30-2007 01:11 PM

Well
I would say you're on the right track, I usually refer to this document for such tasks.

colucix 08-31-2007 04:01 AM

Quote:

Originally Posted by hpladd (Post 2875991)
I've been banging my head against a wall trying to come up with a single string that can do it, but now I realize that it probably was a script.

Not really a script: you can launch a for loop directly in the command line. As you can see from the document suggested by Junior Hacker, usually there are a lot of ways to perform the same task. It is a matter of efficiency or elegance or whatever you are focused on.

hpladd 08-31-2007 09:07 AM

Quote:

Originally Posted by colucix (Post 2876576)
Not really a script: you can launch a for loop directly in the command line. As you can see from the document suggested by Junior Hacker, usually there are a lot of ways to perform the same task. It is a matter of efficiency or elegance or whatever you are focused on.

The reason I have a love/hate relationship with Linux: I start out trying to learn how to do some seemingly trivial task, and discover a relatively broad new facet to Linux (and computers). I believe I just stumbled upon "structured Language Constructs," as defined here http://www.freeos.com/guides/lsst/index.html

Thanks for your help!


All times are GMT -5. The time now is 10:55 AM.