Linux – How to extract multiple 7zip archives to folders with the same names in Linux

7-zipextractlinux

I have about 300 7zip archives, each with several files in them, and no directory structure inside.

I need to extract all these archives to folders that match the respective archive.

Example:

foo.7z
bar.7z

Extract contents of foo.7z to /foo/
Extract contents of bar.7z to /bar/


I tried this script:

find . -name "*.7z" -type f| xargs -I {} 7z x -o{} {}

But it resulted in:

can not open output file ./foo.7z/file.bin
Skipping    file.bin

What am I doing wrong? I'm using Ubuntu Linux.

Best Answer

You're trying to create a directory with the same name as the file. That cannot work: There can't be two directory entries with the same name.

One way to fix this is to use basename to strip the .7z extension from the files and create the directories without the .7z suffix.

Like:

for archive in *.7z; do 7z x -o"`basename \"$archive\" .7z`" "$archive"; done
Related Question