Skip to content

Instantly share code, notes, and snippets.

@paul-chambers
Last active April 1, 2025 12:55
Show Gist options
  • Save paul-chambers/71ef48e40449ec73eef95430b9e4e6c7 to your computer and use it in GitHub Desktop.
Save paul-chambers/71ef48e40449ec73eef95430b9e4e6c7 to your computer and use it in GitHub Desktop.
Helper script for deluge or qBittorrent. It hardlinks completed torrents into a destination folder. It also unrars it, if it includes a rar archive.
#!/bin/bash
# Helper script for the Deluge torrent client
# Intended to be run at torrent completion, using the 'torrent complete' event in the 'Execute' plugin.
#
# The basic idea is to hardlink the files that deluge has just finished downloading to a second directory.
# This allows you to configure deluge to automatically pause or delete torrents when they reach a given seed ratio,
# while also keeping a copy around ofr other reasons. Personally I use SyncThing to propagate new downloads to
# a remote machine, where they are processed and moved out of the Syncthing folder, which in turn propagates their
# deletion back to the original machine running deluge.
#
# The upshot is that both deluge's seeding process and the 'forward to somewhere else' process (via Syncthing) can
# execute in parallel, not needing to be aware of what the other is doing, yet the net result is that downloads are
# cleaned up automagically when both have finished their respective tasks.
#
# Paul Chambers, Copyright (c) 2019.
#
# Made available under the Creative Commons 'BY' license
# https://creativecommons.org/licenses/by/4.0/
#
torrentId=$1
torrentName=$2
torrentPath=$3
# echo "$torrentId $torrentName $torrentPath" >> /tmp/torrent-complete.log
srcDir="${torrentPath}/${torrentName}"
destDir="/downloads/completed/${torrentName}"
rarFile=`find "${srcDir}" -name "*.rar"`
if [ -z "${rarFile}" ]
then
cp -arl "${srcDir}" "${destDir}"
else
mkdir -p "${destDir}" \
&& cd "${destDir}" \
&& unrar e -inul -o+ "${rarFile}" \
&& find "${srcDir}" -type f ! \( -regex '.*\.r[0-9][0-9]' -o -name '*.rar' \) -exec ln "{}" "${destDir}/" ';'
fi
@thedirektor
Copy link

I was tired of the plugin solutions and was hoping to find an alternative. This is it. Unrar/Copy/Clean This works fantastic!
Thank you.

@paul-chambers
Copy link
Author

Glad you found it useful :)

@Theangelofspace15
Copy link

Theangelofspace15 commented Oct 6, 2019

How can all this b integrated on sonarr radarr? I used to use copy completed and unrar plugin to do this but copy completed not working now. Also how can it workd with labels so it copy to /LABEL/*

@paul-chambers
Copy link
Author

paul-chambers commented Oct 7, 2019 via email

@Theangelofspace15
Copy link

Thats no what I mean. Deluge use label and copy movies tvshows to different folder base on label when they r done. I want to know how to move the different files base on label with your script

@paul-chambers
Copy link
Author

It already supports labels. I use labels myself. See lines 35 and 40.

@Theangelofspace15
Copy link

I see, but the label in the script is a variable that you created it is not? label="${torrentPath#$srcDir}"
Or would that get the label assigned on the torrent using the label or label plus plugin?

@Koyakami
Copy link

Koyakami commented Jan 9, 2020

Hi mate, complete beginner - Deluge -> Plugins -> Execute. Select Add, Event=Torrent Completed and what do I put in the command?

Do I need to transfer this .sh file over to my Deluge FTP and then type the file name into that?

OR do I copy and paste everything in the .sh file?

Cheers Paul

@TinTin192
Copy link

Haven't been able to get this running for some reason. Perms are correct etc etc.. But just isn't executing...

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