Page 1 of 1

[Linux] Rename downloads as NZB

Posted: September 10th, 2010, 8:12 am
by Orbi
Hello,

I just wrote this simple script to cover my simple needs. What I would like is that the files I download are renamed as the NZB I download. Sabnzbd already puts these downloaded files in a directory named after the NZB.

Currently the situation is like this:

Movies/NZBnameinqueu/a.mkv
                                    b.nfo
                                    c.srt

I would like it to become this:
Movies/NZBnameinqueu/NZBnameinqueu.mkv
                                    NZBnameinqueu.nfo
                                    NZBnameinqueu.srt

Please bear with me as this is the first code I ever wrote (probably cannot get any simpler :P). Can the experts tell me if it will do what I would like it to do?

Code: Select all

#!/bin/bash
fullpath=$1
filename=$3
cd $fullpath
mv $* filename
Thanks!

Re: [Linux] Rename downloads as NZB

Posted: September 10th, 2010, 9:20 am
by sweetie
That won't.

Code: Select all

#!/bin/bash

for files in *; do
  [ ! -f "$3.${files##*.}" ] && mv -v "$files" "$3.${files##*.}"
done
That will.
Of course if you have multiple files with same extension only the first will be renamed.

Re: [Linux] Rename downloads as NZB

Posted: September 10th, 2010, 1:46 pm
by Orbi
sweetie wrote: That won't.

Code: Select all

#!/bin/bash

for files in *; do
  [ ! -f "$3.${files##*.}" ] && mv -v "$files" "$3.${files##*.}"
done
That will.
Of course if you have multiple files with same extension only the first will be renamed.
Thanks for trying to help me out. Your script worked in an unexpected way. Instead of renaming the downloaded file, it renamed my entire SABnzbd program directory, making the application crash instantly. I was worried 2 seconds hoping my entire file server would not have been renamed. Luckily only did the Sabnzbd directory.

What happened?

Re: [Linux] Rename downloads as NZB

Posted: September 10th, 2010, 3:04 pm
by sweetie
Oh sorry. You'll need to change directory to download dir. (You had that part right in your psuedo-script.)

Code: Select all

#!/bin/bash

cd "$1"

for files in *; do
  [ ! -f "$3.${files##*.}" ] && mv -v "$files" "$3.${files##*.}"
done


Note: Strongly recommend always running scripts in a test folder before a critical/live environment, though in this case it was my fault, sorry!

Re: [Linux] Rename downloads as NZB

Posted: September 13th, 2010, 1:40 am
by Orbi
That does exactly what I wanted! Now I have Sabnzbd fully configured, everything is automated. I love it.

And thanks for complimenting my pseudo-scripting abilities  ;D