Extract information from a file name for automatic copying

This is my first post on StackOverflow. Be gentle. :)

I have a linux server on which I download television shows and manually move the files to the appropriate folders for my Plex server. I would like to automate this process. I got as low as possible.

Show.Name.SeasonNumberEpisodeNumber.HDTV.X264.etc File Naming Convention ...

Example: almost. Person. S01E01.720p.HDTV.X264.mkv

NOTE. The display name may have a different length with. between each word in the name.

I can extract the show folder name from the file name.

#!/bin/bash
readonly FILEPATH=~/downloads
readonly SAVEPATH=~/shows

for file in $FILEPATH/*.mkv
do
#Get foldername from filename (everything before .S0 is foldername
        foldername=$(basename "${file%.S0*}" | tr '.' ' ')
#Need to convert extracted season info into folder name ex. S01 = Season 1
#       seasonfolder=$(basename "${file}" | sed -e 's/^[^S0]*//;')

#Copy the file to the path we built.
#Auto-create folder if it doesn't exist?

#       cp $file  "$SAVEPATH/$foldername/#seasonfolder"

done

Problems:

  • . SxxExx, . , , S01 ​​ 1, .

Resulting Copy ( )

cp Almost.Human.S01E01.720p.HDTV.X264.mkv shows/Almost Human/Season 1

sed regex,   , ,   "".

!

UPDATE

! , .

. Plex , " ", .

, CRON.

#!/bin/bash
readonly FILEPATH=~/downloads
readonly SAVEPATH=~/shows

for file in $FILEPATH/*.mkv
do
        dfile="$SAVEPATH/$(basename "$file" | sed -e 's/\./ /g' -e 's?\(.*\) [Ss]\([0-9][0-9]\)[Ee]\([0-9][0-9]\) .*?\1/Season \2/\1 - S\2E\3.mkv?')"

        if [ ! -f "$dfile" ]

        then
                cp -v "$file" "$dfile"
                mkdir -p "$(dirname "$dfile")"
        else
                echo "file exists "$dfile""
        fi

done
+4
1

- :

for file in $FILEPATH/*.mkv; do
    # get the destination filename
    dfile="$SAVEPATH/$(basename "$file" | sed -e 's/\./ /g' -e 's?\(.*\) S0\([0-9]\)E\([0-9][0-9]\) .*?\1/Season \2/Episode \3.mkv?')"

    # create the destination directory
    mkdir -p "$(dirname "$dfile")"

    cp "$file" "$dfile"
done

, :

Almost Human/Season 1/Episode 01.mkv

:

Almost Human/Season 1/Almost Human Episode 01.mkv

sed :

sed -e 's/\./ /g' -e 's?\(.*\) S0\([0-9]\)E\([0-9][0-9]\) .*?\1/Season \2/\1 Episode \3.mkv?

- \(...\), \1 \(...\), \2 ...

+2

All Articles