2

I'm trying to work on a script that will crawl my Plex media folder, find any header ".r00" files, extract them in their own directory, and trash the archive zips after it's done. I have two options I've been playing around with. Combined they do what I want, but I would like to have it all in one nice little script.

Option 1:

  • This script opens the "LinRAR" GUI, makes me navigate to a specific directory, finds and extracts any .r00 file in that directory, and successfully deleted all archive zips.

    while true; do
    if dir=$(zenity --title="LinRAR by dExIT" --file-selection --directory); then 
        if [[ ! -d $dir ]]; then
    echo "$dir: Wrong Directory" >&2
    else
    ( cd "$dir" && for f in *.r00; do [[ -f $f ]] || continue; rar e "$f" && rm "${f%00}"[0-9][0-9]; done )
    fi
    else
    echo "$bold Selection cancelled $bold_off" >&2
        exit 1
    fi
    zenity --title="What else...?" --question --text="More work to be done?" || break
    done
    

Option 2:

  • This script cd's to my Plex folder, recursively finds any .r00 files, extracts to my /home/user folder, and does not remove the archive zips.

    (cd '/home/user/Plex');
     while [ "`find . -type f -name '*.r00' | wc -l`" -gt 0 ]; 
          do find -type f -name "*.r00" -exec rar e -- '{}' \; -exec rm -- '{}' \;; 
    done
    

I would like to have something that takes the first working script, and applies the recursive find to all folders inside of /Plex instead of only letting me navigate to one folder at a time through the "LinRAR" GUI.

ztoliver
  • 41
  • 7
  • Not an answer to your question, but have you heard of rar2fs? http://hasse69.github.io/rar2fs/ This might do what you want without losing any time extracting. – Gfy Apr 20 '15 at 18:39
  • Funny to find my code from 2010 :) thx this helped! Upvote – dExIT May 20 '18 at 16:56

1 Answers1

0

No need to use cd. find takes a starting directory.
It's that dot (.) you passed to it.

Also added another (more sane) alternative for the find & loop:

#!/bin/bash

while true; do
if dir=$(zenity --title="LinRAR by dExIT" --file-selection --directory); then 
    if [[ ! -d $dir ]]; then
        echo "$dir: Wrong Directory" >&2
    else
        # Alternative 1 - a little more comfortable
        files="$(find "${dir}" -type f -name '*.r00')"
        for file in ${files}; do
            rar e "${file}" && rm "${file}"
        done

        # Alternative 2 - based on your original code
        while [ "`find "${dir}" -type f -name '*.r00' | wc -l`" -gt 0 ]; do
            find "${dir}" -type f -name "*.r00" -exec rar e -- '{}' \; -exec rm -- '{}' \;;
        done
    fi
else
    echo "$bold Selection cancelled $bold_off" >&2
    exit 1
fi
zenity --title="What else...?" --question --text="More work to be done?" || break
done

According to the comments, I ran a small example of this code and it works perfectly fine:

#!/bin/bash

if dir=$(zenity --title="LinRAR by dExIT" --file-selection --directory); then
    if [[ ! -d $dir ]]; then
        echo "$dir: Wrong directory" >&2
    else
        find $dir -type f
    fi
else
    echo "cancelled"
fi

A directory is successfully picked and all its files are printed. If I chose to cancel in zenity, then it prints 'cancelled'.

ArnonZ
  • 3,822
  • 4
  • 32
  • 42
  • Thanks for the edit. This doesn't do anything different though. I still have to navigate to my folders where I have .rar files in order to extract them. The point of my question is to automate the process where I can just run the script and it will find all zip files in my Plex folder, unzip them in the directory they reside, and delete the archive files once it does that. If I have 100 TV shows to unzip I don't want to have to manually navigate through GUI to each folder. – ztoliver Apr 23 '15 at 04:18
  • I thought you wanted your scripts merged. Anyhow, with the current implementation you can give the UI a root dir with many rar files inside and it will find them in the subdirs (meaning - give it your plex dir). You can spare the UI trick and just replace the zenity line with `dir=/home/user/Plex`. – ArnonZ Apr 26 '15 at 11:08
  • Close, but it's not finding the zips in home/user/Plex. I have to drill down all the way to the episode containing directory for it to find the rar. If I could get that working, then it would be perfect and one useful answer for you. – ztoliver Apr 28 '15 at 02:26
  • How could it not find it? If you're running `find "/home/user/Plex" -type f -name '*.r00'` the files are not found in the subdirs? – ArnonZ Apr 28 '15 at 09:47
  • Doesn't seem so. The following is super close to what I need. `code #!/bin/bash while true; do if dir=/home/user/Plex/TV_Shows; then if [[ ! -d $dir ]]; then echo "$dir: Wrong Directory" >&2 else ( cd "$dir" && for f in find *.r00; do find -type f -name "*.r00" || continue; rar e "$f" && rm "${f%00}"[0-9][0-9]; done ) fi else echo "$bold Selection cancelled $bold_off" >&2 exit 1 fi zenity --title="What else...?" --question --text="More work to be done?" || break done` – ztoliver May 05 '15 at 03:07
  • The output is : `code ./Name_Of_Show/Episode_Name.r00 (Actually finds all the .r00 files. I just have one in this case) RAR 4.20 Copyright (c) 1993-2012 Alexander Roshal 9 Jun 2012 Trial version Type RAR -? for help Cannot open find.rar No such file or directory No files to extract ` – ztoliver May 05 '15 at 03:13
  • With your alternative 1, I get "/home/user/Plex: Is a directory Selection Cancelled" – ztoliver May 05 '15 at 03:21
  • I'm not sure how are you getting that message when you're using the `--directory` option of zenity. tested a shortened version of your script and it works perfectly fine. added it to the answer for you to check. – ArnonZ May 05 '15 at 07:44
  • According to the error you have (cannot open find), you need to remove the word find from the for loop. for works like this: `for element in e1 e2 e3` - element will get e1 on first iteration, e2 on second, etc. you added an element called `find` to your for loop - remove it (the `find` after `do` is necessary). – ArnonZ May 05 '15 at 07:59