Extract 7z using p7zip in Ubuntu

7z is an archive file format, this article focuses on archive files which end with .7z extension. We would discuss how to compress and extract 7z using p7zip in Ubuntu distribution.

There could be numerous reasons which would make us use Archive files. An archive file may contain files having similar/different file formats.

Lets say we want to store/mail ‘n’ files of different file formats. Then, it would easier for us to handle just one file rather than ‘n’ different files. That’s where idea of archive files kick-in. We can compress those ‘n’ files in one & act accordingly. Furthermore, it also takes less storage space. In addition to, we can also choose various compression formats of our choice.

Note: Following operations would require you to have superuser privileges. In case you don’t have one, then contact your System Administrator for assistance.

We would first discuss installation steps required for p7zip in Ubuntu.

Install p7zip in Ubuntu

Since, p7zip package is already available in standard Ubuntu repository. Hence, issue the following in terminal –

sudo apt update
sudo apt install p7zip-full
Compress and Extract 7z using p7zip

We could use p7zip command-line utility to compress a file, for instance –

1.txt

To compress it, issue the following in terminal –

p7zip 1.txt

It would give us the outcome – 1.txt.7z

Now, to decompress (or, extract) it (i.e. 1.txt.7z)

p7zip -d -k 1.txt.7z

where,

-d to Decompress file,
-k to keep the input file, else it gets deleted.

If we have multiple files to deal with, then we can use 7z command-line utility. It comes packaged with p7zip-full.

Consider an example where we have three text files –

1.txt
2.txt
3.txt

To compress

7z a <ouput_file.7z> <input_file1> <input_file2> <input_file3> ...  <input_fileN>

where,

a – add files to the archive

so,

7z a compressed.7z 1.txt 2.txt 3.txt

This would get us the compressed file – compressed.7z

If we want to list the compressed file contents –

7z l <input_file.7z>

where,

l – list contents of our archive

7z l compressed.7z

Lastly, to extract its contents –

7z x <input_file.7z>

where,

x – to extract files

Hence,

7z x compressed.7z

In conclusion, we have discussed the most basic usage of p7zip command-line utility.

Additional info –

If we want to identify whether our file is a valid 7-zip archive. Then, we can use file command-line utility –

file <file_name>

If its a valid 7-zip archive, then it would return –

<file_name>: 7-zip archive data, version X.X