simple script to move files after processing

Come up with a useful post-processing script? Share it here!
CloudDweller
Newbie
Newbie
Posts: 19
Joined: October 27th, 2010, 12:31 pm

Re: simple script to move files after processing

Post by CloudDweller »

Thanks Mar2zz for the script.  I have to admit I'm a total noob and must be doing something wrong as I can't get it working.  Where exactly am I supposed to enter the location of the output folder?  The script log just says request unsuccessful.  I'm not at all familiar with scripting and really need hand holding.  Thanks for your patients.
User avatar
Mar2zz
Jr. Member
Jr. Member
Posts: 85
Joined: February 4th, 2011, 8:30 am
Contact:

Re: simple script to move files after processing

Post by Mar2zz »

Please post your log so I can see what to edit, you can find the log in history of sabnzbd, click the + sign and then more, copy that content and paste it here or on pastebin.com and link it here.

The outputpath is virtually created (or guessed) with this: sed "s#/Users/Chris/Downloads/#/Network Drive/#g")

/Users/Chris/Downloads/ is replaced by /Network Drive/, so you need to edit /Network Drive/ I suppose to your networkdrivename and rootfolder (rootfolder is the folder where your TV and Movies share are in) For example: \\Linkstation\
So try: sed "s#/Users/Chris/Downloads/#smb://Linkstation/#g") or see below. The best option is to mount your linkstation locally.

This virtual replacing mirrors the exact situation of folders and files inside /Users/Chris/Downloads/ to \\Linkstation\. This way it will see for itself if the prefix is Movies or TV (or other categories for that matter, if you create special folders for it on your desktop it will mirror them on your NAS.)

Ah wait, those networkadresses are irritating, they need escaping as \ is an escape itself. Did you mount it? else you need to escape it and change all / to \.
Use the mounted networkdir by preference, else use two pipes, replace the newpath by this

Code: Select all

newpath=$(echo $folders | sed "s#/Users/Chris/Downloads/#//Linkstation/#g" | sed "s#/#\\#g")
Last edited by Mar2zz on February 10th, 2011, 2:34 pm, edited 1 time in total.
CloudDweller
Newbie
Newbie
Posts: 19
Joined: October 27th, 2010, 12:31 pm

Re: simple script to move files after processing

Post by CloudDweller »

Here is my log file without anything changed from the script you gave me.

http://pastebin.com/raw.php?i=Y4LhM5te

