How do I extract a file name into 2 parts, making one into directory and the other one inside of it?

前端 未结 4 1726
轮回少年
轮回少年 2020-12-21 18:54

I\'m trying to sort all mp3 files by artist and name. At the moment, they\'re in 1 giant file name. E.g Artist - Song name.mp3 I want to convert this to Artist/Song name.mp

4条回答
  •  时光说笑
    2020-12-21 19:21

    Just in case you don't have the rename utility. A fix on your original script.

    for f in *.mp3; do
      # extract artist and song name and remove spaces
      artist=${f%% -*}
      song=${f##*- }
      #make directory with the extracted artist name and move + rename the file into the directory
      echo mkdir -p -- "$artist" && echo mv -- "$f" "$artist/$song"
    done
    
    • Remove the echo If you're satisfied with the output.

提交回复
热议问题