Skip to main content
2 of 3
added 510 characters in body
Andy Dalton
  • 14.7k
  • 1
  • 28
  • 50

By quoting the $(..), you get one token and not N tokens:

I start out with a couple sample files:

$ ls
a.mp3 b.mp3 c.mp3

If I do what you did, I get one line with all three:

for i in "$(ls *.mp3)"; do
    echo "--> $i"
done
--> a.mp3 b.mp3 c.mp3

If I omit the quotes around the $(...), I get three different output lines:

for i in $(ls *.mp3); do
    echo "--> $i"
done
--> a.mp3
--> b.mp3
--> c.mp3

If you have files with spaces, then something like this might solve your problem (note that this applies only to files in the current directory):

Before

$ ls
'DaftPunk VeridisQuo.mp3'  'French79 AfterParty.mp3'  'French79 BetweentheButtons.mp3'  'French79 Hometown.mp3'

Use find to do the rename:

$ find *.mp3 -maxdepth 1 -type f -name *.mp3 -exec mv {} "electro_{}" \;
$ ls
'electro_DaftPunk VeridisQuo.mp3'  'electro_French79 AfterParty.mp3'  'electro_French79 BetweentheButtons.mp3'  'electro_French79 Hometown.mp3'
Andy Dalton
  • 14.7k
  • 1
  • 28
  • 50