Monitor a directory and move file types (inotifywait)...

Forum rules
Before you post please read this

Monitor a directory and move file types (inotifywait)...

Postby Fugazi on Thu Feb 07, 2013 2:44 pm

I will be running a Mint 11 machine (Dostoevsky) as a media player attached to the TV (this I can do, no problem so far).
Many of my legally owned files are on another Mint 11 (Trotsky) machine which also does my downloading of, oh let's say, linux OS torrents.
I have made Transmission on my Trotsky automatically load torrents from a directory (woo, see me go). I can move a .torrent files from downloads on Dostoevsky to the Transmission folder on Trotsky with great ease. I'd like Dostoevsky to monitor it's downloads folder and move any .torrent files to '/home/media/.gvfs/torrents on trotsky/'.

How do I make my Mint 11 box monitor a folder for a file type and move any file of that type to another folder?
Last edited by Fugazi on Thu Feb 07, 2013 3:56 pm, edited 1 time in total.
Fugazi
Level 1
Level 1
 
Posts: 46
Joined: Sun Oct 30, 2011 3:42 pm

Linux Mint is funded by ads and donations.
 

Re: Monitor a directory and move file types...

Postby xenopeek on Thu Feb 07, 2013 3:17 pm

Moved it here as this doesn't seem to be a question about Gnome Shell specifically (the forum section where you had posted this).

Do you want a program for it, or write a script? I can move your topic to another forum section again if you are looking for some program to do this for you. A script is probably easily written using inotifywait. That waits for some specific filesystem event (like a created file) on some specific folder before returning control to your script again. You can then process the file, and continue doing the wait for event & process in a loop.
Forum Rules | IRC Channel Rules
Image
Arch Linux / 64-bit / Gnome Shell
User avatar
xenopeek
Level 21
Level 21
 
Posts: 13653
Joined: Wed Jul 06, 2011 3:58 am
Location: The Netherlands

Re: Monitor a directory and move file types...

Postby Fugazi on Thu Feb 07, 2013 3:31 pm

I'm looking for any way to do it. If a script can run in the background (without a terminal window) it would be great.
How would inotifywait be used?
I want to monitor /downloads/ and send .torrent files to /torrents/
Thanks.
Fugazi
Level 1
Level 1
 
Posts: 46
Joined: Sun Oct 30, 2011 3:42 pm

Re: Monitor a directory and move file types...

Postby Fugazi on Thu Feb 07, 2013 3:56 pm

I can move any torrent with this script:
Code: Select all
#!/bin/bash
 
WORKINGDIR=/home/media/Downloads
 
cd $WORKINGDIR
 
for file in *.torrent; do
if [ $file == "*.torrent" ]; then
break
fi
mv "$file" "/home/media/.gvfs/torrents on trotsky/"
done;

which I have saved as torrent.sh and I run with the command:
Code: Select all
bash torrent.sh

It does the job (though I get the following in terminal: "mv: preserving permissions for `/home/media/.gvfs/torrents on trotsky/raspberrypiOS.torrent': Operation not supported). How do I use inotifywait or CRON to automate it, and will the error cause problems?
Fugazi
Level 1
Level 1
 
Posts: 46
Joined: Sun Oct 30, 2011 3:42 pm

Re: Monitor a directory and move file types...

Postby xenopeek on Thu Feb 07, 2013 4:09 pm

I have done something similar before. You'll need to install the package inotify-tools.

Might need a little tweaking to do exactly as you want probably, but here is a start. You would create a file somewhere in your home folder, put the following into it, mark the file executable, and in the Startups Applications program add the full path to the file to run it at login. It keeps running once started, and creates a log file in your home folder so you can check it is working (remove the echo command to disable logging).
Code: Select all
#!/bin/bash

# configure logfile
LOGFILE=~/.copied_torrents.log

# configure folder to watch for .torrent files being created
SRCDIR=/home/media/Downloads

# configure folder to move the .torrent files to
DSTDIR=/home/media/.gvfs/torrents\ on\ trotsky

# watch SRCDIR for closed files and move them to DSTDIR if they are .torrent files
do_watch () {
   while true; do
      NAME=$(inotifywait --quiet --event close --format '%f' "$SRCDIR")
      if [[ -f "$SRCDIR/$NAME" && "$NAME" =~ .*\.torrent ]]; then
         echo "$(date +'%b %d %X'): moving .torrent file  $SRCDIR/$NAME to $DSTDIR" >> "$LOGFILE"
         mv "$SRCDIR/$NAME" "$DSTDIR"
      fi
   done
}

# start watching
do_watch &

If you want to try it out, probably best put a line with "exit" on it after the line "mv $SRCDIR/$NAME $DSTDIR". That way you can start it manually and it will exit after the first file that matches. Else if you want to tweak and test the script some more, you'd have to find all the started processes and kill them.

Edit: some changes because you have folder names with spaces in them and I didn't account for that :roll:
Forum Rules | IRC Channel Rules
Image
Arch Linux / 64-bit / Gnome Shell
User avatar
xenopeek
Level 21
Level 21
 
Posts: 13653
Joined: Wed Jul 06, 2011 3:58 am
Location: The Netherlands

Re: Monitor a directory and move file types (inotifywait)...

Postby Fugazi on Thu Feb 07, 2013 4:28 pm

I shall try that tomorrow.
Folders with spaces is because Mint mounts shares un .gvfs under their description, not under their folder name! It's the fault of whoever wrote samba for nautilus!
Thanks.
Fugazi
Level 1
Level 1
 
Posts: 46
Joined: Sun Oct 30, 2011 3:42 pm

Re: Monitor a directory and move file types (inotifywait)...

Postby xenopeek on Thu Feb 07, 2013 4:42 pm

Oh, ah, Samba? Better test it then. I doubt inotifywait will work with remote filesystems. I thought you had the other machine "upload" the files to the one where you have the script running...
Forum Rules | IRC Channel Rules
Image
Arch Linux / 64-bit / Gnome Shell
User avatar
xenopeek
Level 21
Level 21
 
Posts: 13653
Joined: Wed Jul 06, 2011 3:58 am
Location: The Netherlands

Linux Mint is funded by ads and donations.
 

Return to Scripts & Bash

Who is online

Users browsing this forum: No registered users and 2 guests