4

i am a total noob, but i figured out this script for doing the following:

I have a folder called "unrar" in there are subfolders with unknown foldername with rar file inside. Now i enter unknownsubfolder, find rar file and unrar it in unknownsubfolder. After that i find the new file and rename it with the unknownsubfoldername. Now i grab the file and move it to ./unrar.

#!/bin/bash

cd /home/user/unrar/

for dir in /home/user/unrar/*; 
    do (cd "$dir" && find -name "*.rar" -execdir unrar e -r '{}' \;); done

echo "$(tput setaf 2)-> unrar done!$(tput sgr0)" 

for dir in /home/user/unrar/*; 
do (cd "$dir" && find -name "*.mkv" -exec mv '{}' "${PWD##*\/}.mkv" \;);     done
for dir in /home/user/unrar/*; 
do (cd "$dir" && find -name "*.mp4" -exec mv '{}' "${PWD##*\/}.mp4" \;);     done
 for dir in /home/user/unrar/*; 
do (cd "$dir" && find -name "*.avi" -exec mv '{}' "${PWD##*\/}.avi" \;); done

cd /home/user/unrar
find -name "*.mkv" -exec mv '{}' /home/user/unrar \;
find -name "*.mp4" -exec mv '{}' /home/user/unrar \;
find -name "*.avi" -exec mv '{}' /home/user/unrar \;

This works fine with most files, but in some cases it doesn't

I want to find *.rar in DIR and unrar it. the newfile.(.mkv|.avi|.mp4) should be renamed to DIR(.mkv|.avi|.mp4) and moved to ./unrar

This is my filestructure.

./unrar/
    - unknownsubfolder/
        -file.rar
        -file.r00
        -....
    - unknownsubfolder1/
        - s01/
            - file.rar
            - file.r00
            - ....
        - s02/
            - file.rar
            - file.r00
            - ....
        - ....

If case1, unrar "/unknownsubfolder/file.rar" and get "x.mkv". the file is renamed from "x.mkv" to "unknwonsubfolder.mkv" and moved to "./unrar/unknownsubfolder.mkv" (same with *.avi + *.mp4) ==perfekt

if case2, in my script unknownsubfolder/s01/file.rar will be unrard, but not renamed to s01.mkv insted to unknwonsubfolder1.mkv. (if there are more like s02, s03, s04 ...) i always end up with one unknownsubfolder.mkv file in ./unrar) ==wrong output

So i guess i have 3 questions

How do i get the right DIRname for renaming the file? Or how do i enter unknownsubfolder/s01 ....?

Is there a way to exclude a word from the find? sometimes "unknownsubfolder" contains another folder+file called "sample(.mkv|.avi|.mp4)". I would like to exclude that, to prevent the original file to be overwritten with the sample file. happens sometimes.

I am sure i can combine some of the code,to make it even shorter. Could someone explain how? So how i combine the mkv,avi and mp4 in one line.

regards, wombat

(EDIT: for better understanding)

wombat
  • 83
  • 7
  • Do I understand correctly, in the problematic case you have 30 subfolders inside unknownsubfolder instead of one rar-file? As you have phrased it, one might think that it is just one folder named `subfolder1-30`. Do those subfolders all still contain just one single rar-file? –  Jul 03 '16 at 12:03
  • Right, unknownsubfolder contains up to 30 more subfolders each contains a compressed video in rar. – wombat Jul 03 '16 at 15:31

1 Answers1

2

UPDATE:

I adjusted the solution to work with unrar. Since I did not had unrar installed previously, I used gunzip to construct the solution and then simply replaced it with unrar. The problem with this approach was that, by default, unrar extracts to the current working directory. Another difference is that the name of the extracted file can be completely different from the archive's name - it is not just a matter of different extensions. The original archive is also not deleted after extraction.

Here is the solution specifically tailored to work with unrar with respect to aforementioned behavior:

#!/bin/bash

path="$1"
omit="$2"

while read f;do
        unrar e -r "${f}" "${f%/*}" > /dev/null
done < <(find "${path}" -type d -name "${omit}" -prune -o -type f -print)

while read f;do
        new="${f%/*}"
        new="${new##*/}"
        mv "${f}" "${path}/${new}"
done < <(find "${path}" -type d -name "${omit}" -prune -o -type f -a \! -name '*.rar' -print )

You can save the script, e.g., as rename-script (do not forget to make it executable), and then call it like

./rename-script /path/to/unrar omitfolder

Notice, that inside the script there is no cd. You will have to at least provide the location of the unrar folder as first parameter, otherwise you will get an error. In case of OP this would be /home/user/unrar. The omitfolder is not a path, it is just the name of the folder that you want to omit. So in OP's case this would be sample.

./rename-script /home/user/unrar sample

As requested by OP in the comments, you can read about the bash read-builtin and process substitution in order to understand how the while-loop works and how it assigns the filenames returned by find to the variable f.

  • thank you very much for your answer. right now i don't undertand a thing, but i will try to figure it out, what does what and come back with better feedback. – wombat Jul 03 '16 at 15:36
  • When i run that script i get syntax error at "done < < " unexpected word. i can almot figure out what every line does in that script, but not really what "read f" means, and then "${f}". I guess it's the file i wnat to unrar, but how does f know that it is the file to unrar. could you explain that? – wombat Jul 03 '16 at 19:38
  • @wombat I updated the solution to work with `unrar`. I used `gunzip` before and simply replaced it with `unrar`. Your error, however, results from not calling the script properly. You must at least provide the location of the unrar-folder as first parameter. If your unrar folder will always be at the same location and the omit folder will always have the same name then you could hardcode both into the script by altering `path="/home/user/unrar"` and `omit=sample`. Then you could just call it without parameters. –  Jul 04 '16 at 09:46
  • thanks for your time and work. I edited my startpost maybe it is easier to understand then. with your script i was asked lots of time if i want to overwrite file. seems like it only cut of the "rar" extension and moved file to ./unrar. There was no extraction of rar files. – wombat Jul 04 '16 at 18:53