Compressing and decompressing files under Linux is quite different from the Windows environment. Under Windows, you only need to install a tool like Winrar to decompress most files, while under Linux command line there are different compression and decompression methods for each kind of file.

Common Linux compression and decompression commands

Using tar to package files

The most common packaging program under Linux is tar (note that tar is packaging, not compression!) We often refer to the package that is created using the tar program as a tarball, and the commands for tarball files usually end with .tar. After generating a tarball, you can use other programs to compress it. tar command has a lot of options (you can check them with man tar).

Format: tar [primary option + secondary option] file or directory

Primary options: they cannot be present at the same time, telling tar what to do is mandatory

  • -c: create a new zip file, equivalent to packing
  • -x: release files from zip file, equivalent to unpacking
  • -t: list the contents of the zip file

Secondary options: Optional

  • -C: switch to the specified directory
  • -z: Does it also have the properties of gzip? That is, does it need to be compressed or decompressed with gzip? The usual format is gz or xx.tgz
  • -j: Does it also have the properties of bzip2? i.e. does it need to be compressed or decompressed with bzip2? Usually the format is bz2
  • -v: show files during compression
  • -f: Specify the compressed file, note that the file name should be immediately after f (your own), no other parameters should be added.
  • -p: use the original attributes of the original file
  • -exclude FILE: don’t pack the FILE during the compression process

Common commands.


  • tar -cvf file or directory name Packed object - pack only, not compress
  • tar -zcvf file or directory name - packed, compressed with gzip
  • tar -jcvf file or directory name - packed, compressed with bzip2

Check: (Note: choose whether to add z depending on whether it is gzip compressed or not)

  • tar -ztvf file or directory name – check for those files in the compressed file


  • tar -xvf file or directory name
  • tar -zxvf file or directory name


  • tar -cvf file2.tar /home/user2/file2 #pack the /home/user2/file2 file into a tar in the current directory, it should be noted that: the source file identified with an absolute path, after compressed with the tar command, the file name along with the absolute path (home/user2/, the root directory “/” is automatically removed) is compressed in together with the home file, two is not file2 after decompression using tar.
  • tar -cvf file2.tar -C /home/user2/file2 # Use the -C command to change the working directory of tar from the current directory to /home/user2 to compress the file2 file (without absolute path) into tar. Note: -C dir is used to change the working directory and is valid until the next -C dir in that command.
  • tar -xvf file2.tar -C /home/user2/file2 # Decompress the file to the specified directory path.

In addition to the most basic packing function, tar can also call other compression software, such as gzip, bzip2, etc., while packing or unpacking.

tar calls gzip

gzip is a compression program developed by the GNU organization. Files that end in .gz are the result of gzip compression. The opposite of gzip is the decompression program gunzip.

  • Decompression 1: gunzip FileName.gz
  • Decompress 2: gzip -d FileName.gz
  • Compression: gzip FileName

The -z argument is used in tar to invoke gzip.

  • tar -czf all.tar.gz *.jpg # This command is to make a tarball of all .jpg files and compress them with gzip to produce a gzip compressed package with the name gz
  • tar -xzf all.tar.gz #This command unpacks the above generated package.

Also, if you encounter a .tgz file, the operation is the same. .tgz is short for .tar.gz.

tar calls bzip2

bzip2 is a much more powerful compression program. Files ending in .bz2 are the result of bzip2 compression. The opposite decompression program to bzip2 is bunzip2.

  • Decompress 1: bzip2 -d FileName.bz2
  • Unzip2: bunzip2 FileName.bz2
  • Compression: bzip2 -z FileName

The -j parameter is used in tar to invoke gzip.

  • tar -cjf all.tar.bz2 *.jpg # This command is to make a tarball of all .jpg files and compress them with bzip2 to produce a bzip2 compressed package with the package name bz2
  • tar -xjf all.tar.bz2 #This command unpacks the above generated package.

The very uncommon .bz file is also decompressed using the same approach.

tar calls compress

