need a little help with renaming files

need a little help with renaming files

am 19.04.2008 20:50:45 von Luuk

Hi,

I was converting some *.m4a files to *.mp3 and ended up with files like:
"name.wav.mp3"

i want to rename them from "name.wav.mp3" to "name.mp3"

i created the following (bash-)script:
#!/bin/bash

s=$1
n=${s/$2.$3/$3}

mv -v "$1" "$n"


i can do:
$ script name.wav.mp3 wav mp3
and this will rename my file

but i do not like this 2nd and 3rd parameter, so i want to do:
$ script name.wav.mp3
but how can this be done?

can anyone point me to the right direction?

--
Luuk

Re: need a little help with renaming files

am 19.04.2008 21:29:30 von Stephane CHAZELAS

2008-04-19, 20:50(+02), Luuk:
[...]
> I was converting some *.m4a files to *.mp3 and ended up with files like:
> "name.wav.mp3"
>
> i want to rename them from "name.wav.mp3" to "name.mp3"
[...]

zmv '(*).wav.mp3' '$1.mp3'

(zmv is a zsh autoloadable function).

--
Stéphane

Re: need a little help with renaming files

am 19.04.2008 22:40:03 von gazelle

In article <0hrpd5-brr.ln1@a62-251-88-195.adsl.xs4all.nl>,
Luuk wrote:
>Hi,
>
>I was converting some *.m4a files to *.mp3 and ended up with files like:
>"name.wav.mp3"
>
>i want to rename them from "name.wav.mp3" to "name.mp3"

The best way to do this, under the following very common assumptions:
1) You've not done this sort of thing before and am not real
comfortable with it (this is why you're asking here).
2) You probably won't be doing this again for quite some time
(i.e., this is a one-off).

is to do it like this:

ls > foo
vi foo

Bring up foo in an editor (usually vi, but use what you are comfortable with) and change every instance of:

name.wav.mp3
to:
mv name.wav.mp3 name.mp3

The advantage here is that you can see onscreen what is happening -
makes for fewer surprises. When you are happy with it, save and exit
your editor, then "source" foo.

Note that if you are using GNU tools, there are some options to the "mv"
command, such as "-v" that should be useful. Note also that if you have
access to the "mmv" command (very useful, BTW), then you can use that
for further safety.

The point to all this is that safety is the important thing, not
elegance or efficiency. Take it from one who knows - who has on
occasion worked up spiffy (but not quite spiffy enough) shell solutions
to this - only to have something go wrong.

Re: need a little help with renaming files

am 19.04.2008 23:23:17 von mop2

I think this is what you want:

#!/bin/bash
mv -v "$1" "${1/.wav/}"



Luuk wrote:
> Hi,
>
> I was converting some *.m4a files to *.mp3 and ended up with files like:
> "name.wav.mp3"
>
> i want to rename them from "name.wav.mp3" to "name.mp3"
>
> i created the following (bash-)script:
> #!/bin/bash
>
> s=$1
> n=${s/$2.$3/$3}
>
> mv -v "$1" "$n"
>
>
> i can do:
> $ script name.wav.mp3 wav mp3
> and this will rename my file
>
> but i do not like this 2nd and 3rd parameter, so i want to do:
> $ script name.wav.mp3
> but how can this be done?
>
> can anyone point me to the right direction?
>
> --
> Luuk

Re: need a little help with renaming files

am 19.04.2008 23:27:56 von mop2

I think this is what you want:

#!/bin/bash
mv -iv "$1" "${1/.wav/}" # "i" = interative if destination exists



Luuk wrote:
> Hi,
>
> I was converting some *.m4a files to *.mp3 and ended up with files like:
> "name.wav.mp3"
>
> i want to rename them from "name.wav.mp3" to "name.mp3"
>
> i created the following (bash-)script:
> #!/bin/bash
>
> s=$1
> n=${s/$2.$3/$3}
>
> mv -v "$1" "$n"
>
>
> i can do:
> $ script name.wav.mp3 wav mp3
> and this will rename my file
>
> but i do not like this 2nd and 3rd parameter, so i want to do:
> $ script name.wav.mp3
> but how can this be done?
>
> can anyone point me to the right direction?
>
> --
> Luuk

Re: need a little help with renaming files

am 20.04.2008 12:33:12 von Dave B

On Saturday 19 April 2008 23:27, mop2 wrote:

> I think this is what you want:
>
> #!/bin/bash
> mv -iv "$1" "${1/.wav/}" # "i" = interative if destination exists

Given that sometimes audio files tend to have, so to speak, particular or
funny names, maybe a safer one could be this:

mv -i -- "$1" "${1%.wav.mp3}.mp3"

or, with bash+GNU mv

mv -iv -- "$1" "${1/%.wav.mp3/.mp3}"

