Skip to main content
added 621 characters in body
Source Link
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'

Why did I suggest find *.mp3 and not simply find . -type f -name '*.mp3' ...?

$ find . -maxdepth 1 -type f -name '*.mp3' -exec mv {} "electro_{}" \;
mv: cannot move './French79 Hometown.mp3' to 'electro_./French79 Hometown.mp3': No such file or directory
mv: cannot move './French79 BetweentheButtons.mp3' to 'electro_./French79 BetweentheButtons.mp3': No such file or directory
mv: cannot move './French79 AfterParty.mp3' to 'electro_./French79 AfterParty.mp3': No such file or directory
mv: cannot move './DaftPunk VeridisQuo.mp3' to 'electro_./DaftPunk VeridisQuo.mp3': No such file or directory

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'

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'

Why did I suggest find *.mp3 and not simply find . -type f -name '*.mp3' ...?

$ find . -maxdepth 1 -type f -name '*.mp3' -exec mv {} "electro_{}" \;
mv: cannot move './French79 Hometown.mp3' to 'electro_./French79 Hometown.mp3': No such file or directory
mv: cannot move './French79 BetweentheButtons.mp3' to 'electro_./French79 BetweentheButtons.mp3': No such file or directory
mv: cannot move './French79 AfterParty.mp3' to 'electro_./French79 AfterParty.mp3': No such file or directory
mv: cannot move './DaftPunk VeridisQuo.mp3' to 'electro_./DaftPunk VeridisQuo.mp3': No such file or directory
added 510 characters in body
Source Link
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'

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

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'
Source Link
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