Creating & Extracting 7z Archives on Linux

pradeep's Avatar author of Creating & Extracting 7z Archives on Linux
This is an article on Creating & Extracting 7z Archives on Linux in Linux.
7z is a compressed archive format which has a modular structure so the user can select the compression method, encryption method, block compression, unicode file names and support for very large files. 7z has been gaining popularity, and it offers great amount of compression of files so it's suited for many purposes, so here today we'll look at installing and using 7z on Linux.

Installing 7z



If you are on CentOS, Fedora etc. use the following command to install 7zip:

Code:
$ yum install 7zip

On ArchLinux:

Code:
$ pacman -S p7zip

On Ubuntu & other Debian derivatives:

Code:
$ sudo apt-get install p7zip-full

You'll get 3 binaries after install, namely 7z, 7za, & 7zr, the difference between each is as follows:

7z needs plugins to handle all archives.
7za is a standalone executable binary. 7za handles fewer archive formats than 7z, but is independent.
7zr is a "light-version" of 7za & standalone executable binary. 7zr handles fewer archive formats than 7z, but is independent.

Creating Archives



The command to create an archive is something like the following:

Code:
7z a <archive_name> <files/directories>

Here's an example:

Code:
[pradeep@home-desktop pradeep]# ls -lR
.:
total 4
drwxr-xr-x 2 pradeep pradeep 4096 Feb 20 03:12 Anjali

./Anjali:
total 24
-rw-r--r-- 1 pradeep pradeep 1024 Feb 20 03:10 alphabets.txt
-rw-r--r-- 1 pradeep pradeep 2323 Feb 20 03:12 friends.txt
-rw-r--r-- 1 pradeep pradeep 4532 Feb 20 03:11 mom-n-dad.txt
-rw-r--r-- 1 pradeep pradeep 5432 Feb 20 03:11 numbers.txt
[pradeep@home-desktop pradeep]# 7z a Anjali.7z Anjali

7-Zip 9.20  Copyright (c) 1999-2010 Igor Pavlov  2010-11-18
p7zip Version 9.20 (locale=C,Utf16=off,HugeFiles=on,2 CPUs)
Scanning

Creating archive Anjali.7z

Compressing  Anjali/alphabets.txt
Compressing  Anjali/friends.txt
Compressing  Anjali/mom-n-dad.txt
Compressing  Anjali/numbers.txt

Everything is Ok

Inflating Archives



You can extract files from archives by using the e argument with 7z command. Here's an example:

Code:
[pradeep@home-desktop Asha]# 7z e Anjali.7z

7-Zip 9.20  Copyright (c) 1999-2010 Igor Pavlov  2010-11-18
p7zip Version 9.20 (locale=C,Utf16=off,HugeFiles=on,2 CPUs)

Processing archive: Anjali.7z

Extracting  Anjali/alphabets.txt
Extracting  Anjali/friends.txt
Extracting  Anjali/mom-n-dad.txt
Extracting  Anjali/numbers.txt
Extracting  Anjali

Everything is Ok

Folders: 1
Files: 4
Size:       13311
Compressed: 13733
[pradeep@home-desktop Asha]# ls -lR
.:
total 44
drwxr-xr-x 2 pradeep pradeep  4096 Feb 20 03:12 Anjali
-rw-r--r-- 1 pradeep pradeep 13733 Feb 20 03:13 Anjali.7z
-rw-r--r-- 1 pradeep pradeep  1024 Feb 20 03:10 alphabets.txt
-rw-r--r-- 1 pradeep pradeep  2323 Feb 20 03:12 friends.txt
-rw-r--r-- 1 pradeep pradeep  4532 Feb 20 03:11 mom-n-dad.txt
-rw-r--r-- 1 pradeep pradeep  5432 Feb 20 03:11 numbers.txt

./Anjali:
total 0

If you have noticed we see that all files have been extracted into the same folder, which is not friendly at all times, this can be fixed by using the x argument instead of e. See the example below:

Code:
[pradeep@home-desktop Asha]# 7z x Anjali.7z

7-Zip 9.20  Copyright (c) 1999-2010 Igor Pavlov  2010-11-18
p7zip Version 9.20 (locale=C,Utf16=off,HugeFiles=on,2 CPUs)

Processing archive: Anjali.7z

Extracting  Anjali/alphabets.txt
Extracting  Anjali/friends.txt
Extracting  Anjali/mom-n-dad.txt
Extracting  Anjali/numbers.txt
Extracting  Anjali

Everything is Ok

Folders: 1
Files: 4
Size:       13311
Compressed: 13733
[pradeep@home-desktop Asha]# ls -lR
.:
total 20
drwxr-xr-x 2 pradeep pradeep  4096 Feb 20 03:12 Anjali
-rw-r--r-- 1 pradeep pradeep 13733 Feb 20 03:20 Anjali.7z

./Anjali:
total 24
-rw-r--r-- 1 pradeep pradeep 1024 Feb 20 03:10 alphabets.txt
-rw-r--r-- 1 pradeep pradeep 2323 Feb 20 03:12 friends.txt
-rw-r--r-- 1 pradeep pradeep 4532 Feb 20 03:11 mom-n-dad.txt
-rw-r--r-- 1 pradeep pradeep 5432 Feb 20 03:11 numbers.txt

Listing Files in a Archive



You can list the files in an archive using the argument l, see example below:

Code:
[pradeep@home-desktop Asha]# 7z l Anjali.7z

7-Zip 9.20  Copyright (c) 1999-2010 Igor Pavlov  2010-11-18
p7zip Version 9.20 (locale=C,Utf16=off,HugeFiles=on,2 CPUs)

Listing archive: Anjali.7z

--
Path = Anjali.7z
Type = 7z
Method = LZMA
Solid = +
Blocks = 1
Physical Size = 13733
Headers Size = 252

   Date      Time    Attr         Size   Compressed  Name
------------------- ----- ------------ ------------  ------------------------
2013-02-20 03:10:36 ....A         1024        13481  Anjali/alphabets.txt
2013-02-20 03:12:18 ....A         2323               Anjali/friends.txt
2013-02-20 03:11:29 ....A         4532               Anjali/mom-n-dad.txt
2013-02-20 03:11:08 ....A         5432               Anjali/numbers.txt
2013-02-20 03:12:18 D....            0            0  Anjali
------------------- ----- ------------ ------------  ------------------------
                                 13311        13481  4 files, 1 folders

Enjoy compressing :-)