Trying to use mv in a shell script but for some reason this does not work:
for f in *.wav;do mv $f $f.bwf;done
I get this:
usage: mv [-f | -i | -n] [-v] source target
mv [-f | -i | -n] [-v] source ... directory
So it's like I'm using 'mv' wrong but I can't see how.
This works so the contens of the folder is read properly in the for-loop:
for f in *.wav;do echo $f;done
---------- Post updated at 01:56 AM ---------- Previous update was at 01:43 AM ----------
There are blank spaces in the filenames, do I need to take that into account?
Yes! Try:
for f in *.wav;do mv "$f" "$f.bwf";done
Yes, that works. Thanks!
But I realized i need to remove the ".wav" before adding the ".bwf".
Which is the most convenient method for that?
---------- Post updated at 10:33 AM ---------- Previous update was at 10:15 AM ----------
A tiny remark for others like me who've missed this.
I edited my script in the standard macOS Texteditor and the quotation character I get there is not the right one for bash. At least not with a Swedish keyboard.
The one used in Don's post works and looks like this:
"
The one I get from the Texteditor does not work and looks like this:
�
A tiny but significant difference. Almost invisible in some typefaces!
So, we move up to the next level in using shell variables (adding parameter expansions to the steps involved):
for f in *.wav;do mv "$f" "${f%wav}bwf";done
And, concerning your "tiny remark"; we usually recommend that you learn to use vi
when editing shell scripts instead of using "pretty-printing editors" like TextEdit
. The vi
editor may have a steep learning curve, but you will learn all about using regular expressions (that you can use in sed
and grep
and find
) and editing commands that you can use not only in vi
but also in sed
, shell command history expansions, ed
, ex
, and cursor motion commands that apply in many other interactive commands, AND you'll get the single-quotes, double-quotes, and back-quotes you need without a pretty-printing editor's thinking that other characters might look prettier but then make your scripts fail miserably since shells don't recognize them as valid syntax.
Thanks, that works nicely.
I should have a look at vi.