我目前按唱片标签组织音乐,每个标签的文件夹中都存在一个播放列表文件(由bash脚本生成),并且与父文件夹的名称相同。如果我添加或修改任何文件或文件夹,则需要删除该播放列表并运行脚本以获取已发生的任何更改。
我的目录树如下所示,播放列表与其父文件夹具有相同的名称,并包含有关发行版的信息。
Labels/
├── Label A
│ ├── Label A.playlist
│ ├── Release A
│ ├── Release B
│ └── Release C
├── Label B
│ ├── Label B.playlist
│ ├── Release A
│ ├── Release B
│ └── Release C
└── Label C
├── Label C.playlist
├── Release A
├── Release B
└── Release C
以防万一我在导入或修改音乐时错过了播放列表,我想知道是否有什么可以告诉我某些内容已更改并自动删除该播放列表。经过大量搜索之后,inotifywait似乎很合适。我花了大部分时间来解决这个问题,终于到了某个地方,但是有一个陷阱。
如果我将新文件/文件夹添加到标签下的任何文件夹,则在执行rm命令时,我需要$ fullpath的目录名来删除现有的播放列表。而如果我修改任何文件(例如ID3标记),则需要$ fullpath目录名的目录名。虽然代码可以实现我想要的功能,但我希望它更时尚。
inotifywait -m -r --format "%e %w%f" /media/C_Drive/Link_to_Music/Sorted1 -e modify -e create -e move -e delete |
while read event fullpath; do
#Remove playlist when changes to folder are reported...
rm "$(dirname "$fullpath")"/"$(basename "$(dirname "$fullpath")")".playlist
#Remove playlist when changes to file are reported...
rm "$(dirname "$(dirname "$fullpath")")/$(basename "$(dirname "$(dirname "$fullpath")")").playlist"
done
更新的脚本.....
#!/bin/sh
inotifywait -m -r --format "%e %w%f" /media/C_Drive/Link_to_Music/Sorted1 -e modify -e create -e move -e delete |
while read -r event fullpath; do
folderdir=${fullpath%/*}
filedir=${folderdir%/*}
modified="MODIFY"
filedelete="DELETE"
filemoved_to="MOVED_TO"
filemoved_from="MOVED_FROM"
folmoved_to="MOVED_TO,ISDIR"
folmoved_from="MOVED_FROM,ISDIR"
foldelete="DELETE,ISDIR"
if [ "$event" = $modified ] || [ "$event" = $filedelete ] || [ "$event" = $filemoved_to ] || [ "$event" = $filemoved_from ] ;then
echo "Because the file ${fullpath##*/} was $event, ${filedir##*/}.playlist has been deleted from the folder $filedir"
# Remove playlist when changes to file...
rm "$filedir"/"${filedir##*/}".playlist
elif [ "$event" = $folmoved_to ] || [ "$event" = $folmoved_from ] || [ "$event" = $foldelete ];then
echo "Because the folder ${fullpath##*/} was $event, ${folderdir##*/}.playlist has been deleted from the folder $folderdir"
#Remove playlist when changes to folder...
rm "$folderdir"/"${folderdir##*/}".playlist
fi
done