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

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Locked
Fugazi

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

Post by Fugazi »

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 LockBot on Wed Dec 28, 2022 7:16 am, edited 2 times in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
User avatar
xenopeek
Level 25
Level 25
Posts: 29597
Joined: Wed Jul 06, 2011 3:58 am

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

Post by xenopeek »

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.
Image
Fugazi

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

Post by Fugazi »

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

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

Post by Fugazi »

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?
User avatar
xenopeek
Level 25
Level 25
Posts: 29597
Joined: Wed Jul 06, 2011 3:58 am

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

Post by xenopeek »

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:
Image
Fugazi

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

Post by Fugazi »

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.
User avatar
xenopeek
Level 25
Level 25
Posts: 29597
Joined: Wed Jul 06, 2011 3:58 am

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

Post by xenopeek »

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...
Image
Locked

Return to “Scripts & Bash”