402

How can I uncompress a *.7z file in Ubuntu and keep the directory structure?

Flyk
  • 1,480
michael
  • 6,059

9 Answers9

533

First install the p7zip-full package:

sudo apt-get install p7zip-full

With this, Nautilus should have an option to uncompress 7-Zip Files.

I also recommend p7zip-rar so it also includes support for RAR files.

This is assuming you want to do it via GUI with Nautilus. If not, after downloading the packages above do the following:

7z x PACKAGE.7z 

that should eXtract the packages with full path.

anonymous2
  • 4,298
Luis Alvarado
  • 211,503
  • 1
    I try to install 7zip-full using apt. I get this message: "Package p7zip-full is not available, but is referred to by another package. This may mean that the package is missing, has been obsoleted, or is only available from another source." – posfan12 Oct 27 '18 at 00:12
  • Note that if the compressed file is VERY large, it will be broken up into separate component files file.7z.001, file.7z.002, etc. However, only the first file, file.7z.001, will show as an decompressable icon on the Nautilus desktop. The others appear as data file icons. Double-click this .7z.001 file to decompress ALL components in the package. Also, apparently all of the .00x component files created during the compression have to be present, or else probably the .001 decompression will fail. – DragonLord Jun 21 '19 at 19:43
  • 4
    Pay attention: if you run 7z x my-archive.7z all files will be extracted to your home! I had a huge archive now it will be a pain in the axe finding all files and moving them in the right place! – Sampgun Aug 11 '20 at 13:27
  • 6
    Hi @Sampgun pretty sure you were on your home folder when you ran that. Additionally there are 2 parameters, the "e" and "x", the main difference and why I recommend the "x" is because it preserves the folder structure inside the .7z file. So if you ended up with a huge amount of files in your home is because, you were in the home folder and the .7z file did not have a folder structure to begin with. Next time what you can do is create a folder, move the .7z over there and extract it inside of it to be safe. Good luck. – Luis Alvarado Aug 13 '20 at 15:18
  • 1
    @LuisAlvarado yeah, I was in the home, now I see. I used the x option and the tree was respected, but the zip contained lots of files in its root. I did add the folder, in fact this was the command 7z x /var/www/html/archive.7z. The html folder was empty, but still it extracted everything to the root, so...bummers! I think there sould be some option as -d exists for unzip – Sampgun Aug 14 '20 at 16:12
  • 1
    @Sampgun I feel you buddy. Fully understand. – Luis Alvarado Aug 14 '20 at 22:50
  • @posfan12 try sudo apt-get update before installing. – dim_voly Aug 27 '23 at 08:48
88

There is also dtrx - which is immensely useful for un-archiving anything.

it stands for "Do The Right eXtraction" - and will uncompress anything without any fuss.

simply:

sudo apt-get install dtrx
dtrx archive.tar.XX

Manpage: http://manpages.ubuntu.com/manpages/trusty/en/man1/dtrx.1.html


UPDATE for Ubuntu 20.04 :

According to their pypi page, dtrx is not currently available in the official repos. Thus, if you cannot install this via apt, then you can still use pip3:

pip3 install dtrx

If you don't already have pip3 installed, you can install it with

sudo apt install python3-pip
rm-vanda
  • 3,176
19

Run following command in the terminal:

sudo apt-get install p7zip-full p7zip-rar

Or search through the ubuntu software center as 7zip and unrar and install the packages.

d a i s y
  • 5,511
user89707
  • 335
15

For ubuntu 17.04, no need for the full package, p7zip is enough:

sudo apt install p7zip

Then, uncompress using the -d command:

p7zip -d something.7z
Derlin
  • 293
  • 8
    p7zip -d --keep something.7z or it will destroy your input file !!!! – Dave Kimble Aug 04 '18 at 07:16
  • 1
    This does not extract all files, due to features missing. in my case password protection. Thus this might be misleading. – scones Sep 25 '18 at 10:53
  • Ah, so the default behavior is to delete the input. That is indeed very useful and polite. I wish all programs did this. – Przemek D Apr 28 '20 at 13:04
10

First install the according package sudo apt install p7zip-full

  • use x flag to extract files with full path
  • use -o flag to set output directory

7z x <archive_name> -o{Directory}

for example

7z x file.7z -o/home/michael/Documents/NewFolder

Notice that there is no space between -o and the output directory. If the file was encrypted, it will automatically ask for the password.

Anno
  • 526
4

The other answers did not work for me.

But this command worked fine:

7z e file.7z
  • 2
    You are right but, you need to use "x" to keep directory structures! Note that if you use the tag "e" f.eg 7z e Myfile.7z it will pull everything into your current working directory and overwritten of the existing files can be a permanent loss. – MathArt Nov 26 '20 at 08:36
2

here is unarchiver https://theunarchiver.com/command-line

sudo apt install unar

then

unar xxx.7z

that's it.

0

To extract all 7z archives into their own folder, using the archive name as the folder name:

7z x "*.7z" -o*

E.g., if one has the two files a.7z and b.7z, then folders a and b will be created and they'll contain the content of a.7z and b.7z (keeping the directory structure), respectively.

0

In 2024 I think the easiest option is to use the Linux version of 7-zip.

This provides an official tool which does not require any installation. The executable 7zzs can be used out of the box and is portable. It also seems to have the same syntax as p7zip (at least for simple stuff). E.g. 7zzs x archive.7z to extract. (Add it to your $PATH in order for 7zzs to work or use the full path e.g. /directory/containing/the/executable/7zzs)


Also possibly of interest: Difference between several command-line tools provided for 7-Zip compression (like `7z`, `7zz`...)

Kvothe
  • 635