ファイル名に基づいてファイルをディレクトリに移動する

ファイル名に基づいてファイルをディレクトリに移動する

ファイル名に基づいてそのディレクトリにファイルを移動する方法がわかりません。デフォルトでは、tvshowname.season.episode.extまたはtv.show.name.season.episode.extで始まるテレビ番組がたくさんあります。 「表示名」、「表示名1」、「表示名2」などのディレクトリがあります。名前に基づいてファイルを同じ名前のディレクトリにコピーしたいと思います。

現在のファイル

Game of Thrones
Shooter
The Curse of Oak Island  
Van.Helsing.S01E08.Little.Things.720p.WEB-DL.DD5.1.H264-DRACULA.mkv
Van.Helsing.S01E08.Little.Things.720p.WEB-DL.DD5.1.H264-DRACULA.mp4
Real Vikings
Van Helsing

これまで私がしたこと

    #!/bin/bash
for FILE in "`ls *.{mp4,mkv}`"
do
        filename=$(basename "$FILE")
        extension=${filename##*.}
        filename=${filename%.*}
echo $filename
done

今私が経験している問題は、ファイル名自体から最初の数語または少なくともプログラム名を取得する方法です。次に、その名前に基づくディレクトリに移動します。それぞれは、The Flash、The Curse of Oak Islandなどにリストされています。

ベストアンサー1

たぶんこれが始めるのに役立ちます:

#!/bin/bash
for f in *.{mp4,mkv}           # no need to use ls.
do
    filename=${f##*/}          # Use the last part of a path.
    extension=${f##*.}         # Remove up to the last dot.
    filename=${filename%.*}    # Remove from the last dot.
    dir=${filename#tv}         # Remove "tv" in front of filename.
    dir=${dir%.*}              # Remove episode
    dir=${dir%.*}              # Remove season
    dir=${dir//.}              # Remove all dots.
    echo "$filename $dir"
    if [[ -d $dir ]]; then     # If the directory exists
        mv "$filename" "$dir"/ # Move file there.
    fi
done

おすすめ記事