Register for the iXsystems Community to get an ad-free experience and exclusive discounts in our eBay Store.

How to move a downloaded file to a different folder automatically

mbrunelli87

Newbie
Joined
Sep 17, 2016
Messages
65
Hi guys,

I would like to move automatically move completed downloads into different folders. I know that there are plugins like Couchpotato, SickBeard, etc..that can automate the process, but these plugins work if you use Transmission
The thing is that for downloading files I use both aMule and Transmission, and I would like to set-up a process to move files with specific extension from their respective "finished" folders to other destinations (e.g. Plex folder, document folders, etc.)

Is there an easy way to do that?
 

toadman

FreeNAS Guru
Joined
Jun 4, 2013
Messages
616
Yes. Write a script to test for existing files and move them. Call it via a periodic cron job. I googled "script to automatically move files" and it's clear a ton of people have solved this problem on basically every platform out there. Pick an sh or bash script, modify it appropriately and you should be good.
 

mbrunelli87

Newbie
Joined
Sep 17, 2016
Messages
65
Yes. Write a script to test for existing files and move them. Call it via a periodic cron job. I googled "script to automatically move files" and it's clear a ton of people have solved this problem on basically every platform out there. Pick an sh or bash script, modify it appropriately and you should be good.
Hi toadman,
Thank you, I gave a look around and actually it's plenty of people asking the same thing as me. Anyway, what you're saying (developing a periodic cron job) goes a little beyond my skills with Freenas. I was looking for something more similar to a plugin or something doable with a sort of interface... If there's nothing like that I will try your way
 

SweetAndLow

Sweet'NASty
Joined
Nov 6, 2013
Messages
5,995
No plugins unless you want to use the ones you already listed.

Sent from my Nexus 5X using Tapatalk
 

toadman

FreeNAS Guru
Joined
Jun 4, 2013
Messages
616
You can look up in the freenas docs how to schedule a cron job. You could use something as simple as this for your script:

