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

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值