0

I found this post for extracting rar files from one folder to same folder name: extract rar files to same folder name

I want to extract several rar files in sub-folders to sub-folders of the same name i.e.

/folder/sub-folder1/file1.rar --> /folder/sub-folder1/new-folder-same-name-as-sub-folder1/extracted_files.xyz

/folder/sub-folder2/file2.rar --> /folder/sub-folder2/new-folder-same-name-as-sub-folder2/extracted_files.xyz

How do I modify:

#!/bin/bash

    for archive in "$(find $loc -name '*.rar')"; do
      destination="${archive%.rar}"
      if [ ! -d "$destination" ] ; then mkdir "$destination"; fi
      unrar e "$archive" "$destination"
    done

to do this i.e. run for all rar files in folder/*rar? Or, use something similar?

Thanks

ButchMonkey
  • 1,873
  • 18
  • 30
  • You can try `mkdir -p $destination` if i have understood the question well..If you want to have `sub-folder2/sub-folder2/` – Riz Jan 19 '20 at 18:30

1 Answers1

0

You can use next pipe from the $loc folder, which path do you expect in filename:

find . -iname *.sh -type f -printf '%P\n' | xargs -i bash -c 'echo "$1" `dirname $1`/`echo "$1" | sed "s/\.sh$//1" | tr "/" "-"`' -- {}

As you can see, $(dirname $archive) will return you a directory path, and pipe sed "s/\.sh$//1" | tr "/" "-" can return you a filename in expected format for you.

Maneevao
  • 331
  • 2
  • 6