Need a script to monitor a folder and copy it's contents


Recommended Posts

I have RUtorrent running which puts all of the completed torrents into the "Completed" folder. I would like to have something that can monitor this folder and when something is placed in it automatically copy it somewhere else.

 

I am not great with linux and was wondering if some of the more experienced linux users know if this can be done and how.

 

 

Thanks,

Scott

Link to comment

It would take a bit of time to get this to work.

You can also look at the mover script in /usr/local/sbin/mover

 

It's also possible with a shell script using find with -newer ${FILENAME} and rsync with the --remove-sent-files option.

After the rsync completes, touch the file named after the -newer option and that will be your marker for the next run.

 

mover might be an easier place to start.

 

 

 

 

Link to comment

thanks I will look at that. What I may do also is just put something in the cron.hourly folder that will run a cp or mv command each hour, although that may get files that have not finished copying into the source folder...may not be a big deal right away. Either way just thinking out loud  ;D

Link to comment

I have RUtorrent running which puts all of the completed torrents into the "Completed" folder.

I would like to have something that can monitor this folder and when something is placed in it automatically copy it somewhere else.

Why don't you simply tell rtorrent to put the completed stuff in that "somewhere else"?

 

If you really need to do some scripting, look into inotify.  Inotify can send a message when the dir tree is modified in some way.

The inotify-tools package provides a lightweight wrapper around the inotify api that is designed to be used in scripting.

 

The man page for inotify is here:

http://linux.die.net/man/7/inotify

 

The Slackware install package can be obtained from here:

ftp://slackware.osuosl.org/pub/slackware/slackware-current/slackware/a/

(The current version as of this writing is inotify-tools-3.14-i486-1.txz )

 

Google the web for plenty of examples of bash scripts using inotify.  One such example:

http://ubuntuforums.org/showthread.php?t=950939

 

Link to comment

Thanks, I will look into both of them. I wanted the torrents in 2 different directories that's is why I didn't just specify that directory in the program. Thanks for the help.

 

With the right rtorrent configuration, you can drop certain .torrent files in directory a and have them moved one place.  Drop other .torrent files in directory b and have them moved to another directory.

 

It gets a bit tricky, but it does work.

Link to comment

I puttered around a little bit this evening, and borrowing heavily from the mover script, I came up with this piece of code:

 

#!/bin/bash
# Author: DocBlock ([email protected])

# This is a rework of the unRAID mover script, designed to move completed files somewhere else
# It arose as a request of another user on the unRAID forums. http://lime-technology.com/forum/index.php?topic=6350.0
# I did basic testing on this script, but that's all. If it breaks your server, you can keep both pieces.

if [ -f /var/run/mymover.pid ]; then
 if ps h `cat /var/run/mymover.pid` | grep mymover.sh ; then
   echo "mymover already running, sorry."
   exit 0
 fi
fi

echo $$ >/var/run/mymover.pid

echo "Moving files from $1 to $2"
(cd "$1"; find -depth -print \( ! -exec fuser -s '{}' \; -exec rsync -i -qdIWRpEAXogt --numeric-ids --inplace --remove-source-files {} "$2" \; \))

# Prune empty directories from source dir
find "$1" -type d -empty -prune -exec rmdir --ignore-fail-on-non-empty {} \;

# Prune empty directories from destination dir
find "$2" -type d -empty -prune -exec rmdir --ignore-fail-on-non-empty {} \;

sync

rm /var/run/mymover.pid

echo "Moving all done."

 

I called it mymover.sh, and made it executable. Then, I created some test directories, that should match roughly what you're trying to do:

 

torrentdisk
`-- Completed Transfers
   |-- dir1
   |   `-- files1
   |-- dir2
   |   `-- files2
   `-- dir3
       `-- not_done_writing
disk1
`-- Completed Files

 

Notice that I have some random mount point called torrentdisk, and it has some stuff in it. The file not_done_writing is still being copied in.

I then run my script, as such:

 

./mymover.sh "/mnt/torrentdisk/Completed Transfers" "/mnt/disk1/Completed Files"

 

And another look at the filesystem shows us this:

 