Code:
#!/bin/bash
FILES=/path/to/source/*
for f in $FILES
do
	while lsof -Fp $f  &>/dev/null
	do
		sleep 1
	done
	mv $f /path/to/destination
done


You would make a file called "movefiles" (or whatever you want) and make it executable. You then call that file every X minutes with cron using it's absolute path. This script should wait if a file is being written to (copied into the source directory) before it tries to move that file.
 

mbrunelli87

Newbie
Joined
Sep 17, 2016
Messages
65
You can look up in the freenas docs how to schedule a cron job. You could use something as simple as this for your script:

Code:
#!/bin/bash
FILES=/path/to/source/*
for f in $FILES
do
	while lsof -Fp $f  &>/dev/null
	do
		sleep 1
	done
	mv $f /path/to/destination
done


You would make a file called "movefiles" (or whatever you want) and make it executable. You then call that file every X minutes with cron using it's absolute path. This script should wait if a file is being written to (copied into the source directory) before it tries to move that file.
Hi toadman,
thanks again for following up

I took a look to the documentation for the Cron Job and in general to the scripts that people post online to perform similar jobs but I have not had the chance to try on my server yer

So if I get things right, I should perform the following steps:
  1. Go to the Cron Job UI in Freenas
  2. Copy-paste the script you posted (changing the "/path/to/source/*" part and the "/path/to/destination" part
Given my extreme inexperience I don't get the part in which you say
You would make a file called "movefiles" (or whatever you want) and make it executable. You then call that file every X minutes with cron using it's absolute path. This script should wait if a file is being written to (copied into the source directory) before it tries to move that file.
Also, I would like to select which kind of files to move (es. only the ".mkv" extension), how can I do that?
 

toadman

FreeNAS Guru
Joined
Jun 4, 2013
Messages
616
I would follow these steps:

(1) Create a text file that contains the script.

Put the script (which is a program) in a new text file, which for example purposes I called "movefiles". Ideally do this by logging into the server command line as root.

So let's say your pool is called "tank." Create a directory /mnt/tank/scripts (or a directory in whatever place you want). In that directory you create and edit the text file called "movefiles" and put the script in it.

Regarding the script, yes, adjust the paths to whatever fits your needs. If you only want to move .mkv files then the line:
Code:
FILES=/path/to/source/*

becomes
Code:
FILES=/path/to/source/*.mkv

(2) Configure the CRON job

Yes, log into the FreeNAS GUI. Create a new cron job. The user is "root" and the Command is the path to the script file. So using the example above "/mnt/tank/scripts/movefiles".

Depending on what other users are accessing these .mkv files/directories the cron user can be set to whatever you want. (Example, on my system, maybe I want the "plex" user to do the moving of files because that user owns the media directories on my system, so I would set the cron user to "plex" vs "root". Though the way I setup the permissions (both are members of the group that owns the directories) either would work in my case.) Just make sure the permissions on the directories allow that user to read/write.​
 

mbrunelli87

Newbie
Joined
Sep 17, 2016
Messages
65
I would follow these steps:

(1) Create a text file that contains the script.

Put the script (which is a program) in a new text file, which for example purposes I called "movefiles". Ideally do this by logging into the server command line as root.

So let's say your pool is called "tank." Create a directory /mnt/tank/scripts (or a directory in whatever place you want). In that directory you create and edit the text file called "movefiles" and put the script in it.

Regarding the script, yes, adjust the paths to whatever fits your needs. If you only want to move .mkv files then the line:
Code:
FILES=/path/to/source/*

becomes
Code:
FILES=/path/to/source/*.mkv

(2) Configure the CRON job

Yes, log into the FreeNAS GUI. Create a new cron job. The user is "root" and the Command is the path to the script file. So using the example above "/mnt/tank/scripts/movefiles".

Depending on what other users are accessing these .mkv files/directories the cron user can be set to whatever you want. (Example, on my system, maybe I want the "plex" user to do the moving of files because that user owns the media directories on my system, so I would set the cron user to "plex" vs "root". Though the way I setup the permissions (both are members of the group that owns the directories) either would work in my case.) Just make sure the permissions on the directories allow that user to read/write.​
Thanks! That looks clear, I'll try tomorrow.

The last thing is, what if I want to move more file extensions? Should I write like the code as shown below?
Code:
]FILES=/path/to/source/*.mkv; .avi
 

toadman

FreeNAS Guru
Joined
Jun 4, 2013
Messages
616
Then change it to this:

Code:
#!/bin/bash

SOURCE=/path/to/source
DESTINATION=/path/to/destination

FILES=`find "$SOURCE" -type f | grep '.mkv\|.mp4'`

for f in $FILES
do
  while lsof -Fp $f  &>/dev/null
  do
	sleep 1
  done
  mv $f $DESTINATION
done


Note, the "`" in the line where FILES is set, before find, and at the end, are a "back tick". If you want more extensions just add "\|.ext" in there. (This is getting beyond "freenas" help and into basic scripting. So if you have trouble, please read up on the web to finish it off. I don't mind helping, but it is a freenas forum. The scripting questions are better suited for a bash scripting forum somewhere.)
 

mbrunelli87

Newbie
Joined
Sep 17, 2016
Messages
65
Hi,
I tried but....it does not work :(

Code:
#!/bin/bash

SOURCE=/v1/media/aMule-disk/Incoming
DESTINATION=/v1/media/Film

FILES=`find "$SOURCE" -type f | grep '.mkv\|.mp4\.avi|\.srt'`

for f in $FILES
do
  while lsof -Fp $f  &>/dev/null
  do
   sleep 1
  done
  mv $f $DESTINATION
done
 

Attachments

toadman

FreeNAS Guru
Joined
Jun 4, 2013
Messages
616
It's hard to debug "it doesn't work." What part doesn't work? It doesn't run? It doesn't find the files? It doesn't move the files? Etc...

If you have verified there are files of the given types in the source directory, what happens if you are logged in as root and run it from the command line? (Trying to isolate the issue to the script, or to the cron part.) You can temporarily replace the, "mv $f $DESTINATION" with "echo $f" if you want to see if it's finding the source files in the first place.

The script works on my machine when I tested it, so I know it's not completely broken. :)
 

mbrunelli87

Newbie
Joined
Sep 17, 2016
Messages
65
It's hard to debug "it doesn't work." What part doesn't work? It doesn't run? It doesn't find the files? It doesn't move the files? Etc...

If you have verified there are files of the given types in the source directory, what happens if you are logged in as root and run it from the command line? (Trying to isolate the issue to the script, or to the cron part.) You can temporarily replace the, "mv $f $DESTINATION" with "echo $f" if you want to see if it's finding the source files in the first place.

The script works on my machine when I tested it, so I know it's not completely broken. :)
Hi toadman, thanks for following up again. Actually the issue is that it does not move the files (yes, there are files with the given extension in the folder). I tried a couple of time to run the Cron Job and the files are not moving.
I will try again the next days or run the script from the command line and get back to you
 
Top