compress is also a compression program. Currently, not as many people use compress as gzip and bzip2. Z-terminated files are the result of bzip2 compression. The opposite of compress is uncompress.

  • uncompress: uncompress FileName.
  • Compress: compress FileName

The -Z parameter is used in tar to invoke compress. here’s an example.

tar -cZf all.tar.Z *.jpg # This command will compress all .jpg files into a tarball and compress them to create an uncompressed package called all.tar.Z

tar -xZf all.tar.Z #This command will uncompress the above generated package

zip and unzip in Linux

zip command explained

zip is a widely used compression program that compresses files to produce a separate zip file with a “.zip” extension.

Syntax: zip [-AcdDfFghjJKlLmoqrSTuvVwXyz$][-b <working directory>][-ll][-n <word ending string>][-t <date time>][-<compression efficiency>][zip file][file...][-i <paradigm style>][-x <paradigm style>]


  • -A Adjusts the executable auto-decompression file.
  • -b <working directory> Specifies the directory to temporarily store the files.
  • -c Annotate each compressed file.
  • -d Delete the specified files from the compressed file.
  • -D Does not create directory names within the compressed file.
  • -f This parameter has a similar effect to specifying the “-u” parameter, but not only updates existing files, but also adds files to the zip file if they did not exist in the zip file.
  • -F Attempts to repair a corrupted zip file.
  • -g Append files to an existing zip file instead of creating a new one.
  • -h Online help.
  • -i<paradigm style> Compress only files that match the conditions.
  • -j Save only the file name and its contents without storing any directory names.
  • -J Remove unnecessary data in front of compressed files.
  • -k Use MS-DOS compatible format for file names.
  • -l Replace LF characters with LF+CR characters when compressing files.
  • -ll Replace LF+CR characters with LF characters when compressing files.
  • -L Display copyright information.
  • -m Delete the original file after compressing and adding it to the zip file, i.e., move the file to the zip file.
  • -n<word-terminated string> Does not compress files with a specific word-terminated string.
  • -o Set the change time of the compressed file to the same as the file that has the latest change time within the compressed file.
  • -q Does not display the command execution process.
  • -r Recursively processes all files and subdirectories in the specified directory together.
  • -S Contains system and hidden files.
  • -t<date time> Sets the date of the compressed file to the specified date.
  • -T Checks that each file within the backup file is correct.
  • -u Replaces the newer files inside the zip file.
  • -v Display command execution process or display version information.
  • -V Saves the file properties of the VMS operating system.
  • -w Suppose the version number in the file name, this parameter is only valid under VMS operating system.
  • -x<paradigm style> Exclude eligible files when compressing.
  • -X Does not save additional file attributes.
  • -y Saves the symbolic link directly, not the file to which the link refers.
  • -z Add comments to compressed files.
  • -$ Saves the volume name of the disk where the first compressed file is located.
  • -<compression efficiency> The compression efficiency is a value between 1 and 9.

unzip command detailed explanation

unzip is the unzip procedure for .zip compressed files.

Syntax: unzip [-cflptuvz][-agCjLMnoqsVX][-P <password>][.zip file][file][-d <directory>][-x <file>] or unzip [-Z]


  • -c Display the result of unzipping to the screen, and do the appropriate conversion of characters.
  • -f Update existing files.
  • -l Displays the files contained within the compressed file.
  • -p Similar to the -c argument, displays the result of decompression to the screen, but does not perform any conversions.
  • -t Check if the compressed file is correct.
  • -u is similar to the -f argument, but in addition to updating the existing files, it will also decompress other files in the zip file to the directory.
  • -v Displays detailed information when executed yes.
  • -z Displays only the comment text of the compressed file.
  • -a Perform the necessary character conversions on text files.
  • -b Do not perform character conversions on text files.
  • -C Case-sensitive file names in compressed files.
  • -j Do not process the original directory path in the compressed file.
  • -L Change all filenames in compressed files to lowercase.
  • -M Send the output to the more program for processing.
  • -n Do not overwrite the original files when decompressing.
  • -o No need to ask the user first, unzip overwrites the original file after execution.
  • -P<password> Use zip’s password option.
  • -q Execute without displaying any information.
  • -s Converts whitespace characters in filenames to underline characters.
  • -V Preserve the file version information of VMS.
  • -X Unzip while saving back the original UID/GID of the file.
  • [.zip file] Specifies a .zip compressed file.
  • [files] Specifies which files in the .zip archive to process.
  • -d<directory> Specifies the directory where the files will be stored after decompression.
  • -x<file> Specifies which files in the .zip zip archive not to process.
  • -Z unzip -Z is equivalent to executing the zipinfo command