--
D.

Re: need a little help with renaming files

am 20.04.2008 14:43:56 von gazelle

In article ,
Dave B wrote:
>On Saturday 19 April 2008 23:27, mop2 wrote:
>
>> I think this is what you want:
>>
>> #!/bin/bash
>> mv -iv "$1" "${1/.wav/}" # "i" = interative if destination exists
>
>Given that sometimes audio files tend to have, so to speak, particular or
>funny names, maybe a safer one could be this:
>
>mv -i -- "$1" "${1%.wav.mp3}.mp3"
>
>or, with bash+GNU mv
>
>mv -iv -- "$1" "${1/%.wav.mp3/.mp3}"

Which is a further argument in favor of doing this carefully and safely,
as indicated in my previous post. It is easier to get the quoting right
when the commands are in front of you onscreen, rather than trying out
some cryptic command that is likely to do some damage before you figure
out what is going on.

Re: need a little help with renaming files

am 20.04.2008 15:14:19 von Luuk

Kenny McCormack schreef:
> In article <0hrpd5-brr.ln1@a62-251-88-195.adsl.xs4all.nl>,
> Luuk wrote:
>> Hi,
>>
>> I was converting some *.m4a files to *.mp3 and ended up with files like:
>> "name.wav.mp3"
>>
>> i want to rename them from "name.wav.mp3" to "name.mp3"
>
> The best way to do this, under the following very common assumptions:
> 1) You've not done this sort of thing before and am not real
> comfortable with it (this is why you're asking here).
> 2) You probably won't be doing this again for quite some time
> (i.e., this is a one-off).
>
> is to do it like this:
>
> ls > foo
> vi foo
>
> Bring up foo in an editor (usually vi, but use what you are comfortable with) and change every instance of:
>
> name.wav.mp3
> to:
> mv name.wav.mp3 name.mp3
>
> The advantage here is that you can see onscreen what is happening -
> makes for fewer surprises. When you are happy with it, save and exit
> your editor, then "source" foo.
>
> Note that if you are using GNU tools, there are some options to the "mv"
> command, such as "-v" that should be useful. Note also that if you have
> access to the "mmv" command (very useful, BTW), then you can use that
> for further safety.
>
> The point to all this is that safety is the important thing, not
> elegance or efficiency. Take it from one who knows - who has on
> occasion worked up spiffy (but not quite spiffy enough) shell solutions
> to this - only to have something go wrong.
>

sure, the SAFE way....
i do (did) this a lot (solving the probling using VI),
but i wanted to go for an option to script this, and 'solve' this in a
more generic way....;-)

--
Luuk

Re: need a little help with renaming files

am 20.04.2008 15:15:24 von Luuk

Stephane CHAZELAS schreef:
> 2008-04-19, 20:50(+02), Luuk:
> [...]
>> I was converting some *.m4a files to *.mp3 and ended up with files like:
>> "name.wav.mp3"
>>
>> i want to rename them from "name.wav.mp3" to "name.mp3"
> [...]
>
> zmv '(*).wav.mp3' '$1.mp3'
>
> (zmv is a zsh autoloadable function).
>

too lazy to learn zsh, and its disadvantages or advantages,
because i'm still bussy getting to know more about bash ... ;-)

--
Luuk

Re: need a little help with renaming files

am 20.04.2008 15:22:19 von Luuk

Dave B schreef:
> On Saturday 19 April 2008 23:27, mop2 wrote:
>
>> I think this is what you want:
>>
>> #!/bin/bash
>> mv -iv "$1" "${1/.wav/}" # "i" = interative if destination exists
>
> Given that sometimes audio files tend to have, so to speak, particular or
> funny names, maybe a safer one could be this:
>
> mv -i -- "$1" "${1%.wav.mp3}.mp3"
>
> or, with bash+GNU mv
>
> mv -iv -- "$1" "${1/%.wav.mp3/.mp3}"
>

yes, i did already have the quotes around my filenames, because of the
spaces in the filenames...

anyway, the "${1/%...." option, is new to /me, and helped a lot

i have to read the 'man bash' page again, i think,
and this time try to read the whole story
so, not stop before the lines about 'Parameter Expansion' (line 756-)
... ;-)

thanks for the answers!

--
Luuk

Re: need a little help with renaming files

am 20.04.2008 18:33:05 von Dave B

On Sunday 20 April 2008 14:43, Kenny McCormack wrote:

>>Given that sometimes audio files tend to have, so to speak, particular or
>>funny names, maybe a safer one could be this:
>>
>>mv -i -- "$1" "${1%.wav.mp3}.mp3"
>>
>>or, with bash+GNU mv
>>
>>mv -iv -- "$1" "${1/%.wav.mp3/.mp3}"
>
> Which is a further argument in favor of doing this carefully and safely,
> as indicated in my previous post. It is easier to get the quoting right
> when the commands are in front of you onscreen, rather than trying out
> some cryptic command that is likely to do some damage before you figure
> out what is going on.

