Bash: renaming file issues.

Hi, i want to rename a group of directories and files of my music, some items are like this:

[Artist] - [Album], for directories.
[Artist] - [Song title], for files.

I want to do something like this:

[Album], for directories.
[Song title], for files.

This is my code:

#!/bin/bash
for fname in *.mp3; do
    echo item: $fname
    mv -f $fname ${fname#*-}
done

This is a reduced list of my output:

user@intel-computer:~/Music/tmp$ ./trim_filename.sh
item: 01 - Bad Romance.mp3
mv: target `Romance.mp3' is not a directory
item: 01 - Just Dance.mp3
mv: target `Dance.mp3' is not a directory
item: 02 - Alejandro.mp3
mv: target `Alejandro.mp3' is not a directory
item: 02 - LoveGame.mp3
mv: target `LoveGame.mp3' is not a directory
item: 03 - Paparazzi.mp3
mv: target `Paparazzi.mp3' is not a directory
item: 04 - Poker Face.mp3
mv: target `Face.mp3' is not a directory
item: 05 - Dance In the Dark.mp3
mv: target `Dark.mp3' is not a directory
item: 06 - Telephone.mp3
mv: target `Telephone.mp3' is not a directory
user@intel-computer:~/Music/tmp$

Can anyone throw some pointers for this?
Thank you for your kind attention.

Because your filenames contain spaces, you need to quote the variable on your move command.

mv -f "$fname" "${fname#*-}"

The way it was coded, mv was seeing more tokens than just source-file and destination-file causing it to want to treat the last token as a directory name.

1 Like

Thanks Agama for your help, the script works fine now.

#!/bin/bash
for fname in *.mp3; do
    mv -f "$fname" "${fname#*- *}"
done