Jason GNSS Beginner

Linux package compression and decompression commands

2020-09-26
Jason Ding

GNSS数据处理时经常需要面对各种各样的数据压缩文件,这些文件来自不同的机构,可能使用了不同的数据压缩格式。不同压缩格式的文件对应了不同的解压方法,Linux 常用的压缩与解压缩命令有:compress/uncompress、zip/unzip、gzip/gunzip、bzip2/bunzip2等,而tar本身属于打包命令,并不压缩文件,但是tar可以通过参数调用gzip和bzip2等命令,实现打包压缩一体化。

Win10开始windows支持linux子系统WSL(Windows Subsystem for Linux),两者共享文件系统(/mnt/win盘符),可以非常方便地在任意位置使用linux命令,故本文仅介绍Linux压缩命令。(有测站名为COM1和COM2,在Windows下这两个文件名的文件夹和文件无法删除/创建,这种情况下打开WSL,就可以愉快的操作了:-))


compress/uncompress

compress是一个相当古老的压缩命令,使用“Lempress-Ziv”编码压缩数据文件,对应的解压缩命令为uncompress,事实上uncompress是指向compress的符号连接,因此不论是压缩还是解压缩,都可通过compress指令单独完成。compress压缩后的文件扩展名为.Z ,compress命令只能压缩单个文件,若要压缩多个文件到一个文件,需要配合tar先打包再压缩。

Usage:

compress -[f][c][r][d][v] <file>
uncompress -[f][c][r] <file>

Examples:

compress data.txt                   # 将data.txt压缩为data.txt.Z
compress -f data.txt                # 强制压缩文件,若目的文件已经存在,则会被覆盖
compress -vf data.txt               # 压缩data.txt,并列印出压缩比例
compress -d data.txt.Z              # 解压缩data.txt.Z压缩包
compress -r *.txt                   # 将所有的txt文件分别压缩(1.txt->1.txt.Z;2.txt->2.txt.Z;...)
compress -c data.txt                # 将压缩后的文件输出到屏幕(不压缩文件,等同于预览)
compress -c data.txt > target.txt.Z # 将压缩后的文件输出后再导入target.tzt.Z(可以自定义压缩文档名)
uncompress -f data.txt.Z            # 强制解压缩文件,若目的文件已经存在,则会被覆盖
uncompress -c data.txt.Z            # 将压缩后的文件输出到屏幕(不解压文件,等同于预览)
uncompress -r *.txt.Z               # 解压全部的压缩文件

zip/unzip

Usage:

zip -[d][u][m][o][q][r] <compressed file> <files to be compressed> -[x] <excluded files>
unzip -[c][l][t][v][q] <compressed file> -[d] <target dir>

Examples:

zip archive.zip data1.txt data2.txt         # 将文件data1.txt和data2.txt压缩到archive.zip
zip -d archive.zip data1.txt                # 把文件data1.txt从压缩包中剔除
zip -u archive.zip data1.txt                # 更新压缩包中的文件,若data1.txt不在压缩包中则添加进去,若在则更新
zip -m archive.zip data1.txt data2.txt      # 压缩后删除源文件
zip -o archive.zip 123.txt 234.txt          # 将压缩文件内的所有文件的最新变动时间设为压缩的时间
zip -q archive.zip *.txt                    # 压缩全部txt文件,安静模式输出,不显示压缩的过程
zip -q -r archive.zip data                  # 递归压缩,将data目录下的所有子文件以及文件一起压缩
zip -r archive.zip data -x "./data/234.txt" # 排除data目录下的234.txt文件
unzip -c archive.zip                        # 显示压缩包中的信息,具体到每个文件中的内容
unzip -l archive.zip                        # 显示压缩包中的文件
unzip -t archive.zip                        # 检查压缩文件是否正确
unzip -v archive.zip                        # 不解压压缩文件,查看压缩包里面的内容(查看显示的文件列表还包含压缩比率)
unzip -q archive.zip -d /opt/               # 安静模式下解压文件到/opt目录

gzip/gunzip

gzip压缩的文件扩展名为.gz,gunzip为gzip的硬链接,因此不论是压缩或解压缩,都可通过gzip指令单独完成。

Usage:

gzip -[f][k][r][d][v] <files to be compressed>
gunzip -[c][f][k][l][r] <compressed file>

Examples:

gzip mydoc.txt         # 压缩mydoc.txt为mydoc.txt.gz
gzip -f mydoc.txt      # 强制压缩文件,若mydoc.txt.gz已经存在,则会被覆盖
gzip -k mydoc.txt      # 保留原始文件,即mydoc.txt
gzip -r testfolder     # 递归压缩,压缩testfolder文件夹下的每个文件,注意是遍历逐个压缩每个文件,不打包
gzip -d mydoc.txt.gz   # 解压缩文件mydoc.txt.gz
gzip -v mydoc.txt      # 预览每个压缩或解压缩文件的名称和减少的百分比
gunzip -c mydoc.txt.gz # 不解压缩的情况下查看压缩文件中的文本
gunzip -f mydoc.txt.gz # 强制解压缩文件,若mydoc.txt已经存在,则会被覆盖
gunzip -k mydoc.txt.gz # 保留原始文件
gunzip -l data.gz      # 显示压缩包中的文件
gunzip -r data         # 递归解压缩data文件夹及其子文件夹中的所有文件

bzip2/bunzip2

bzip2命令是.bz2文件的压缩程序。bzip2采用新的压缩演算法,压缩效果比传统的LZ77/LZ78压缩演算法来得好。若没有加上任何参数,bzip2压缩完文件后会产生.bz2的压缩文件,并删除原始的文件。bunzip2参数与bzip2参数相似。

Usage:

bzip2 -[k][d][][][][] <files to be compressed>

Examples:

bzip2 data.txt        # 压缩data.txt文件为data.txt.bz2,同时删除源文件
bzip2 -k data.txt     # 压缩data.txt文件为data.txt.bz2,保留源文件
bzip2 -f data.txt     # 强制压缩,若data.txt.bz2已经存在,则会被覆盖
bzip2 -d data.txt.bz2 # 解压缩data.txt.bz2文件
bzip2 -v data.txt     # 压缩或解压缩文件时,显示详细的信息

tar

1. 打包

Usage:

tar -[z][Z][j][J][x][t][c][v][p][P]f <compressed file> -C <target dir> <files to be compressed>

Examples:

tar -cf archive.tar foo bar                # 将文件foo和bar打包为archive.tar
tar -rf archive.tar abb                    # 将abb文件增加到archive.tar包里
tar -tvf archive.tar                       # 查看并显示archive.tar压缩包内的所有文件
tar -xf archive.tar -C /opt/               # 解压提取archive.tar到/opt目录下
tar -cf archive.tar --exclude=aa.txt happy # 打包happy目录下除了aa.txt外的文件到archive.tar
  • 必要参数:-f

  • 可选参数:-z, -Z, -j, -J, -x, -t, -c, -C, -v, -p, -P

参数释义

参数 含义 参数 含义
-f 指定压缩后的文件名,多参数时放在最后 -t 查看压缩包内容
-z 是否同时具有gz属性 -c 建立一个打包文档
-Z 是否同时具有Z属性 -C 指定目录,表示指定解压缩包的内容和打包的内容存放的目录
-j 是否同时具有bz2属性 -v 显示压缩或打包的内容
-J 是否同时具有xz属性 -p 保留备份数据的原本权限与属性,常用于备份重要的配置文件
-x 解压缩,提取打包内容 -P 保留绝对路径
-r 增加文件到tar包    
2. 压缩

四种压缩方式:

  1. gzip: 速度最快,应用最广泛,后缀为.tar.gztgz
  2. bzip2: 压缩的文件小,压缩率高,后缀为.tar.bz2
  3. xz: 最新的压缩方式,可以自动提供最佳的压缩率,后缀为.tar.xz
  4. compress: 应用较少,后缀为.tar.Z

Examples:

tar -zcvf archive.tar.gz foo bar  # 使用gzip压缩
tar -jcvf archive.tar.bz2 foo bar # 使用bzip2压缩
tar -Jcvf archive.tar.xz foo bar  # 使用xz压缩
tar -Zcvf  archive.tar.Z foo bar  # 使用compress压缩
3. 解压缩

tar命令解压文件时不需要指定压缩方式,可以自动识别压缩的方式,然后使用对应的方式解压文件。

tar -xf archive.tar.gz -C /opt1/   # 将文件解压到/opt1目录下
tar -xf archive.tar.bz2 -C /opt2/  # 将文件解压到/opt2目录下
tar -xf archive.tar.xz -C /opt3/   # 将文件解压到/opt3目录下
tar -xf archive.tar.Z -C /opt4/    # 将文件解压到/opt4目录下

rar/unrar

Examples:

rar a file file.ext           # 将file.ext添加/更新到file.rar,如果file.rar不存在将创建file.rar
rar a -r a.rar a/             # 递归的将a/下所有东西压缩到a.rar
rar x test.rar                # 带路径解压文档中内容到当前目录
rar a -df test.rar file1.txt  # 压缩到test.rar中之后,删除源文件

References

https://www.runoob.com/w3cnote/linux-tar-gz.html

https://www.cnblogs.com/vurtne-lu/p/6208275.html

https://www.geeksforgeeks.org/gunzip-command-in-linux-with-examples/?ref=lbp

https://www.cnblogs.com/wuxiang/p/4799971.html


下一篇 Matlab Plot Skills

Comments

Content