Linux:常用压缩及解压缩命令

Linux:常用压缩及解压缩命令


后缀:tar

解包:tar xf redis-4.0.10.tar

打包:tar cf redis-4.0.10.tar redis-4.0.10

说明:

       -c, --create
              create a new archive
       -x, --extract, --get
              extract files from an archive
       -f, --file=ARCHIVE
              use archive file or device ARCHIVE
       -v, --verbose
              verbosely list files processed

例如:

$ ls -l
total 7500
-rw-rw-r--. 1 test1280 test1280 7680000 Aug 17 04:52 redis-4.0.10.tar
$ tar xf redis-4.0.10.tar 
$ ls -l
total 7504
drwxrwxr-x. 6 test1280 test1280    4096 Jun 13  2018 redis-4.0.10
-rw-rw-r--. 1 test1280 test1280 7680000 Aug 17 04:52 redis-4.0.10.tar
$ tree -L 2
.
├── redis-4.0.10
│   ├── 00-RELEASENOTES
│   ├── BUGS
│   ├── CONTRIBUTING
│   ├── COPYING
│   ├── deps
│   ├── INSTALL
│   ├── Makefile
│   ├── MANIFESTO
│   ├── README.md
│   ├── redis.conf
│   ├── runtest
│   ├── runtest-cluster
│   ├── runtest-sentinel
│   ├── sentinel.conf
│   ├── src
│   ├── tests
│   └── utils
└── redis-4.0.10.tar

5 directories, 14 files
$ rm redis-4.0.10.tar 
$ ls -l
total 4
drwxrwxr-x. 6 test1280 test1280 4096 Jun 13  2018 redis-4.0.10
$ tar cf redis-4.0.10.tar redis-4.0.10
$ ls -l
total 7504
drwxrwxr-x. 6 test1280 test1280    4096 Jun 13  2018 redis-4.0.10
-rw-rw-r--. 1 test1280 test1280 7680000 Aug 17 04:58 redis-4.0.10.tar

$ file redis-4.0.10.tar 
redis-4.0.10.tar: POSIX tar archive (GNU)

后缀:tar.gz、tgz

解压:tar zxf redis-4.0.10.tar.gz

压缩:tar zcf redis-4.0.10.tar.gz redis-4.0.10

说明:

       -z, --gzip
              filter the archive through gzip
       -j, --bzip2
              filter the archive through bzip2

tar jcf 打包压缩生成 tar.bz2 文件
tar jxf 解压解包获取 目录/文件

       -c, --create
              create a new archive
       -x, --extract, --get
              extract files from an archive
       -f, --file=ARCHIVE
              use archive file or device ARCHIVE
       -v, --verbose
              verbosely list files processed
例如:

$ ls -l 
total 1716
-rw-rw-r--. 1 test1280 test1280 1753203 Aug 17 04:50 redis-4.0.10.tar.gz
$ tar zxf redis-4.0.10.tar.gz 
$ ls -l
total 1720
drwxrwxr-x. 6 test1280 test1280    4096 Jun 13  2018 redis-4.0.10
-rw-rw-r--. 1 test1280 test1280 1753203 Aug 17 04:50 redis-4.0.10.tar.gz
$ tree -L 2
.
├── redis-4.0.10
│   ├── 00-RELEASENOTES
│   ├── BUGS
│   ├── CONTRIBUTING
│   ├── COPYING
│   ├── deps
│   ├── INSTALL
│   ├── Makefile
│   ├── MANIFESTO
│   ├── README.md
│   ├── redis.conf
│   ├── runtest
│   ├── runtest-cluster
│   ├── runtest-sentinel
│   ├── sentinel.conf
│   ├── src
│   ├── tests
│   └── utils
└── redis-4.0.10.tar.gz

5 directories, 14 files
$ rm redis-4.0.10.tar.gz
$ ls -l
total 4
drwxrwxr-x. 6 test1280 test1280 4096 Jun 13  2018 redis-4.0.10
$ tar zcf redis-4.0.10.tar.gz redis-4.0.10
$ ls -l
total 1720
drwxrwxr-x. 6 test1280 test1280    4096 Jun 13  2018 redis-4.0.10
-rw-rw-r--. 1 test1280 test1280 1753203 Aug 17 04:52 redis-4.0.10.tar.gz

$ file redis-4.0.10.tar.gz 
redis-4.0.10.tar.gz: gzip compressed data, from Unix, last modified: Sat Aug 17 04:52:57 2019

后缀:zip

解压:unzip redis-4.0.10.zip

