PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Forums Hébergement > Forum Serveur - Sécurité et techniques > comp.unix.shell > need a little with renaming files
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
comp.unix.shell Using and programming the Unix shell.

need a little with renaming files

Réponse
 
LinkBack Outils de la discussion
Vieux 19/04/2008, 19h50   #1
Luuk
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut need a little with renaming files

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"
<END OF SCRIPT>

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
  Réponse avec citation
Vieux 19/04/2008, 20h29   #2
Stephane CHAZELAS
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: need a little with renaming files

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
  Réponse avec citation
Vieux 19/04/2008, 21h40   #3
Kenny McCormack
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: need a little with renaming files

In article <0hrpd5-brr.ln1@a62-251-88-195.adsl.xs4all.nl>,
Luuk <Luuk@invalid.lan> 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 <files> > 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.

  Réponse avec citation
Vieux 19/04/2008, 22h23   #4
mop2
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: need a little with renaming files

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"
> <END OF SCRIPT>
>
> 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

  Réponse avec citation
Vieux 19/04/2008, 22h27   #5
mop2
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: need a little with renaming files

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"
> <END OF SCRIPT>
>
> 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

  Réponse avec citation
Vieux 20/04/2008, 11h33   #6
Dave B
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: need a little with renaming files

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.
  Réponse avec citation
Vieux 20/04/2008, 13h43   #7
Kenny McCormack
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: need a little with renaming files

In article <fuf64n$uhm$1@registered.motzarella.org>,
Dave B <daveb@addr.invalid> 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.

  Réponse avec citation
Vieux 20/04/2008, 14h14   #8
Luuk
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: need a little with renaming files

Kenny McCormack schreef:
> In article <0hrpd5-brr.ln1@a62-251-88-195.adsl.xs4all.nl>,
> Luuk <Luuk@invalid.lan> 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 <files> > 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
  Réponse avec citation
Vieux 20/04/2008, 14h15   #9
Luuk
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: need a little with renaming files

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
  Réponse avec citation
Vieux 20/04/2008, 14h22   #10
Luuk
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: need a little with renaming files

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 ed 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
  Réponse avec citation
Vieux 20/04/2008, 17h33   #11
Dave B
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: need a little with renaming files

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.
  Réponse avec citation
Vieux 20/04/2008, 17h52   #12
Kenny McCormack
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: need a little with renaming files

In article <fufr7j$t77$1@registered.motzarella.org>,
Dave B <daveb@addr.invalid> 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.

  Réponse avec citation
Vieux 20/04/2008, 18h00   #13
Dave B
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: need a little with renaming files

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.
  Réponse avec citation
Vieux 20/04/2008, 20h42   #14
Marcel Bruinsma
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: need a little with renaming files

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.

  Réponse avec citation
Vieux 20/04/2008, 20h51   #15
Luuk
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: need a little with renaming files

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
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 05h37.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 1,43847 seconds with 23 queries