Skip to content

Instantly share code, notes, and snippets.

@Serneum
Created April 4, 2024 22:47
Show Gist options
  • Save Serneum/2f615f9490972677b36ad6c4ab8eee7f to your computer and use it in GitHub Desktop.
Save Serneum/2f615f9490972677b36ad6c4ab8eee7f to your computer and use it in GitHub Desktop.
Hardlink or clean up existing files when new files are detected
#!/bin/bash
inotifywait -m -r /data/media/Anime -e create -e moved_to -e delete --format "%w%f@%e" |
while read -r fullpathwithevent; do
ev="${fullpathwithevent##*@}"
echo "Event: $ev"
fullpath=$(echo $fullpathwithevent | sed 's/@.*//')
echo "Full path: $fullpath"
file="${fullpath##*/}"
echo "File name: $file"
directory=$(dirname "${fullpath}")
echo "Directory name: $directory"
if [[ "$ev" == "DELETE" ]]; then
escaped_filename=$(echo $file | sed 's/\([][]\)/\\\1/g')
echo "$file was deleted. Deleting /data/media/ShokoAnime/dest/**/$file"
echo "find \"/data/media/ShokoAnime/dest\" -type f -name \"$escaped_filename\" -delete"
find "/data/media/ShokoAnime/dest" -type f -name "$escaped_filename" -delete
else
echo "Found file '$file' in directory '$directory'"
if [[ -f "$fullpath" ]]; then
echo "Moving '$file' to Shoko import directory"
echo "ln $fullpath /data/media/ShokoAnime/source/$file"
ln "$fullpath" "/data/media/ShokoAnime/source/$file"
fi
fi
echo ""
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment