Automatically extract rar files downloaded with transmission

My new project recently has been to configure sonarr to work with transmission. The challenge was getting these two pieces of software to properly interface with each other. Sonarr would successfully instruct transmission to download the requested show but once the download completed it would not import the show to its library. The reason behind this was my torrent tracker – most torrents are downloaded as multi part rar files. Sonarr has no mechanism for processing rar files so I had to get creative.

The solution was to write a simple script and have transmission execute it after finishing the download. The script uses the find command to look for rar files in the directory transmission created for that particular torrent. If any rar files are found it will extract them into that same directory. This was important because sonarr will only look in the torrent download directory for the completed video file.

After some tweaking I got it to work pretty well for me. Here is the code I used (thanks to this site for the direction.)

#!/bin/bash
#A simple script to extract a rar file inside a directory downloaded by Transmission.
#It uses environment variables passed by the transmission client to find and extract any rar files from a downloaded torrent into the folder they were found in.
find /$TR_TORRENT_DIR/$TR_TORRENT_NAME -name "*.rar" -execdir unrar e -o- "{}" \;

Save the above script into a file your transmission client can read and make it executable. Lastly configure transmission to run this script on torrent completion by modifying your settings.json file (mine was located at /var/lib/transmission/.config/transmission-daemon/settings.json) Modify the following variables (be sure to stop your transmission client first before making any changes.)

"script-torrent-done-enabled": true, 
"script-torrent-done-filename": "/path/to/where/you/saved/the/script",

That’s it! Sonarr will now properly import shows that were downloaded via multipart rar torrent.

25 thoughts on “Automatically extract rar files downloaded with transmission”

  1. “Save the above script into a file your transmission client can read and make it executable.”

    Where is the optimal path to save the script. Besides naming it unrar.sh, how do I make it executable? Newbie question. Sorry

    1. You make it executable by running the following command:

      “chmod +x

      As for where to put it, you can place it anywhere your torrent client can read it. Your home directory is fine.

  2. Not sure if you’re monitoring comments to this post anymore, but: for the life of me, I cannot get this to work.

    1. My Mac installation of Transmission (GUI, 2.92) does not seem to produce a json file, anywhere. There is the plist, which shows the correct values you note above, though.

    2. No matter WHAT, Transmission never calls urar.sh. I’ve run chmod +x on the script (which lives in my home directory), and Transmission has been instructed (in Preferences) to call that script upon torrent completion.

    Nothing works. Do you have any idea as to a) why that might be the case, or b) what I might do to try and fix it?

    1. Unfortunately I don’t have any experience with the mac client of Transmission. The only way I’ve used it is within a Linux environment. That being said the biggest gotcha with Transmission is you have to stop the service before you make any changes to its configuration, then restart the service once you’ve finished making changes. If you try to edit any configuration while it’s running, the configuration will get overwritten when transmission restarts.

    2. OS X uses .plist file for Transmission. You can write configs to that file with:

      $ defaults write org.m0k.transmission script-torrent-done-enabled 1
      $ defaults write org.m0k.transmission script-torrent-done-filename /usr/local/bin/unrar.sh”;

      And read with
      $ defaults read org.m0k.transmission | grep script-torrent

  3. Bravo, Sir!!! Bravo! This was exactly what I need to get my setup running completely unattended. You are my hero.

  4. Nice script. I tried using in on Docker in my NAS, but the argument -execdir does not seem to be supported. Can I use the same command but use -exec instead? Or do I need to change something else as well?

    1. If you don’t have execdir you can tweak the script slightly to tell the unrar command to extract into that directory. Replace that find command with this one:

      find $TR_TORRENT_DIR/$TR_TORRENT_NAME -name “*.rar” -exec unrar e “{}” “$TR_TORRENT_DIR/$TR_TORRENT_NAME/” \;

      Hope this helps.

      1. Thanks for your time helping out. But I’m afraid it didn’t work. The problem is I cannot see whats wrong, is there like a logging fuction for this? I’m running Transmission in Docker on a Synology and I begin to suspect that some commands are not available. But I don’t know for sure. Any suggestions?

        1. I would run the script manually within your docker. Run “docker exec -it name_of_container /bin/bash” and then try to set the variables transmission passes to the script manually, then running the script.

        2. Hi Cimarron,
          I’m also on a Synology NAS, and the find util did not support execdir. I used the script below to replicate execdir with the exec one:

          [code]
          #!/bin/bash
          #Extract compressed RAR files

          PATH_TO_DIR=${TR_TORRENT_DIR}/${TR_TORRENT_NAME}

          echo ‘Unraring files in: ‘${PATH_TO_DIR}

          find “${PATH_TO_DIR}” -name ‘*.rar’ -exec sh -c ‘unrar e -o- “{}” $(dirname “{}”)’ \;
          [/code]

          Hope that helps!

  5. i changed the scrip a so it would extract the content inside the torrent folder not the root of where the script is

    find /$TR_TORRENT_DIR/$TR_TORRENT_NAME -name ‘*.rar’ -execdir unrar e {} \;

    ideally i want to check if the rar files have been extracted before and skip to next one

  6. The script is amazing; I can run it perfectly as a standalone script but for some reason it doesn’t seem to run when the torrent completes still. I’ve put the file in the same folder as transmission downloads to (/media/), it’s executable and should in theory work without any issues… but it just doesn’t seem to run.

    I’m using iocage jails and have set the settings.json up with the right location.

    Anyone able to help work out what the issue is?! Thanks!

    1. I am having the same issue as well. I think it has something to do with the permissions on the folder that is saved. I may move it to a root directory and try later.

  7. What if they are zip files? Also, how about putting them into a folder within the folder called Extracted or something similar? I don’t like having all the files extracted into a seeding folder, I like to keep it clean

  8. You should add to this post that on mac os there is a setting:
    Settings -> Transfers -> Management -> Call script when download completes.

  9. Add ‘”‘ to TR_TORRENT_NAME when downloading files with a long name

    find $TR_TORRENT_DIR/”$TR_TORRENT_NAME” -name “*.rar” -execdir unrar e -o- “{}” \;

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.