I have a lot of rar files
- Folder/
--- Spain.rar
--- Germany.rar
--- Italy.rar
All the files contains no root folder so it's just files.
What I want to achieve when extracting is this structure:
- Folder/
-- Spain/
---- Spain_file1.txt
---- Spain_file2.txt
-- Germany/
---- Germany_file1.txt
---- Germany_file2.txt
-- Italy/
---- Italy_file1.txt
---- Italy_file2.txt
So that a folder with the name of the archive is created and the archive is extracted to it.
I found this bash example in another thread but it's not working for me, it's trying to create one folder with all the files as name.
#!/bin/bash
for archive in "$(find . -name '*.rar')"; do
destination="${archive%.rar}"
if [ ! -d "$destination" ] ; then mkdir "$destination"; fi
unrar e "$archive" "$destination"
done
Any ideas how I can do this?
Best Answer
I have a script in my personal archive that does exactly this. More precisely, it extracts e.g.
Spain.rar
to a new directory calledSpain
, except that if all the files inSpain.rar
are already under the same top-level directory, then this top-level directory is kept.Usage (after installing this script in your
$PATH
under the nameextract
and making it executable):