0

folks, need a hand with unpacking a big batch of rars. Don't want to do this one-by-one. I have several directories inside subdirectories of 'dir' with many .rar's in each.

dir 
    subdir_a
        filename with spaces 01.rar
        filename with spaces 02.rar
        filename with spaces NN.rar
    subdir_b
        filename with spaces 01.rar
        filename with spaces 02.rar
        filename with spaces NN.rar
    subdir_c
        filename with spaces 01.rar
        filename with spaces 02.rar
        filename with spaces NN.rar

I woud like to extract the rars into the same locaitons where they currently sit in direcories labebed with the .rar's filename. So, after unraring, subdir_a would look like this:

dir 
    subdir_a
        filename with spaces 01
            file 1.ext
            file N.ext
        filename with spaces 01.rar
        filename with spaces 02.rar
        filename with spaces 02
            file 1.ext
            file N.ext
        filename with spaces NN.rar
        filename with spaces NN
            file 1.ext
            file N.ext

In dir I tried % find . -name '*.rar' -exec sh -c 'echo unrar e \"$1\" \"${1%.*}\" ' _ {} \;, with the echo for testing. It prints the results I'd expect:

unrar e "./subdir_a/filename with spaces 01.rar" ".subdir_a/filename with spaces 01/"

And so on. However, when I remove the "echo", I get ERROR: Unknown option:

Any idea why and/or if there's a better way for me to approach?

0

2 Answers 2

2

Don't escape the double quotes inside the single-quoted argument to sh -c - that causes sh to interpret them literally rather than as shell special characters, so that instead of protecting the filename with spaces 01.rar they actually cause it to be split so that unrar receives arguments e, "filename, with, spaces, and 01.rar".

To illustrate:

$ sh -c 'printf "%s\n" unrar e \"$1\" \"${1%.*}\" ' _ 'filename with spaces 01.rar'
unrar
e
"filename
with
spaces
01.rar"
"filename
with
spaces
01"

whereas you want

$ sh -c 'printf "%s\n" unrar e "$1" "${1%.*}" ' _ 'filename with spaces 01.rar'
unrar
e
filename with spaces 01.rar
filename with spaces 01
1

steeldriver has the correct answer to the specific issue you're facing; however note that in this case you could get away with not using find at all and with using just globbing; in Bash matching anything nested deeper than the current working directory using globbing patterns requires enabling the globstar option, so that would be:

shopt -s globstar; for f in dir/**/*.rar; do unrar e "${f}" "${f%.rar}"; done

However, judging from your prompt, it seems as though you're using Zsh, which has the functionality provided by globstar enabled by default, so in that case you could just run:

for f in dir/**/*.rar; do unrar e "${f}" "${f%.rar}"; done
> tree -a
.
├── dir
│   ├── subdir_a
│   │   ├── filename with spaces 01.rar
│   │   ├── filename with spaces 02.rar
│   │   ├── filename with spaces 03.rar
│   │   ├── filename with spaces 04.rar
│   │   └── filename with spaces 05.rar
│   ├── subdir_b
│   │   ├── filename with spaces 01.rar
│   │   ├── filename with spaces 02.rar
│   │   ├── filename with spaces 03.rar
│   │   ├── filename with spaces 04.rar
│   │   └── filename with spaces 05.rar
│   └── subdir_c
│       ├── filename with spaces 01.rar
│       ├── filename with spaces 02.rar
│       ├── filename with spaces 03.rar
│       ├── filename with spaces 04.rar
│       └── filename with spaces 05.rar
└── foo

6 directories, 15 files
> for f in dir/**/*.rar; do echo unrar e "${f}" "${f%.rar}"; done
unrar e dir/subdir_a/filename with spaces 01.rar dir/subdir_a/filename with spaces 01
unrar e dir/subdir_a/filename with spaces 02.rar dir/subdir_a/filename with spaces 02
unrar e dir/subdir_a/filename with spaces 03.rar dir/subdir_a/filename with spaces 03
unrar e dir/subdir_a/filename with spaces 04.rar dir/subdir_a/filename with spaces 04
unrar e dir/subdir_a/filename with spaces 05.rar dir/subdir_a/filename with spaces 05
unrar e dir/subdir_b/filename with spaces 01.rar dir/subdir_b/filename with spaces 01
unrar e dir/subdir_b/filename with spaces 02.rar dir/subdir_b/filename with spaces 02
unrar e dir/subdir_b/filename with spaces 03.rar dir/subdir_b/filename with spaces 03
unrar e dir/subdir_b/filename with spaces 04.rar dir/subdir_b/filename with spaces 04
unrar e dir/subdir_b/filename with spaces 05.rar dir/subdir_b/filename with spaces 05
unrar e dir/subdir_c/filename with spaces 01.rar dir/subdir_c/filename with spaces 01
unrar e dir/subdir_c/filename with spaces 02.rar dir/subdir_c/filename with spaces 02
unrar e dir/subdir_c/filename with spaces 03.rar dir/subdir_c/filename with spaces 03
unrar e dir/subdir_c/filename with spaces 04.rar dir/subdir_c/filename with spaces 04
unrar e dir/subdir_c/filename with spaces 05.rar dir/subdir_c/filename with spaces 05
2
  • Beautiful and good eye to see the zsh prompt. Thanks for the help. I'll read up on globbing, that's rad. Commented Jul 12, 2024 at 14:22
  • @JehanAlvani You're welcome! Commented Jul 12, 2024 at 18:08

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.