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

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