0

For some reason transmission's watch-dir functionality doesn't work for me (I tried a few "solutions" I found but nothing worked). So I made myself a script to supply for that (note that I put this script in my crontab to run hourly, so I needed to add fullpaths for everything):

#!/bin/bash

prefix='/home/user'
folder=$prefix'/path/to/watched/dir'
cd $folder

count=$(ls -1 *.torrent 2>/dev/null | wc -l)
if [ $count != 0 ];then
    echo $count torrents files found
    for torrent in '*.torrent'; do
        echo adding $torrent
        transmission-remote -n 'transmission:transmission' -a $folder/"$torrent"
        rm $folder/$torrent
    done
else
    echo no torrents found
fi

What I got with this script is that it works if there is only 1 torrent file. But if there is 2 or more, then only 1 of them is added, all of them are removed and the line echo adding $torrent shows all torrents.

What am I doing wrong?

slm
  • 369,824

1 Answers1

3

I suspect that the line

for torrent in '*.torrent'; do

is not expanding to a list of files because you've enclosed it in quotes. It expands later each time $torrent is used, but this passes all of your files to transmission at once.

Rewrite this line as

for torrent in *.torrent; do

I also recommend using nullglob. See for loop glob mishaps.

Also, inotify-wait can be used to trigger a shell script when a file is created in a directory.

slm
  • 369,824
Nick ODell
  • 2,608
  • thanks, i removed quotes and now it works fine. Could you expand more in how to use inotify-wait?, a quick search and i couldn't see how to monitor only *.torrent creations, but i now that it should be with -e create option. – kurokirasama Jul 09 '18 at 02:19
  • See https://unix.stackexchange.com/questions/323901/how-to-use-inotifywait-to-watch-a-directory-for-creation-of-files-of-a-specific – Nick ODell Jul 09 '18 at 02:36