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

前端 未结 4 1755
轮回少年
轮回少年 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:36

    Assuming there are many files, it's probably much faster to do this using pipes instead of a for loop. This has the additional advantage of avoiding complicated bash-specific syntax and using core unix/linux command line programs instead.

    find *-*.mp3 |
      sed 's,\([^-]\+\)\s*-\s*\(.*\),mkdir -p "\1"; mv "&" "\1"/"\2",' |
      bash
    

    Explanation:

    This find to find all the files matching -.mp3 in the current directory.

    This sed command changes each line to a command string, e.g.:

    aaa - bbb.mp3
    ->
    mkdir -p "aaa"; mv "aaa - bbb.mp3" "aaa"/"bbb.mp3"
    

    The bash command runs each of those command strings.

提交回复
热议问题