Skip to content

Instantly share code, notes, and snippets.

@dofy
Last active April 13, 2019 01:44
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dofy/699db45f8d1a2ddd732b6f277f6f5364 to your computer and use it in GitHub Desktop.
Save dofy/699db45f8d1a2ddd732b6f277f6f5364 to your computer and use it in GitHub Desktop.
Monitor the movie download folder and organize it. https://youtu.be/L98iF7MHxqc
#!/bin/bash
# Monitor the movie download folder and organize it.
# Author: Seven Yu <dofyyu@gmail.com>
# Version: 1.0
if [[ -z $1 ]]; then
echo "⚠️ You must input a path"
exit 1
fi
if [[ "$1" == "." || "$1" == ".." ]]; then
echo "⚠️ Not support . and .."
exit 1
fi
if [ "$1" == "-h" ]; then
echo "Usage:"
echo "./watch.sh -h|path"
echo
echo -e "Options:"
echo -e "-h\tShow help"
echo -e "path\tWatch file changed under path"
exit 1
fi
# 监测路径
beginChar=`echo $1 | cut -c 1-1`
if [ "$beginChar" == "/" ]; then
watchPath=$1
else
watchPath=`echo $(pwd)/$1`
fi
# 检测 fswatch 命令是否可用
if type fswatch >/dev/null 2>&1; then
# 检查监测路径是否存在
if [[ -d "$watchPath" ]]; then
echo "👀 Watching on \"$watchPath\""
echo -e "-----------------------------\n"
# 监测循环
fswatch -0 "$watchPath" | while read -d "" file
do
# 判断文件是否存在
if [[ -f "$file" ]]; then
folderName=`dirname "$file"`
# 只监测目录第一级
if [ "$folderName" == "$watchPath" ]; then
fileName=`basename "$file"`
fName=$(echo "$fileName" | sed -e "s/\.[^.]*$//" -e "s/\.S[0-9]*E[0-9]*//")
season=$(echo "$fileName" | sed "s/.*\.\(S[0-9]\{1,\}\)E[0-9]\{1,\}\..*/\1/")
# 得不到「季」数据则默认保存到 S00
[ "$season" == "$fileName" ] && season=S00
folder=$watchPath/$fName/$season
# 创建文件夹并移动文件
mkdir -p "$folder" && mv "$file" "$folder"
# 输出执行结果
echo 🚗 [$(date +"%Y-%m-%d %T")]
echo -e "🍺 File \"$fileName\" has be moved to\n\t\"$folder\"\n"
fi
fi
done
# 监测循环
else
echo "⚠️ \"$watchPath\" is not exists!"
fi
else
echo "Please install fswatch first."
echo "https://github.com/emcrisostomo/fswatch#installation"
fi
@dofy
Copy link
Author

dofy commented Apr 11, 2019

Watching movie downlowd folder, and auto move the new files which named like name.S01E02.ext to name/S01 folder.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment