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

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

    you can try this.

    #!/usr/local/bin/bash
    for f in *.mp3
    do
    artist=`echo $f | awk '{print $1}' FS=-`
    song=`echo $f | awk '{print $2}' FS=-`
    mkdir -p $artist
    mv $artist-$song $song
    mv $song ./$artist
    done
    

    here I am using two variable artist and song. as your test file name is "hi\ -\ hey" so I change the awk delimiter to "-" to store variable according to it.

    we don't need to use awk..by using bash parameter expansion.... it is working.

    #!/usr/local/bin/bash
    for f in *.mp3
    do
    artist=`echo ${f%-*}`
    song=`echo ${f#*-}`
    mkdir -p $artist
    mv $artist-$song $song
    mv $song ./$artist
    done
    

提交回复
热议问题