So many parameters are estimated to have seen dizzy, the actual process is not so troublesome, the following sample list.

  • zip -r abc.txt dir1 #compress a file txt and a directory dir1 into a zip
  • unzip # unzip the file to the current directory
  • unzip abc? .zip # simultaneously unzip the files in the current directory zip, zip and means a character, if you use * means any multiple characters
  • unzip -v # only view the contents of the zip file, not decompression
  • unzip -t # verify the integrity of the zip file
  • unzip -j # Extract all the files in the zip archive to the first level directory, instead of creating multiple levels of directories with the same structure as the original one

Compressing and decompressing rar files under Linux

.rar is the most common compression file format in Windows, in Linux you need to install rarlinux if you want to use it, official:

cd rar
make && make install

Or use RPM to install UNRAR, download at:

The rar and unrar commands can be used after the installation is complete. Simple usage.

rar a all *.jpg #该命令是将所有.jpg的文件压缩成一个rar包,名为rar,该程序会将.rar 扩展名将自动附加到包名后。
unrar e all.rar #该命令是将rar中的所有文件解压出来。
$ rar --help

用法: rar <命令> -<选项 1> -<选项 N> <压缩档案> <文件...>
<@文件列表...> <解压缩路径\>

a 添加文件到压缩档案
c 添加压缩档案注释(注释的大小最大不能超过62000字节)
cf 添加文件备注( 当使用'v'参数时显示文件备注,最大不能超过32767字节)
ch 改变压缩档案参数
cw 向指定文件写入档案备注
d 从档案中删除文件
e 解压缩文件到当前目录(不创建任何子目录)
f 刷新档案中的文件(更新这些文件自它们被打包之后产生的变化,不在压缩档案中增加新的)
i[par]=<str> 在档案中查找特定字符
k 锁定档案(任何试图改变压缩档案中内容的命令都会被忽略)
l[t,b] 列出档案[technical, bare]
m[f] 移动到压缩档案中[只针对文件]
p 打印文件到标准输出
r 修补压缩档案
rc 重建丢失的分卷
rn 重命名压缩档案中的文件
rr[N] 添加数据恢复记录
rv[N] 创建恢复卷
s[name|-] 压缩档案与SFX(自解压文档)之间的转换
t 测试压缩档案文件
u 更新压缩档案中的文件
v[t,b] 列出压缩档案的详细信息[technical,bare]
x 解压缩到完整路径

