How to use 7zip on Linux command Line7zip is award winning open source file archiver. Besides operating on the 7z format, it supports many other popular archive formats and can seamlessly work on them. The 7zip project was started in 1999 by a Russian freelance programmer who is the developer and maintainer of this project. 7zip claims to have the highest compression ratio. As an end user, I have personally used 7zip many times and found it better than many other fellow archivers especially when compressing files into a 7z format. Its a great tool to have in your kitty so I decided to write a basic tutorial on how to use 7zip through 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
After both p7zip and p7zip-full are installed, you will see the following three command line utilities installed in your Linux box :
From p7zip wiki : The package includes three binaries, /usr/bin/7z, /usr/bin/7za, and /usr/bin/7zr. Their manpages explain the differences: 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
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 (loc
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 (loc
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 (loc
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 (loc
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 (loc
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
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 (loc
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 pointsThe following section from the man page is worth mentioning here :
For those who want to download the 7zip tool or want to look at the code, here is the project's home page on sourceforge.
|
