Search code examples
linuxbashsortingmove

bash - moving files based on information extracted from filename


Trying to create a script that will take a tv show video file and move it into the correct folder in my TV directory hierarchy.

ie. example filenames:

archer.2009.s01e01.publichd.mkv
archer.s05e10.dimension.mkv

I would like these moved to: Television/Archer/Season 1/ and Television/Archer/Season 5/ respectively and create them if they don't already exist.

Here's what I have so far, right now it's only sorting based on season for one particular show, Archer. Planning to expand that once I get it working:

#!/bin/bash

for season in 01 02 03 04 05 #06 07 08 09 10 11 12 13 14 15 16
do

    echo 'Season ' $season
    #find -iname *archer*s$season\*.*
    var=$(find -iname *archer*s$season\*.*)
    var=$(echo $var | cut -c 3-)
    echo $var

    var2=$(sed "s/0//" <<< $season)
    var3=$(echo "Season $var2/")
    echo $var3

    if [[ -z "$var" ]]
    then
        : 
    else
        mkdir -p /home/adam/Downloads/Television/Archer/$var3;
        mv "./$var" "/home/adam/Downloads/Television/Archer/$var3"
    fi

done

I'm having problems creating/moving the files to the $var3 directory variable. It does have a space in the name which I know is giving me the problems. I already have a large library of shows in this format though so I'm rather not change it.

Any help would be appreciated - I know the script is VERY primitive, I'm just piecing it together as I go.


Solution

  • You need to put quotes around var3 when using it.

    mkdir "$var3"