Hopefully you can see what the problem is as your last post about "escapes" and "pipes" totally lost me  :-[  Also, I might be misunderstanding what your script does, but I've worked out that my network drive address is "/Volumes/TV or /Volumes/Movies" so could I not enter these locations instead of virtually creating/guessing it.  Like I said, I may be totally misunderstand what you're doing.
Last edited by CloudDweller on February 10th, 2011, 2:56 pm, edited 1 time in total.
User avatar
Mar2zz
Jr. Member
Jr. Member
Posts: 85
Joined: February 4th, 2011, 8:30 am
Contact:

Re: simple script to move files after processing

Post by Mar2zz »

I am sorry, I will not be to technical. Yes you need pathguessing, why? Otherwise the script can't see which path a tvshow has to go in. We need the last part of your folder behind /Users/Chris/downloads/.  This part we need: TV/The Big Bang Theory/Season 3/ is the part we need to store your shows in the right location. Replacing with sed does that for you.

Now, we need to replace /Users/Chris/downloads/ with /Volumes/. You want /Users/Chris/downloads/TV/The Big Bang Theory/Season 3/The Big Bang Theory - S03E14.avi to go to /Volumes/TV/The Big Bang Theory/Season 3/The Big Bang Theory - S03E14.avi right?

I don't know for a Mac, but Linux is very casesensitive when it comes to file and foldernames.

Your downloadfolder isn't Downloads but downloads. In bash always watch for these little mistakes.
Here is the script you need:

Code: Select all

#!/bin/bash

# VARIABLES 
#---- EDIT HERE IF THINGS CHANGE, E.G. OTHER NETWORKLOCATION----

local="/Users/Chris/downloads/";
network="/Volumes/";

#---- DO NOT EDIT BELOW -----

for folders in $1 #### for all folders inside the downloadpath do the following
do
newpath=$(echo $folders | sed "s#$local#$network#g") #### create new path by replacing local dirs with networkdir
mkdir -p "$newpath" || #### make the newdir if it doesn't exist
mv -f "$folders/*.*" "$newpath"
#rm -Rf $1 #### uncomment to delete sourcefolder.
done

note: you can do testruns by executing the script like this ./scriptname "/path/to/process" e.g. /.movefiles "/Users/Chris/downloads/TV/The Big Bang Theory/Season 3/". The part behind the scriptname is $1. So you don't need to download everytime to test ;)
Last edited by Mar2zz on February 10th, 2011, 4:23 pm, edited 1 time in total.
CloudDweller
Newbie
Newbie
Posts: 19
Joined: October 27th, 2010, 12:31 pm

Re: simple script to move files after processing

Post by CloudDweller »

Thanks for the new script.  I ran it but it still didn't move any files  :(  I then removed the forward slashes at the end of the local and network location and this is what I am now getting.

Download Folder:
Users/Chris/downloads/TV/Legend of the Seeker/Season 2/video file.mkv

Network Drive:
Volumes/TV/Legend/

So its starting to create a folder structure on my network drive but the show name folder's name isn't complete and there's nothing inside it.

P.S. I'm not actually downloading a TV show, just a sync fix but this shouldn't cause a problem should it?

Here is a copy of my latest log if this helps http://pastebin.com/raw.php?i=ZR2LVU3K
User avatar
Mar2zz
Jr. Member
Jr. Member
Posts: 85
Joined: February 4th, 2011, 8:30 am
Contact:

Re: simple script to move files after processing

Post by Mar2zz »

Your pastebin link doesn't work
Then it works, but is missing quotationmarks somewhere. I always forget those... spaces in locations break up scripts if it is not properly quoted...

Here, I quoted everything to be sure.

Code: Select all

#!/bin/bash

# VARIABLES 
#---- EDIT HERE IF THINGS CHANGE, E.G. OTHER NETWORKLOCATION----

local="/Users/Chris/downloads/";
network="/Volumes/";

#---- DO NOT EDIT BELOW -----

for folders in "$1" #### for all folders inside the downloadpath do the following
do
newpath=$(echo "$folders" | sed "s#$local#$network#g") #### create new path by replacing local dirs with networkdir
mkdir -p "$newpath" || #### make the newdir if it doesn't exist
mv -f "$folders/*.*" "$newpath"
#rm -Rf "$1" #### uncomment to delete sourcefolder.
done
CloudDweller
Newbie
Newbie
Posts: 19
Joined: October 27th, 2010, 12:31 pm

Re: simple script to move files after processing

Post by CloudDweller »

Okay I think we are nearly there. The directory structure is being created but the final file (avi, mkv, iso etc...) isn't being copied/moved.  I'm at work at the moment so will send you my log file when I'm home.  Thanks again for all your help.
User avatar
Mar2zz
Jr. Member
Jr. Member
Posts: 85
Joined: February 4th, 2011, 8:30 am
Contact:

Re: simple script to move files after processing

Post by Mar2zz »

Ok, I learned some shortcomings in the mv-command. It can't move directory's to directory's that allready exist. That's kinda stupid in my opinion. So I did some research and we need the copycommand. After the copy te source can be deleted, and so it is the same as the move-command but with more options.

Also || works a little different then I thought it would. I still need to learn a lot about bash, I write bashscripts for a month now, I can be considered a noob too, so this one was a nice study...  ;) (Let's hope it's fixed now or I'll have to study some more). First test it, and if it works remove # from this one: #rm -Rf "$folders", that one will throw away the source after it's copyied to your networklocation.

I edit the code below the variables:

Code: Select all

for folders in "$1" #### for all folders inside the downloadpath do the following
do
newpath=$(echo "$folders" | sed "s#$local#$network#g") #### create new path by replacing local dirs with networkdir
mkdir -p "$newpath" #### make the newdir if it doesn't exist
cp -TRf "$folders" "$newpath"  #### copy all files and folders inside the downloadpath 
#rm -Rf "$folders" #### delete sourcefolder
done
CloudDweller
Newbie
Newbie
Posts: 19
Joined: October 27th, 2010, 12:31 pm

Re: simple script to move files after processing

Post by CloudDweller »

Sorry I meant to post a couple of days ago but I haven't been able to test the last script until today. It works great. Thank you so much for all your help with this. I really do appreciate it ;D
Adamward
Newbie
Newbie
Posts: 1
Joined: February 28th, 2011, 4:48 am
Location: Cheshire

Re: simple script to move files after processing

Post by Adamward »

I was looking for this coding , I am  going to give it a try now.
CloudDweller
Newbie
Newbie
Posts: 19
Joined: October 27th, 2010, 12:31 pm

Re: simple script to move files after processing

Post by CloudDweller »

Is there a way to confirm that the files have been moved prior to deletion?  At the moment, if the ‘mv’ command tries to move the files and fails it will still run the ‘rm’ command and delete the files.  I’d prefer it to check first and if the move has failed then it doesn’t remove the files, if it has worked then it does.

Is this possible?
User avatar
Mar2zz
Jr. Member
Jr. Member
Posts: 85
Joined: February 4th, 2011, 8:30 am
Contact:

Re: simple script to move files after processing

Post by Mar2zz »

Just add && behind the cp command, this means after command is succesfull go to the next command, else it stops.
cp -TRf "$folders" "$newpath" &&
rm -Rf "$folders"
mfacer
Newbie
Newbie
Posts: 4
Joined: September 9th, 2010, 4:02 pm

Re: simple script to move files after processing

Post by mfacer »

racso wrote: I currently use this script on a OSX Mac.

Code: Select all

#!/bin/bash

if [ "$5" = "movies" ]; then
  mv -fv "$1" "/Volumes/External HD/Media/Movies"
elif [ "$5" = "tv" ]; then
  mv -fv "$1" "/Volumes/External HD/Media/TV"
fi
Note that you can change the "/Volume/../.././" bit to whatever you like. Even (mounted) network drives!

If you want to see your mounted disks go to the "Finder" menu -> "Go" -> "Go to folder". Then type "/Volumes/" (without the quotes) and voila!  
Hey - I'm now running sabnzbd on my mac mini... I tried the above script (working on my Amahi box) but I cannot for the life of me get it to work! I get the message "Exit(-1) Cannot run script /Users/matt/downloads/sort2.sh". I've CHMOD to 777 and tried moving it around. I have tried cutting down the script to just work on the one folder and all sorts. What were the settings for Mac OSX you used? I'm running this on an old mac mini (Tiger 10.4)

Thanks
User avatar
Mar2zz
Jr. Member
Jr. Member
Posts: 85
Joined: February 4th, 2011, 8:30 am
Contact:

Re: simple script to move files after processing

Post by Mar2zz »

That's probably a problem with line endings (invisible stuff). See http://en.wikipedia.org/wiki/Line_endings

Convert it to LF for Mac I think.
Crush
Newbie
Newbie
Posts: 15
Joined: May 8th, 2010, 1:46 am

Re: simple script to move files after processing

Post by Crush »

I am looking for a script similar to this. What I want is for the contents of the final folder (that is, after unpacking) to be moved to my desktop. I have tried this:

Code: Select all

mv -fv "$1/*" ${HOME}/Desktop/
but I get an error saying
mv: rename /Users/[REDACTED]/Desktop/Test/* to Desktop/*: No such file or directory
Post Reply