Monday, 15 October 2018

How to use 7zip on Linux command Line

 
7zip is distributed under LGPL license as a free software to use. The version available for Linux is known as p7zip package. I am using Linux mint so the installation part of this tutorial would be most suited for Linux mint, Ubuntu and other debain Linux distributions while the examples are universal for any Linux distribution. 
 

How to install p7zip package

When I started exploring 7zip package on my Linux mint machine, soon I found that its not currently installed. So I decided to install it. The first command that I used to install this package was :
 
$ sudo apt-get install p7zip
[sudo] password for himanshu: 
Reading package lists... Done
Building dependency tree       
Reading state information... Done
p7zip is already the newest version.
0 upgraded, 0 newly installed, 0 to remove and 50 not upgraded.
 
The output pointed that p7zip is already installed. Then I researched and bit and found that to install 7z archiver as a command line utility, I need to install the p7zip-full package. So I tried to install this package :
 
$ sudo apt-get install p7zip-full
Reading package lists... Done
Building dependency tree       
Reading state information... Done
Suggested packages:
  p7zip-rar
The following NEW packages will be installed:
  p7zip-full
0 upgraded, 1 newly installed, 0 to remove and 50 not upgraded.
Need to get 1,419kB of archives.
After this operation, 3,662kB of additional disk space will be used.
WARNING: The following packages cannot be authenticated!
  p7zip-full
Authentication warning overridden.
Get:1 http://archive.ubuntu.com/ubuntu/ lucid/universe p7zip-full 9.04~dfsg.1-1 [1,419kB]
Fetched 1,419kB in 29s (48.0kB/s)                                                                                                                           
Selecting previously deselected package p7zip-full.
(Reading database ... 133376 files and directories currently installed.)
Unpacking p7zip-full (from .../p7zip-full_9.04~dfsg.1-1_amd64.deb) ...
Processing triggers for man-db ...
Setting up p7zip-full (9.04~dfsg.1-1) ...
 
After both p7zip and p7zip-full are installed, you will see the following three command line utilities installed in your Linux box :
  • 7z
  • 7za
  • 7zr

From p7zip wiki :
The package includes three binaries, /usr/bin/7z, /usr/bin/7za, and /usr/bin/7zr. Their manpages explain the differences:

7z uses plugins to handle archives.
7za is a stand-alone executable. 7za handles fewer archive formats than 7z, but does not need any others.
7zr is a stand-alone executable. 7zr handles fewer archive formats than 7z, but does not need any others. 7zr is a "light-version" of 7za that only handles 7z archives.

One thing that was different at my end was that the utility 7zr was installed as part of p7zip package while the other two were installed as part of p7zip-full package. I still don't know the reason behind this.


Anyway, now all the three utilities were present and this can be confirmed by the 'whereis' command.
$ whereis 7z
7z: /usr/bin/7z /usr/share/man/man1/7z.1.gz
$ whereis 7za
7za: /usr/bin/7za /usr/share/man/man1/7za.1.gz
$ whereis 7zr
7zr: /usr/bin/7zr /usr/share/man/man1/7zr.1.gz
 
 
This was all about installation. Now lets try to explore the 7z utility. As we know that the 7z utility is the main utility, so we will discuss only 7z here.

The syntax of 7z utility is :
7z [adeltux] [-] [SWITCH]  ...
 

7z command line examples

In all the examples below, I'll use the following files :
 
$ ls
abc.txt  basic  bufferoverflow.c
In the above output, 'basic' is a directory while the other two are files.
 

1. Create an archive

This can be done by using the function letter 'a'.

Here is a small example :
$ 7z a basic.7z basic

7-Zip 9.04 beta  Copyright (c) 1999-2009 Igor Pavlov  2009-05-30
p7zip Version 9.04 (locale=en_IN,Utf16=on,HugeFiles=on,2 CPUs)
Scanning

Creating archive basic.7z

Compressing  basic/helloworld.c      
Compressing  basic/helloworld.o      
Compressing  basic/helloworld.i      
Compressing  basic/helloworld.s      
Compressing  basic/helloworld      

Everything is Ok

$ ls
abc.txt  basic  basic.7z  bufferoverflow.c
So we can see that, using 7z an archive basic.7z was created for the directory 'basic'.
 