压缩:zip -r redis-4.0.10.zip redis-4.0.10

说明:

unzip:
       -q     perform operations quietly (-qq = even quieter).

zip:
       -q
       --quiet
              Quiet mode; eliminate informational messages and comment prompts.  (Useful, for example, in shell scripts and background tasks).

       -r
       --recurse-paths
              Travel the directory structure recursively;

例如:

$ ls -l
total 2040
-rw-rw-r--. 1 test1280 test1280 2086113 Aug 17 05:14 redis-4.0.10.zip
$ unzip redis-4.0.10.zip 
Archive:  redis-4.0.10.zip
   creating: redis-4.0.10/
  inflating: redis-4.0.10/.gitignore  
……
  inflating: redis-4.0.10/utils/whatisdoing.sh  
$ ls -l
total 2044
drwxrwxr-x. 6 test1280 test1280    4096 Jun 13  2018 redis-4.0.10
-rw-rw-r--. 1 test1280 test1280 2086113 Aug 17 05:14 redis-4.0.10.zip
$ tree -L 2
.
├── redis-4.0.10
│   ├── 00-RELEASENOTES
│   ├── BUGS
│   ├── CONTRIBUTING
│   ├── COPYING
│   ├── deps
│   ├── INSTALL
│   ├── Makefile
│   ├── MANIFESTO
│   ├── README.md
│   ├── redis.conf
│   ├── runtest
│   ├── runtest-cluster
│   ├── runtest-sentinel
│   ├── sentinel.conf
│   ├── src
│   ├── tests
│   └── utils
└── redis-4.0.10.zip

5 directories, 14 files
$ rm redis-4.0.10.zip 
$ zip -r redis-4.0.10.zip redis-4.0.10
  adding: redis-4.0.10/ (stored 0%)
……
  adding: redis-4.0.10/utils/whatisdoing.sh (deflated 35%)
$ ls -l
total 2044
drwxrwxr-x. 6 test1280 test1280    4096 Jun 13  2018 redis-4.0.10
-rw-rw-r--. 1 test1280 test1280 2086113 Aug 17 05:16 redis-4.0.10.zip

$ file redis-4.0.10.zip 
redis-4.0.10.zip: Zip archive data, at least v1.0 to extract

后缀:gz

解压:
	gunzip redis-4.0.10.tar.gz
	gzip -d redis-4.0.10.tar.gz

压缩:
	gzip redis-4.0.10.tar

说明:
	gzip只能针对文件压缩,不能对目录压缩;
	如果要压缩目录,需要使用tar先将目录打包成单个文件(tar),然后再压缩。

	所以我们看到的tar.gz的后缀文件,其实都是使用tar打包gzip压缩的产物。

       -d --decompress --uncompress
              Decompress.

例如:

解压缩(或使用gunzip替代gzip -d):

$ ls -l
total 1716
-rw-r--r--. 1 test1280 test1280 1753182 Sep 14  2018 redis-4.0.10.tar.gz
$ gzip -d redis-4.0.10.tar.gz 
$ ls -l
total 7500
-rw-r--r--. 1 test1280 test1280 7680000 Sep 14  2018 redis-4.0.10.tar

获得压缩前的单个tar文件,继续:

$ tar xf redis-4.0.10.tar 
$ ls -l
total 7504
drwxrwxr-x. 6 test1280 test1280    4096 Jun 13  2018 redis-4.0.10
-rw-r--r--. 1 test1280 test1280 7680000 Sep 14  2018 redis-4.0.10.tar

获得解包后的目录。

当然,可以通过 tar zxf redis-4.0.10.tar.gz 一步到位。

压缩:

gzip不能打包压缩目录,需要先tar打包目录:

$ tar cf redis-4.0.10.tar redis-4.0.10
$ ls -l
total 7504
drwxrwxr-x. 6 test1280 test1280    4096 Jun 13  2018 redis-4.0.10
-rw-rw-r--. 1 test1280 test1280 7680000 Aug 17 05:50 redis-4.0.10.tar

使用gzip压缩tar文件:

$ gzip redis-4.0.10.tar 
$ ls -l
total 1720
drwxrwxr-x. 6 test1280 test1280    4096 Jun 13  2018 redis-4.0.10
-rw-rw-r--. 1 test1280 test1280 1753220 Aug 17 05:50 redis-4.0.10.tar.gz

同样,可以通过 tar zcf redis-4.0.10.tar.gz redis-4.0.10 一步到位。

后缀:bz2