torrentdisk
`-- Completed Transfers
   `-- dir3
       `-- not_done_writing
disk1
`-- Completed Files
   |-- dir1
   |   `-- files1
   `-- dir2
       `-- files2

 

The files that aren't completed are still on the torrent disk, and completed files have been migrated to disk1. I think this is what you asked for. Best of luck! ;)

 

NOTE: The locations I used in this example are purely fictional. I'm still new to unRAID and I didn't go poke around enough to figure out where disks really get mounted, etc. You need to ensure the proper paths are supplied to the mymover.sh script.

Link to comment
  • 12 years later...

This thread was extremely helpful, thanks! My use-case is slightly different wherein I'm looking to find just my audiobook files (m4a, mp3, etc.) from the downloads folder to a temp folder, where they'll be tagged separately with metadata. I wanted the downloads folder to be untouched since I'm seeding from there. I tried creating this script, but unfortunately, it isn't doing its job and I would love some help from someone who is more proficient in Linux commands.

 

#!/bin/sh
find /mnt/user/Downloads/torrents/completed/audiobooks/ -type f \( -iname \*.m4b -o -iname \*.mp3 -o -iname \*.mp4 -o -iname \*.m4a -o -iname \*.ogg \) -mmin -3 -exec cp -n "{}" /mnt/user/Media/Audiobooks/Temp/ \;

 

I added this as new under User Scripts and nothing happens when I run it. The log file doesn't generate any errors either.

 

What am I doing incorrectly?

Link to comment
On 7/22/2022 at 8:48 PM, vvolfpack said:

This thread was extremely helpful, thanks! My use-case is slightly different wherein I'm looking to find just my audiobook files (m4a, mp3, etc.) from the downloads folder to a temp folder, where they'll be tagged separately with metadata. I wanted the downloads folder to be untouched since I'm seeding from there. I tried creating this script, but unfortunately, it isn't doing its job and I would love some help from someone who is more proficient in Linux commands.

 

#!/bin/sh
find /mnt/user/Downloads/torrents/completed/audiobooks/ -type f \( -iname \*.m4b -o -iname \*.mp3 -o -iname \*.mp4 -o -iname \*.m4a -o -iname \*.ogg \) -mmin -3 -exec cp -n "{}" /mnt/user/Media/Audiobooks/Temp/ \;

 

I added this as new under User Scripts and nothing happens when I run it. The log file doesn't generate any errors either.

 

What am I doing incorrectly?

 

I personally use this in my User.Scripts

 

#Find Files from Incoming that refuse to move
find /mnt/cache/uploads/incoming/* -iname "*.mkv" -type f -mmin +10 -exec mv {} /mnt/cache/uploads/Tv-Incoming/ \;
find /mnt/cache/uploads/incoming/* -iname "*.mp4" -type f -mmin +10 -exec mv {} /mnt/cache/uploads/Tv-Incoming/ \;
find /mnt/cache/uploads/incoming/* -iname "*.avi" -type f -mmin +10 -exec mv {} /mnt/cache/uploads/Tv-Incoming/ \;

 

Every 15 Minutes I run a script that does a lot more, but I have this nested in the script that look for files that are at least 10minutes old and if they are .mkv, .mp4 or an .avi they are moved. Obviously you can change the time, but I figured 10minutes was plenty which allowed FileBot to uncompress the files and make sure they are not in the middle of being uncompressed and moved half way done. 

 

Find /mnt/cache/uploads/incoming/* -mmin +30 -type d -empty -delete

 

After any files are moved from a Directory after it was unpacked I use the above to check the folders and delete them when they are empty. I set it to longer to give files a chance to be moved a few times. 

 

 

  • Like 1
Link to comment
34 minutes ago, vvolfpack said:

You're a top person! I started using the script you provided (modified ever so slightly for my audio file use-case) and it's working like a charm. Super appreciated!

 

Your very welcome. I'm a true believer of writing out my code with Comments and doing little tweaks so I can come back later and see what I've broken or what works or don't work. I have a couple scripts with so many little routines because I figure I need 10 things to happen and I might as well type out a list and then address each on my list and pray it works. lol

 

Some of the things I do generate errors, but its a lot of the time "I couldn't find a file", but oh well next step and keep on moving. 

  • Like 1
Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.