1

I'm on Ubuntu server 18.04.

My main goal is to run a script from a parent directory which unrars all the files inside all sub directories of the parent directory. I have also installed apt install unrar and it is located at "/usr/bin/unrar". This is what I have come up with till now. But it does not seem to work:

for dir in 'pwd/*/'
do
  dir=${dir%*/} 
  cd dir
    for file in dir/*/
  do
    "/usr/bin/unrar" x dir/*.r* dir/
done

I've found a working script for Windows which uses 7zip here

  • The immediate problem is that the single quotes prevent the wildcard from matching any files. If `pwd` is not the literal name of the only directory wou want to traverse, you should probably replace it simply with `.` (the proper way to needlessly call the `pwd` command would be `"$(pwd)"`). – tripleee Jul 29 '20 at 17:53
  • Similarly, if you want to use the variable `dir` rather than try to access a directory named literally `dir`, the syntax for that is `"$dir"`. (The quotes are not syntactically required, but always a good idea when manipulating references to unknown file names; of course `/usr/bin/unrar` is a literal reference to a file with no funny surprises like `?` or `*` in its name, so quoting that is superfluous and slightly odd. See also [When to wrap quotes around a shell variable?](https://stackoverflow.com/questions/10067266/when-to-wrap-quotes-around-a-shell-variable)) – tripleee Jul 29 '20 at 17:53

3 Answers3

0

Here's a starting example; adjust pattern and echo statements as needed:

#!/bin/bash

pattern='*/*.rar'
archives=($pattern)

if [[ "${archives[@]}" == "$pattern" ]]; then
    echo NONE 1>&2
    exit
fi

for rpath in "${archives[@]}"; do
    dir=${rpath%/*}
    rar=${rpath##*/}
    pushd "$dir" > /dev/null
    echo -e "\ndir: $dir"
    echo unrar args "$rar"
    popd > /dev/null
done
Milag
  • 1,793
  • 2
  • 9
  • 8
  • Putting the pattern in a variable and then immediately using it just once to populate an array seems like a weird way to waste typing and a tiny amount of memory. – tripleee Jul 29 '20 at 17:57
  • The var pattern is present for OP ease-of-use and there are multiple uses, lines 4 and 6. – Milag Jul 31 '20 at 00:05
0

If you just want to unrar every rar file in the directory where it was found, find can do that directly. It takes some getting used to, but it's well worth learning.

find . -name '*.rar' -execdir unrar {} \;

Briefly, -execdir says to run the stuff up to \; on each found file in the directory where it was found; the {} placeholder gets replaced with the file name.

tripleee
  • 175,061
  • 34
  • 275
  • 318
0
find . -name '*.rar' -execdir unrar {} \;

This didn't work for me.

I changed it to this and it worked.

find . -name '*.rar' -execdir unrar e -r {} \;
Peter Csala
  • 17,736
  • 16
  • 35
  • 75
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 20 '21 at 02:33