2. Extract an archive

This can be done using the function letter 'e'.

Lets extract the archive created in the previous example :
$ 7z e basic.7z

7-Zip 9.04 beta  Copyright (c) 1999-2009 Igor Pavlov  2009-05-30
p7zip Version 9.04 (locale=en_IN,Utf16=on,HugeFiles=on,2 CPUs)

Processing archive: basic.7z

Extracting  basic/helloworld.c
Extracting  basic/helloworld.o
Extracting  basic/helloworld.i
Extracting  basic/helloworld.s
Extracting  basic/helloworld
Extracting  basic

Everything is Ok

Folders: 1
Files: 5
Size:       27541
Compressed: 5805

$ ls
abc.txt  basic  basic.7z  bufferoverflow.c  helloworld  helloworld.c  helloworld.i  helloworld.o  helloworld.s
$ 
So we see that basic.7z was extracted and all the files were extracted into the same folder. The files extracted are shown in bold in the output above.
 

3. List archive details

This can be done by using the function letter 'l'.

Here is an example :
$ 7z l basic.7z

7-Zip 9.04 beta  Copyright (c) 1999-2009 Igor Pavlov  2009-05-30
p7zip Version 9.04 (locale=en_IN,Utf16=on,HugeFiles=on,2 CPUs)

Listing archive: basic.7z

----
Path = basic.7z
Type = 7z
Method = LZMA BCJ
Solid = +
Blocks = 2
Physical Size = 5805
Headers Size = 232

   Date      Time    Attr         Size   Compressed  Name
------------------- ----- ------------ ------------  ------------------------
2012-09-09 16:47:17 ....A          192         3600  basic/helloworld.c
2012-09-09 16:47:17 ....A         1568               basic/helloworld.o
2012-09-09 16:47:17 ....A        16700               basic/helloworld.i
2012-09-09 16:47:17 ....A          577               basic/helloworld.s
2012-09-09 16:47:17 ....A         8504         1973  basic/helloworld
2012-09-09 16:47:17 D....            0            0  basic
------------------- ----- ------------ ------------  ------------------------
                                 27541         5573  5 files, 1 folders
So we see that the details of the archive basic.7z were listed in the output.
 

4. Test integrity of the archive

This can be done using the function letter 't'.

Here is an example :
$ 7z t basic.7z basic

7-Zip 9.04 beta  Copyright (c) 1999-2009 Igor Pavlov  2009-05-30
p7zip Version 9.04 (locale=en_IN,Utf16=on,HugeFiles=on,2 CPUs)

Processing archive: basic.7z

Testing     basic/helloworld.c
Testing     basic/helloworld.o
Testing     basic/helloworld.i
Testing     basic/helloworld.s
Testing     basic/helloworld
Testing     basic

Everything is Ok

Folders: 1
Files: 5
Size:       27541
Compressed: 17566
So we see that integrity check was done.
 

5. Update an existing archive

This can be done using the function letter 'u'.

Here is an example :
$ 7z u basic.7z basic

7-Zip 9.04 beta  Copyright (c) 1999-2009 Igor Pavlov  2009-05-30
p7zip Version 9.04 (locale=en_IN,Utf16=on,HugeFiles=on,2 CPUs)

Scanning

Updating archive basic.7z

      
Everything is Ok
 
So we see that the output says that archive is already up to date. Lets now introduce a new file into the directory 'basic' and again the run the update command :
$ cp bufferoverflow.c basic/
$ ls basic/
bufferoverflow.c  helloworld  helloworld.c  helloworld.i  helloworld.o  helloworld.s

$ 7z u basic.7z basic

7-Zip 9.04 beta  Copyright (c) 1999-2009 Igor Pavlov  2009-05-30
p7zip Version 9.04 (locale=en_IN,Utf16=on,HugeFiles=on,2 CPUs)

Scanning

Updating archive basic.7z

Compressing  basic/bufferoverflow.c 

Everything is Ok

$
So firstly the file bufferoverflow.c was copied to directory 'basic' and then the update command was run again. It can be seen in the output that the archive was updated by compressing this new file and adding it to the archive.
 

6. Delete a file from the archive

This can be done using the function letter 'd' along with the switch -r. This switch tells the 7zip utility to traverse the subdirectories.

