The concept
So I´ve been struggling for a while now with a script that will run when rtorrent has finished a download. The script should check the third argument from rtorrent and act accordingly.
.rtorrent.rc:
system.method.set_key = event.download.finished,unrar_move_files,"execute={/home/holmen/script/testrt.sh,$d.get_base_path=,$d.get_name=,$d.get_custom1=}"
The script looks like this:
#!/bin/bash
# First, copy the downloaded material to the storage drive
# then unpack the files (if the unrar returned successful)
# lastly remove the rar files with rm -rfv command
hdfilm1=/media/store1/HD-film
hdfilm2=/media/store2/HD-film
download=/media/store3/Download
# Copy the downloaded material to correct storage drive
rsync -r --info=progress2 "$download"/"$2" "$3"
if [ "$3" = "$hdfilm1" ] || [ "$3" = "$hdfilm2" ]; then
# Check folders and subfolders of the downloaded material
while IFS= read -r dir; do
# Find and unpack archive files
if [ "$(find $dir | egrep -i '\.r00|\.001|part01\.rar|part001\.rar|subs\.rar')" ]; then
rarFile=`ls $dir | egrep -i '\.r00|\.001|part01\.rar|part001\.rar|subs\.rar'`;
searchPath="$dir/$rarFile"
yes no | nice -n 15 unrar x -o+ "$searchPath" "$dir"
remFile=`ls $dir | egrep -i '\.(rar|sfv|r([0-9]{2}))$'`;
remPath="$dir/$remFile"
rm -rfv $remPath
fi
done < <(find "$3"/"$2" -type d)
fi
This works basically but i have trouble when trying to delete the archive files (after they are unpacked). When adding a remove script in the while
loop the script just removes files in the specific subfolder its in at the moment.
The script unpacks:
/media/store1/HD-film/Movie.folder/*.rar
/media/store1/HD-film/Movie.folder/Subs/*.rar
but only removes the rar files in this folder
/media/store1/HD-film/Movie.folder/Subs/
How can I fix so my script removes the archive files also in the parent folder?
Edit: I have tried putting the rm
command after the unrar command with &&
but the result is the same.
if [ "$(find $dir | egrep -i '\.r00|\.001|part01\.rar|part001\.rar|subs\.rar')" ]; then
rarFile=`ls $dir | egrep -i '\.r00|\.001|part01\.rar|part001\.rar|subs\.rar'`;
searchPath="$dir/$rarFile"
remFile=`ls $dir | egrep -i '\.(rar|sfv|r([0-9]{2}))$'`;
remove="$dir/$remFile"
yes no | nice -n 15 unrar x -o+ "$searchPath" "$dir" && rm -rfv "$remove"
fi