|
|
|
|
||||||
| comp.unix.shell Using and programming the Unix shell. |
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#9 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#10 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#11 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#12 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#13 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#14 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#15 |
|
Messages: n/a
Hébergeur: |
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 |
|
![]() |
| Outils de la discussion | |
|
|