With the shell approach there are no quoting problems whatsoever. The fact
is, with a shell approach you can just do

....
newname=${whatever}
mv -- "$name" "$newname"

and be done with that, without having to worry about special characters.
(if you care, you can do
printf "name is %s, newname is %s\n" "$name" "$newname"
before doing the actual mv, to check that everything is ok; it usually is).

With vim, on the other hand, you're writing the actual filenames in mv
commands, so you have to be *much more* careful. If filenames don't have
single quotes, that that's easy. But if they do (and music files probably
do), you have to properly escape and quote everything, and it's easier
(imho) to visually miss wrongly escaped characters, especially if you have
200 filenames like

03 - "Want $$?"--'Take this sh*t!'(hax0r_rul3z).wav.mp3

(ok that's just an example, of course, but hopefully you got the idea).

But then, if you do that with vim, then you can also do the same with sed:
you can still inspect the result before feeding it to sh, and it has the
advantage of being more easily scriptable.

Note: I'm all for (awk/sed | sh) -based approaches to mass renaming files,
when the names have regular and predictable patterns. In the case of music
files, that is not usually true, so I prefer a shell-based solution.

--
D.

Re: need a little help with renaming files

am 20.04.2008 18:52:36 von gazelle

In article ,
Dave B wrote:
....
>Note: I'm all for (awk/sed | sh) -based approaches to mass renaming files,
>when the names have regular and predictable patterns. In the case of music
>files, that is not usually true, so I prefer a shell-based solution.

Yes, you do. Because you are (presumably) competent, and have done this
before. You know enough to try it out in a test directory first, and to
proceed cautiously until you are familiar with what's going on.

The problem is that people post shell solutions on newsgroups and the OP
tries them out directly, without doing the necessary QA. And there's
always _something_ just a little bit subtly wrong with the posted
solution. You (meaning "one", not you, "Dave B") are always going to
have to massage it a little to make it work for you, in _your_
environment.

I would never just pipe it into sh, as you indicate above. That blows
the safety of writing it to a file, inspecting the file, and then,
finally, sourcing the file.

Re: need a little help with renaming files

am 20.04.2008 19:00:36 von Dave B

On Sunday 20 April 2008 18:52, Kenny McCormack wrote:

> Yes, you do. Because you are (presumably) competent, and have done this
> before. You know enough to try it out in a test directory first, and to
> proceed cautiously until you are familiar with what's going on.
>
> The problem is that people post shell solutions on newsgroups and the OP
> tries them out directly, without doing the necessary QA. And there's
> always _something_ just a little bit subtly wrong with the posted
> solution. You (meaning "one", not you, "Dave B") are always going to
> have to massage it a little to make it work for you, in _your_
> environment.

Sorry for being slow to understand, but my point is that a shell solution
can be tried directly without problems by everyone. The OP could have
copied/pasted my solution directly and used it on *any* file, regardless of
its name, and it would have worked.
The vim approach imho gives a false sense of security, because you simply
can't be sure you've properly escaped everything in a list of 2000
arbitrarily complex filenames, even if you are sitting in front of it.

> I would never just pipe it into sh, as you indicate above. That blows
> the safety of writing it to a file, inspecting the file, and then,
> finally, sourcing the file.

If you know in advance that all the files are named

AAABBBCCC.aaa

where AAA, BBB and CCC are numbers, and aaa are letters, there is no reason
to worry in piping the output to sh. Simply, that is not the case with
music (or multimedia, for that matter) files.

--
D.

Re: need a little help with renaming files

am 20.04.2008 21:42:34 von Marcel Bruinsma

In article <0hrpd5-brr.ln1@a62-251-88-195.adsl.xs4all.nl>,
Luuk wrote:

> I was converting some *.m4a files to *.mp3 and ended up with files like:
> "name.wav.mp3"
>
> i want to rename them from "name.wav.mp3" to "name.mp3"

$ rename .wav. . *.wav.mp3

gr.

Re: need a little help with renaming files

am 20.04.2008 21:51:41 von Luuk

Marcel Bruinsma schreef:
> In article <0hrpd5-brr.ln1@a62-251-88-195.adsl.xs4all.nl>,
> Luuk wrote:
>
>> I was converting some *.m4a files to *.mp3 and ended up with files like:
>> "name.wav.mp3"
>>
>> i want to rename them from "name.wav.mp3" to "name.mp3"
>
> $ rename .wav. . *.wav.mp3
>
> gr.
>

thanks/bedankt/merci

many ways to Rome...

--
Luuk