How to use 7zip on Linux command Line

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 :
 
$ 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 "dir ecto 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.


Tags:  7zip 7za linux archiver p7zip 7zr
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
7z文件管理器 是一种主流高效的压缩格式,它拥有极高的压缩比。在计算机科学中,7z是一种可以使用多种压缩算法进行数据压缩的档案格式。该格式最初被7-Zip实现并采用,但是这种档案格式是公有的,并且7-Zip软件本身亦在GNU宽通用公共许可证 (GNU LGPL)协议下开放源代码。   7z压缩包的主要特征 开源且模块化的组件结构(允许使用任何压缩,转换或加密算法) 最高的压缩比 强大的 AES-256 加密 可更改和配置压缩的算法 支持超大文件(最大支持到16EB) Unicode文件名支持 支持固实压缩,容许内类的档案在用一个串流中压缩,使类似的内容被有效的压缩 支持档案的文件头压缩 支援多线程压缩 7z格式支持256位键钥AES算法加密。键钥则由用户提供的暗码进行SHA-256hash算法得到(使用大量迭代以使得对暗码的暴力解码更加困难) 7z压缩包的安装方法   双击打开下载好的安装包h7z_x86.exe,上图显示的是安装路径不需要管他点击「install」即可。 7z压缩软件界面 7z压缩包支持的格式以及特色使用平台 压缩/解压缩:7z、 XZ、 BZIP2、 GZIP、 TAR、 ZIP 和 WIM 仅解压缩:ARJ、CAB、CHM、CPIO、CramFS、DEB、DMG、FAT、HFS、ISO、LZH、LZMA、MBR、MSI、NSIS、NTFS、RAR、RPM、SquashFS、UDF、VHD、WIM、XAR、7Z 对于 ZIP 及 GZIP 格式,7-Zip 能提供比使用 PKZip 及 WinZip 高 2-10% 的压缩比 为 7z 与 ZIP 提供更完善的 AES-256 加密算法 7z 格式支持创建自释放(SFX)压缩包 Windows 资源管理器集成 强大的的文件管理器 强大的命令行版本 支持 FAR Manager 插件 支持 79 种语言 7-Zip 适用于 Windows 7 / Vista / XP / 2008 / 2003 / 2000 / NT / ME / 98 并且有面向 Mac OS X、Linux、Unix 平台的命令行版本。 怎么使用7z压缩包软件   右击文件->选择7Z->添加压缩包;添加到压缩包会出现对话框,里面设置参数,压缩的格式、压缩等级、压缩方法、字典大小、单词大小、固实数据大小,以及CPU的参数哦,这里固实数据是显示你的内存,也就是你自己机器本身内存是多大的,他会占用您的内存空间。   7Z压缩的卸载 依次打开我的电脑(计算机)-> C盘 ->Program Files7-Zip->Uninstall.exe 或者依次打开我的电脑->卸载或更改程序->卸载你所卸载的软件,您可以用 Window XP控制面板中的添加或删除程序功能,或用 Windows Vista、Windows 7中的程序和功能删除7zip , 然后点击开始卸载。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值