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

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

    By far the simplest way of doing this is to use rename a.k.a. Perl rename.

    Basically, you want to replace the sequence SPACE-DASH-SPACE with a forward slash directory separator, so the command is:

    rename --dry-run -p 's| - |/|' *mp3
    

    Sample Output

    'Artist - Song name.mp3' would be renamed to 'Artist/Song name.mp3'
    'Artist B - Song name 2.mp3' would be renamed to 'Artist B/Song name 2.mp3'
    

    If that looks correct, just remove --dry-run and run it again for real. The benefits of using rename are:

    • it can do a dry-run to test before you run for real
    • it will create all necessary directories with the -p option
    • it will not clobber (overwrite) files without warning
    • you have the full power of Perl available to you and can make your renaming as sophisticated as you wish.

    Note that you can install on macOS with homebrew:

    brew install rename
    

提交回复
热议问题