- 停止对选项的扫描
ad 追加档案名称到目标路径
ag[format] 使用当前数据生成压缩档案名
ap<path> 在档案内部设置路径
as 同步档案内目录
av 设置认证检测(只有注册版提供此功能)
av- 关闭认证检测
c- 关闭备注显示
cfg- 关闭读取设置
cl 转换名称为小写字母
cu 转换名称为大写字母
df 压缩后删除原文件
dh 打开共享文件
ds 关闭针对于稳固档案的名称排序
dw 压缩后擦除文件
e[+]<attr> 设置文件排除与包含属性
ed 不添加空目录
en 不设置档案结束块
ep 名称中不包含路径
ep1 名称中不包含基本文件目录
ep3 展开路径到完整包含的驱动器名
f 刷新文件
hp[password] 加密文件数据和文件头部
id[c,d,p,q] 关闭消息
ierr 发送所有消息到标准错误输出
ilog[name] 记录错误日志到文件中(只有注册版提供此功能)
inul 关闭所有消息
isnd 打开声音
k 锁定压缩档案
kb 保持被解压过的已损坏的文件
m<0..5> 设置压缩等级(0-存储...3-默认...5-最大)
mc<par> 设置高级压缩参数
md<size> Dictionary size in KB (64,128,256,512,1024,2048,4096 or A-G)
ms[ext;ext] 存储指定的文件类型
n<file> 仅包含指定的文件
n@ 从标准输入读取文件名到包含内容中
n@<list> 在指定列表文件中包含文件
o[+|-] 设置重写模式
ol 替文件保存符号连接作为连接
or 自动重命名文件
ow 保存活存储文件的拥有者与群组
p[password] 设置密码
p- 不询问密码
r 对子目录进行递归操作
r0 仅用通配符名称对子目录进行递归操作
rr[N] 添加数据恢复记录
rv[N] 创建恢复
s[<N>,v[-],e] 创建稳固档案压缩
s- 关闭稳固档案压缩
sc<chr>[obj] 设置指定的字符串
sfx[name] 创建SFX档案
si[name] 从标准输入读取数据
sl<size> 处理文件的大小小于指定的值
sm<size> 处理文件的大小大于指定的值
t 压缩后测试文件
ta<date> 处理修改日期为指定日期之后的文件,<date>格式: YYYYMMDDHHMMSS 
tb<date> 处理修改日期为指定日期之前的文件,<date>格式: YYYYMMDDHHMMSS
tk 保持档案最初的时间
tl 设置档案时间为据现在最近的文件时间
tn<time> 处理比指定时间新的文件
to<time> 处理比指定时间旧的文件
ts<m,c,a>[N] 保存或存储文件时间(修改,创建,访问)
u 更新文件
v 创建自动大小的卷或列出所有卷
v<size>[k,b] 创建指定大小的卷size=<size>*1000 [*1024, *1]
ver[n] 文件版本控制
vn 使用旧的卷命名体系
vp 在每个卷之前暂停
w<path> 指定工作目录
x<file> 排除指定文件
x@ 排除从标准输入中读取的文件名称
x@<list> 从指定列表文件中指定要排除的文件
y 设定所有回答为yes
z[file] 从文件中读取压缩档案的备注

Note: The <command> part of rar does not have a “-” sign, only the <option> part has a “-” sign, so pay attention to this.

Example 1: Adding files or directories to a compressed archive, using the a command.

For example, add file1 to abc.rar, use a or m command, a command to add file1 to abc.rar file to keep the original file1 file unchanged, m command to move file1 file to file1.rar file (the original file1 file will be deleted after the compression is completed, note: m command only operates on files.)

rar a abc.rar file1

Explanation: If abc.rar file does not exist at this time, it will create abc.rar text file by itself, if abc.rar file already exists, it will compress the file1 file into abc.rar file, if a file1 file already exists in abc.rar file, it will update the file1 file. And the original file1 file still exists in the current directory, if you want to move the file1 file into file1.rar please use the m command, you can do the same for the directory.

Note: If you enter only “rar a file1.rar” command, and no file name or directory name, then all the files and folders in the current directory will be compressed into the file1.rar file. This point should be noted.

Example 2: decompression of the contents of the abc.rar file

You can use the e or x command, assuming that the abc.rar directory has a file named file1 and a directory named test, test directory has a file named file2.

rar e abc.rar

Note: Using the e command will extract the file1 file in abc.rar along with the file2 file in the test directory to the current directory. If you want to keep the directory structure in the abc.rar directory please use the x command.

rar x abc.rar

Note: At this point, the file1 file and the test directory will be extracted to the current folder.

Example 3: Adding comments to the entire zip file

rar c abc.rar

Description:After entering this command, the bottom of the screen will display

Processing archive test.

rarReading comment from stdin

and there is a cursor blinking, from the location of the blinking cursor to enter the comment information, finished typing press Ctrl + D to end the input

Example 4:Adding comments to a single file in a zip file, using the cf command.

If you want to add a comment to file1 in the abc.rar file

rar ch abc.rar file1

At this point the bottom of the screen will show

Reading comment for abc.rar : file1 from stdin