解压:
		bunzip2 redis-4.0.10.tar.bz2
		bzip2 -d redis-4.0.10.tar.bz2

压缩:
		bzip2 redis-4.0.10.tar

说明:
	bzip2只能针对文件压缩,不能对目录压缩;
	如果要压缩目录,需要使用tar先将目录打包成单个文件(tar),然后再压缩。

	所以我们看到的tar.bz2的后缀文件,其实都是使用tar打包bzip2压缩的产物。

       -d --decompress
              Force decompression.

例如:

解压缩(或使用bunzip2替代bzip2 -d):

$ ls -l
total 1356
-rw-rw-r--. 1 test1280 test1280 1388011 Aug 17 05:56 redis-4.0.10.tar.bz2
$ bzip2 -d redis-4.0.10.tar.bz2 
$ ls -l
total 7500
-rw-rw-r--. 1 test1280 test1280 7680000 Aug 17 05:56 redis-4.0.10.tar

获得压缩前的单个tar文件,继续:

$ tar xf redis-4.0.10.tar 
$ ls -l
total 7504
drwxrwxr-x. 6 test1280 test1280    4096 Jun 13  2018 redis-4.0.10
-rw-r--r--. 1 test1280 test1280 7680000 Sep 14  2018 redis-4.0.10.tar

获得解包后的目录。

当然,可以通过 tar jxf redis-4.0.10.tar.bz2 一步到位。

压缩:

bzip2不能打包压缩目录,需要先tar打包目录:

$ tar cf redis-4.0.10.tar redis-4.0.10
$ ls -l
total 7504
drwxrwxr-x. 6 test1280 test1280    4096 Jun 13  2018 redis-4.0.10
-rw-rw-r--. 1 test1280 test1280 7680000 Aug 17 05:50 redis-4.0.10.tar

使用bzip2压缩tar文件:

$ bzip2 redis-4.0.10.tar 
$ ls -l
total 1356
-rw-rw-r--. 1 test1280 test1280 1388011 Aug 17 06:04 redis-4.0.10.tar.bz2

同样,可以通过 tar jcf redis-4.0.10.tar.bz2 redis-4.0.10 一步到位。

总结:

命令 功能
tar 打包、解包
gzip 压缩(单个文件,结合tar打包来压缩目录);gzip -d 解压缩
gunzip 解压,等同于 gzip -d
bzip2 压缩(单个文件,结合tar打包来压缩目录);bzip2 -d 解压缩
bunzip2 解压,等同于 bzip2 -d
zip 压缩(单个文件或目录递归)
unzip 解压
xz 压缩
unxz 解压
compress 压缩
uncompress 解压
后缀 操作 命令
.tar.gz 打包,压缩 tar zcf test1280.tar.gz test1280
.tar.bz2 打包,压缩 tar jcf test1280.tar.bz2 test1280
.gz 压缩 gzip file
.bz2 压缩 bzip2 file
.zip 压缩 zip [-r] test1280.zip test1280
.tar 打包 tar cf test1280.tar test1280
.tar.gz 解压,解包 tar zxf test1280.tar.gz
.tar.bz2 解压,解包 tar jxf test1280.tar.bz2
.gz 解压 gunzip file.gz
.bz2 解压 bunzip2 file.bz2
.zip 解压 unzip test1280.zip
.tar 解包 tar xf test1280.tar

Linux中文件后缀可以随意修改,有时不能确认真实的文件格式,可以使用file命令查看文件的真实格式,例如:

$ file redis-4.0.10.tar 
redis-4.0.10.tar: POSIX tar archive (GNU)

其他:

tar Jxf redis-4.0.10.tar.xz
tar Jcf redis-4.0.10.tar.xz redis-4.0.10

man tar:
        -J, --xz
              filter the archive through xz

tar支持z(gzip)、j(bzip2)、J(xz)、Z(compress)等几类。

关于rar文件,需要安装单独的rar工具压缩和解压。

试试 which xz、which compress


参考:

1.https://blog.csdn.net/yiliumu/article/details/20656597
2.https://www.cnblogs.com/EasonJim/p/8288585.html
3.https://www.jianshu.com/p/6bb6197d9a5e
4.https://zhuanlan.zhihu.com/p/33026265
5.https://juejin.im/post/5d00bf0ae51d4577407b1d19
6.https://www.runoob.com/w3cnote/linux-tar-gz.html
7.http://linux-wiki.cn/wiki/zh-hans/%E5%8E%8B%E7%BC%A9%E4%B8%8E%E8%A7%A3%E5%8E%8B

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章