Linux Bash Post Script (Clean, Move, Rename, chown)

Come up with a useful post-processing script? Share it here!
Post Reply
User avatar
duckvader
Newbie
Newbie
Posts: 6
Joined: June 9th, 2012, 3:20 pm

Linux Bash Post Script (Clean, Move, Rename, chown)

Post by duckvader »

Ok I have been working on putting together a script that will clean up the trash files and move the folder. Once the files have been moved it then renames and change the owner and group. I have got the rename and changing the owner and group working its the clean up and move function that is not working. I need some more eyes on this to help me figure out why the SABnzbd variables are not being passed to the script. Below is the code for the script. I have commented out the rename and change owner because the are working. Any help would be great.

Code: Select all

#!/bin/bash
# Clean Trash, Move, Change Owner , and Rename for HD Movies in SABnabd

### Clean Up HD Movie Directory ####
trashRemoval () {
				trash=".nfo .srr .sfv .nzb" #### Add or Remove Extensions Here ####
				for junk in $trash
				do
				find $1 -name *$junk -type f -exec rm -f {} \;
				done
				}

#### Move HD Movies to /media-movies/movies/HD ####
moveHD () {
		  mv -fv $1 /media-movies/movies/HD ;
          }
				
#### Rename HD Movies in /media-movies/movies/HD ####
renameHD () {
			path_to_movies=/media-movies/movies/HD/

			find $path_to_movies -maxdepth 1 -type d > /media-movies/scripts/movies_all.txt
			cat /media-movies/scripts/movies_all.txt | grep -E "(19|20)[0-9]{2,2}" > /media-movies/scripts/movies_with_year.txt
			cat /media-movies/scripts/movies_all.txt | grep -vE "(19|20)[0-9]{2,2}" > /media-movies/scripts/movies_without_year.txt

			counter=0
			move=1
			echo=1

			while read movie_path; do
			movie_name=`echo "$movie_path"|perl -wple 's|'$path_to_movies'||;s|/||'`
			movie_year=`echo "$movie_name"|perl -wple 's/.*((19|20)[0-9]{2,2}).*$/$1/'`
			nice_movie_name=`echo "$movie_name"|perl -wple 's/((((19|20)[0-9]{2,2}).*$)|\(|\))*//ig;s/(imax.?edition|imax|extended.?unrated.?se|unrated|remastered|directors.?cut)*//ig;s/\./ /g;s/[ ]{2,}/ /ig;s/ $//g;s/S W A T/SWAT/ig;s/L A/LA/ig'`

			temp=""
			for word in $nice_movie_name; do
			if ! echo $word|grep -qiE "^(vs|and|the|at|of|to|a|for|in)$"; then
			if [ -n "$temp" ]; then
				temp="$temp ${word^}"
			else
				temp="${word}"
			fi
			else
			if [ -n "$temp" ]; then
				temp="$temp ${word,,}"
			else
				temp="${word}"
			fi
			fi
			done

			nice_movie_name="$temp"
			
			if echo "$nice_movie_name"|grep -qi "^the "; then
				nice_movie_name=`echo "$nice_movie_name"|perl -wple 's/^the //i'`
			fi

			dest_path="$path_to_movies/$nice_movie_name ($movie_year)"

			if [ $echo -eq 1 ]; then
				echo "$movie_path -> $dest_path"
			fi

			if [ $move -eq 1 ]; then
				mv -vi "$movie_path" "$dest_path"
			fi

			let "counter += 1"

			done < /media-movies/scripts/movies_with_year.txt

			while read movie_path; do
				movie_name=`echo "$movie_path"|perl -wple 's|'$path_to_movies'||;s|/||'`
				nice_movie_name=`echo "$movie_name"|perl -wple 's/(720p|1080p|720i|1080i).*$//ig;s/(proper|x264|h264|bluray|hddvd|repack|\.DL\.|hdtv|\.OAR\.| OAR |imax.?edition|imax|extended.?unrated.?se|limited|extended|swedish|subbed|unrated|remastered|directors.?cut)*//ig;s/\./ /g;s/[ ]{2,}/ /ig;s/ $//g'`

				temp=""
			for word in $nice_movie_name; do
			if ! echo $word|grep -qiE "^(vs|and|the|at|of|to|a|for|in)$"; then
			if [ -n "$temp" ]; then
				temp="$temp ${word^}"
			else
				temp="${word}"
			fi
			else
			if [ -n "$temp" ]; then
				temp="$temp ${word,,}"
			else
				temp="${word}"
			fi
			fi
			done

			nice_movie_name="$temp"

			if echo "$nice_movie_name"|grep -qi "^the "; then
				nice_movie_name=`echo "$nice_movie_name"|perl -wple 's/^the //i'`
			fi

			dest_path="$path_to_movies/$nice_movie_name"

			if [ $echo -eq 1 ]; then
                echo "$movie_path -> $dest_path"
			fi

			if [ $move -eq 1 ]; then
                mv -vi "$movie_path" "$dest_path"
			fi	

			let "counter += 1"

			done < /media-movies/scripts/movies_without_year.txt

			echo "Number of folders that were moved: $counter"
}


#### HD Movies Change Owner ####
changeOwnerHD () {
			# Define the Username, Group and Permissions to Apply Here #
			user="sabnzbd"
			group="users"
			folders="775"
			files="644"

			# Directories Being Changed #
			moviesHD="/media-movies/movies/HD"
	
			# HD Movies CHOWN Script #
			chown -R $user:$group "$moviesHD"
			find "$moviesHD" -type d | while read i; do chmod $folders "$i"; done
			######find "$moviesHD" -type f | while read i; do chmod $files "$i"; done

			echo "Owner and Permissions for HD Movies Have Been Changed Successfully."
			}

#### Execute All Above Function ####
 
trashRemoval
moveHD
#renameHD
#changeOwnerHD
User avatar
shypike
Administrator
Administrator
Posts: 19774
Joined: January 18th, 2008, 12:49 pm

Re: Linux Bash Post Script (Clean, Move, Rename, chown)

Post by shypike »

Arguments are passed to the script, but I see you don't use quotes.
You should always assume that there are spaces in names.
So always use things like: "$1" and "/my_folder/$2".
BTW: SABnzbd has it's own extension-based cleanup, specified in Config->General.
Post Reply