zip 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 :
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 :
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.
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 :
The syntax of 7z utility is :
7z command line examples
In all the examples below, I'll use the following files :
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 :
Here is a small example :
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 :
Lets extract the archive created in the previous example :
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 :
Here is an example :
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 :
Here is an example :
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 :
Here is an example :
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 :
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 :
Here is an example :
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 :
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 :
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 "directo ry/. prof ile" )
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.