Here is an example :
$ 7z l basic.7z

7-Zip 9.04 beta  Copyright (c) 1999-2009 Igor Pavlov  2009-05-30
p7zip Version 9.04 (locale=en_IN,Utf16=on,HugeFiles=on,2 CPUs)

Listing archive: basic.7z

----
Path = basic.7z
Type = 7z
Method = LZMA BCJ
Solid = +
Blocks = 3
Physical Size = 6154
Headers Size = 269

   Date      Time    Attr         Size   Compressed  Name
------------------- ----- ------------ ------------  ------------------------
2012-09-09 16:47:17 ....A          192         3600  basic/helloworld.c
2012-09-09 16:47:17 ....A         1568               basic/helloworld.o
2012-09-09 16:47:17 ....A        16700               basic/helloworld.i
2012-09-09 16:47:17 ....A          577               basic/helloworld.s
2012-09-09 17:33:51 ....A          634          312  basic/bufferoverflow.c
2012-09-09 16:47:17 ....A         8504         1973  basic/helloworld
2012-09-09 17:33:34 D....            0            0  basic
------------------- ----- ------------ ------------  ------------------------
                                 28175         5885  6 files, 1 folders

$ 7z d basic.7z helloworld -r

7-Zip 9.04 beta  Copyright (c) 1999-2009 Igor Pavlov  2009-05-30
p7zip Version 9.04 (locale=en_IN,Utf16=on,HugeFiles=on,2 CPUs)

Updating archive basic.7z

      
Everything is Ok

$ 7z l basic.7z

7-Zip 9.04 beta  Copyright (c) 1999-2009 Igor Pavlov  2009-05-30
p7zip Version 9.04 (locale=en_IN,Utf16=on,HugeFiles=on,2 CPUs)

Listing archive: basic.7z

----
Path = basic.7z
Type = 7z
Method = LZMA
Solid = +
Blocks = 2
Physical Size = 4165
Headers Size = 253

   Date      Time    Attr         Size   Compressed  Name
------------------- ----- ------------ ------------  ------------------------
2012-09-09 16:47:17 ....A          192         3600  basic/helloworld.c
2012-09-09 16:47:17 ....A         1568               basic/helloworld.o
2012-09-09 16:47:17 ....A        16700               basic/helloworld.i
2012-09-09 16:47:17 ....A          577               basic/helloworld.s
2012-09-09 17:33:51 ....A          634          312  basic/bufferoverflow.c
2012-09-09 17:33:34 D....            0            0  basic
------------------- ----- ------------ ------------  ------------------------
                                 19671         3912  5 files, 1 folders
$
 
First we checked the files in the archive, next we tried to delete the 'helloworld' executable. Again when the entries in the archive were listed, no trace of 'helloworld' was found. So we can say that this file was successfully deleted from the archive.


NOTE : Besides function letters that we used in the examples above, there are numerous switches also that we can use with this utility. For information on switches, you should go to the man page of 7z utility.

An example, from the man page that describes the use of switches :
$7z a -t7z -m0=lzma -mx=9 -mfb=64 -md=32m -ms=on archive.7z dir1

adds all files from directory "dir1" to archive archive.7z using "ultra settings"

       -t7z   7z archive

       -m0=lzma
              lzma method

       -mx=9  level of compression = 9 (Ultra)

       -mfb=64
              number of fast bytes for LZMA = 64

       -md=32m
              dictionary size = 32 megabytes

       -ms=on solid archive = on
So we see that switches can be used to customize the settings.
 

Some important points

The following section from the man page is worth mentioning here :
DO NOT USE the 7-zip format for backup purpose on Linux/Unix because :

- 7-zip does not store the owner/group of the file.

On Linux/Unix, in order to backup directories you must use tar :

- to backup a directory :
tar cf - directory | 7za a -si directory.tar.7z

- to restore your backup :
7za x -so directory.tar.7z | tar xf

- If you want to send files and directories (not the owner of file) to others Unix/MacOS/Windows users, you can use the 7-zip format.
example : 7za a directory.7z directory

Do not use "-r" because this flag does not do what you think.

Do not use directory/* because of ".*" files (example : "directory/*" does not match "directory/.profile"

0 comments:

Post a Comment