Type the comment you want to add for file1 from the cursor blinking out, Ctrl+D to end the input

Example 5:Writing comments for the entire file to a single file, using the cw command.

Add the comment you want to write for abc.rar to the test.txt file

rar cw abc.rar test.txt

Description: If a file named test.txt does not exist in the current directory, it will create a file named test.txt by itself and write the comments of abc.rar to the text.txt file, if a file named text.txt already exists in the current directory, you will be prompted whether to overwrite the existing file, if you choose Yes or If you choose Yes or All, the original content of test.txt will be cleared and the comments of abc.rar will be written to text.txt file.

Example 6: To delete a file or directory in a compressed archive, use the d command .

For example, you want to delete the file1 file in the abc.rar archive.

rar d abc.rar file1

Note: The command deletes the file1 file in the abc.rar file, which is also valid for the directory.

The above is the commonly used compression and decompression commands, <option> part of the use is no longer introduced, please try it yourself.

The use of the unrar command.

$unrar --help

用法: unrar <command> -<switch 1> -<switch N> <archive> <files...>
<@listfiles...> <path_to_extract\>

e 解压文件到当前目录
l[t,b] 列出压缩文档信息[technical, bare]
p 打印文件到标准输出
t 测试压缩我俄当
v[t,b] 列出压缩文档的详细信息[technical,bare]
x 解压文件到完整路径


The commands in unrar have the same effect as rar. You can see that unrar contains only some of the commands of rar, so you can do all the operations using rar.

7z file decompression on Linux

Personally, I prefer to install 7-zip on windows, and often use the .7z format when compressing. To decompress .7z files, you first need to install 7zip software.

yum install p7zip

The command to use 7zip is 7za.

How to use it after installation: 7za {a|d|l|e|u|x} zip file name {file list or directory, optional}

  • a add files to the zip archive or create a zip archive, e.g. add jpg to 7z, execute: 7za a 001.7z 001.jpg; pack the 001 directory execute: 7za a 001.7z 001.
  • d Remove files from the zip, such as removing jpg from 7z, execute: 7za d 001.7z 001.jpg
  • l List the files in the zip, for example, list the files in 7z, execute: 7za l 001.7z
  • e decompression to the current directory, the directory structure will be destroyed, such as the following directory and file 123/456/789.html in the rar
  • Execute: 7za e 001.rar, the directory 123 and 456 and the file html will be stored in the current directory.
  • x Extract with full path.


  • 7za a vps12.7z /www # Pack the files in the www directory under the root directory
  • 7za x vps12.7z -o/home/www # unpack, -o means output directory, note that there is no space between it and the directory path

Linux decompression Chinese messy code problem solution

In the above article, we learned the common Linux decompression commands. But in the actual Linux use process, decompression still has many doors and pits exist.

Graphical environment decompression software

  • Xarchive: Manjaro (Deepin version) comes with its own, lightweight decompression software
  • Peazip: supports both Windows and Linux systems
  • Engrampa: comes with decompression software for MATE desktop environment
  • B1 Free Archiver: supports both PC (Linux, Windows, Mac OS) and cell phone (Android)

The above is the decompression software I have installed, no matter which one is similar in function, the unified problem is that decompression of .rar and .zip files created under Windows will have Chinese messy code problem, because the default character set of Windows Chinese version is not UTF-8 but CP936.

Command line environment decompression software

In addition to the previously introduced tar, zip/unzip, rar/unrar, p7zip, here is a special recommendation for a command-line decompression software unar, unar from the Mac OS decompression software The Unarchiver, but to the Linux The power of Unar compared to other software is that it can set the encoding when decompressing.

unar common commands

  • Unzip the archive: unar
    • Various formats are supported by default, including zip, rar, etc.
  • Specify where to keep the unzip result: unar -o /home/dir
  • Specify the encoding: unar -e GBK
    • Use CP936, GB2312, GBK, GB18030 are the same
  • Specify the unzip password: unar -p password
    • You can also specify no password, the command line interface will show an interactive prompt asking for a password