Renaming Glob to bash

I'm new to bash, so sorry if this is kind of a basic question. I tried to rename a bunch of mp3 files to add 1-to their file names, but it mv *.mp3 1-*.mp3doesn’t work. So I tried scripting it, first with an echo to check the commands:

for f in *.mp3 ; do echo mv \'$f\' \'1-$f\'; done

It seems that the commands that I like are outputting, so I removed the echo by changing the command to

for f in *.mp3 ; do mv \'$f\' \'1-$f\'; done

What mistake. Then I tried to execute commands so on

for f in *.mp3 ; do echo mv \'$f\' \'1-$f\'; done | /bin/sh

What worked, but if someone could tell me why the middle team is not working, I would be interested to know. Or, if there is a more elegant single-liner that will do what I wanted, I would be interested to see this too.

+5
2

,

for f in *.mp3 ; do mv "$f" "1-$f"; done

- 'file1.mp3' '1-file1.mp3' mv ( ).

+7

:

rename -n 's/^/1-/' *.mp3

-n, . man rename .

+7

All Articles