跳到主要内容
18:54:36
🔅🔆

[toc]

归档压缩命令

1.归档命令tar​

1.1 命令说明​

用来压缩和解压文件,tar本身不具有压缩功能,他是调用压缩功能实现的

1.2 命令格式​

tar [option] 归档后的文件名 要归档的文件

1.3 选项​

1.3.1 压缩选项(都必须配合-f选项)​

说明

压缩选项前边的 - 可以不加

-f 使用归档文件​
-c 建立一个压缩文件​
$ ls
1.txt 2.txt 3.txt
$ tar zcf num.tar.gz 1.txt 2.txt 3.txt
$ ls
1.txt 2.txt 3.txt num.tar.gz
-h 不压缩链接文件,压缩链接文件源文件​
说明

不加 -h 选项,打包链接文件解压后会造成断链

$ ll
total 0
lrwxrwxrwx 1 root root 8 Aug 23 15:16 test-link.txt -> test.txt
-rw-r--r-- 1 root root 0 Aug 23 15:16 test.txt

# 创建压缩文件
$ tar zcf link.tar.gz test-link.txt

# 解压缩文件
$ tar xf link.tar.gz -C /tmp/

查看解压缩后的文件,发现链接丢失

iShot_2024-08-23_15.19.02

加-h选项后,打包链接文件并解压就没有问题了

$ ll
total 0
lrwxrwxrwx 1 root root 8 Aug 23 15:16 test-link.txt -> test.txt
-rw-r--r-- 1 root root 0 Aug 23 15:16 test.txt

# 创建压缩文件 加-h选项
$ tar zcfh link.tar.gz test-link.txt

# 解压缩文件
$ tar xf link.tar.gz -C /tmp/

# 查看解压后的文件
$ ll /tmp/test-link.txt
-rw-r--r-- 1 root root 0 Aug 23 15:16 /tmp/test-link.txt
-z 打包后调用gzip压缩​
$ tar zcf num.tar.gz 1.txt 2.txt 3.txt
$ ls
1.txt 2.txt 3.txt num.tar.gz
-j 打包后调用bzip2压缩​
$ tar zcf num.tar.bz2 1.txt 2.txt 3.txt
$ ls
1.txt 2.txt 3.txt num.tar.bz2
-r/-u 向归档文件末尾追加文件,更新归档文件​
注意

-r 、-u 选项只支持归档文件,不支持压缩文件

$ tar zcf num.tar.gz 1.txt 2.txt 3.txt
$ tar rf num.tar.gz 6.txt
tar: Cannot update compressed archives
tar: Error is not recoverable: exiting now
# 创建一个归档文件
$ tar cf num.tar 1.txt 2.txt 3.txt

# 查看归档文件
$ tar tf num.tar
1.txt
2.txt
3.txt

# 向归档文件末尾追加文件,更新归档文件
$ tar rf num.tar 6.txt

# 查看归档文件
$ tar tf num.tar
1.txt
2.txt
3.txt
6.txt
--exclude 指定不打包文件​
$ ls
1.txt 2.txt 3.txt 6.txt 7.txt 8.txt
$ tar zcf num.tar.gz *.txt --exclude=7.txt
$ tar tf num.tar.gz
1.txt
2.txt
3.txt
6.txt
8.txt
-P 不显示从成员名中删除​

不加 -P 的效果

$ tar zcf etc.tar.gz /etc
tar: Removing leading `/' from member names

可以看到,加上 -P 选项之后不提示 tar: Removing leading /' from member names`

tar zcfP etc.tar.gz /etc
-p 保持源文件属性不变​

1.3.2 解压缩选项​

-x 解压缩文件​

可以使用 xf 直接解压缩,系统会自动解压,也可以使用 -z 、-j 选项解压缩文件

-C 将压缩归档文件解压到哪个位置​

1.3.3 查看压缩选项​

-t 查看压缩文件中的文件​
$ tar tf num.tar.gz
1.txt
2.txt
3.txt

2.压缩命令gzip​

2.1 命令说明​

用来压缩文件

2.2 命令格式​

gzip [option] file

2.3 选项​

2.3.1 压缩选项​

-c 保留源文件,需要结合重定向符号​

不加 -c 选项,压缩文件后源文件没有被保留

$ ls
1.txt
$ gzip 1.txt
$ ls
1.txt.gz

gzip -c 源文件 > /xx/xx.gz

$ gzip -c 1.txt > /tmp/1.txt.gz
$ ll /tmp/1.txt.gz
-rw-r--r-- 1 root root 26 Aug 23 16:58 /tmp/1.txt.gz
-n​
说明

n 表示数字,用于指定压缩比,范围是 1-9 ,压缩比越大压缩时间越长,默认是 6

2.3.2 解压缩选项​

-d 解压缩​
$ ls
test.txt.gz
$ gzip -d test.txt.gz
$ ls
test.txt

2.3.3 查看压缩选项​

使用 zcat 命令查看 .gz 压缩文件的内容

$ echo 123 >> test.txt
$ echo test >> test.txt
$ ls
test.txt
$ gzip test.txt
$ zcat test.txt.gz
123
test

2.3.4 其他选项​

-v 显示详细信息​
$ gzip -v test.txt
test.txt: 0.0% -- replaced with test.txt.gz
-t 检测压缩文件正确性​
$ gzip -tv test.txt.gz
test.txt.gz: OK
-V 显示gzip版本信息​
$ gzip -V
gzip 1.5
Copyright (C) 2007, 2010, 2011 Free Software Foundation, Inc.
Copyright (C) 1993 Jean-loup Gailly.
This is free software. You may redistribute copies of it under the terms of
the GNU General Public License <http://www.gnu.org/licenses/gpl.html>.
There is NO WARRANTY, to the extent permitted by law.

Written by Jean-loup Gailly.

3.压缩命令bzip2​

3.1 命令说明​

bzip2 命令用来解压缩文件,压缩比比 bzip 大

3.2 命令格式​

gzip [option] file

3.3 选项​

3.3.1 压缩选项​

-k 保留源文件​

不加 -k 选项,压缩文件后源文件没有被保留

$ bzip2 test.txt
$ ls
test.txt.bz2

加 -k 选项,压缩后保留源文件

$ bzip2 -k test.txt
$ ls
test.txt test.txt.bz2
-n​

n 表示数字,用于指定压缩比,范围是 1-9,压缩比越大压缩时间越长,默认 6

3.3.2 解压缩选项​

-d 解压缩​
$ ls
test.txt.bz2
$ bzip2 -d test.txt.bz2
$ ls
test.txt

3.3.3 查看压缩选项​

使用 bzcat 查看 .bz2 压缩文件

$ echo 123 >> test.txt
$ echo test >> test.txt
$ ls
test.txt
$ bzip2 test.txt
$ ls
test.txt.bz2
$ bzcat test.txt.bz2
123
test

3.3.4 其他选项​

-v 显示详细信息​
$ bzip2 -dv test.txt.bz2
test.txt.bz2: done
-t 检测压缩文件正确性​
$ bzip2 -t test.txt.bz2
$ bzip2 -tv test.txt.bz2
test.txt.bz2: ok
-V 显示bzip2版本信息​
$ bzip2 -V
bzip2, a block-sorting file compressor. Version 1.0.6, 6-Sept-2010.

Copyright (C) 1996-2010 by Julian Seward.

This program is free software; you can redistribute it and/or modify
it under the terms set out in the LICENSE file, which is included
in the bzip2-1.0.6 source distribution.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
LICENSE file for more details.

bzip2: I won't write compressed data to a terminal.
bzip2: For help, type: `bzip2 --help'.

总结​

类型解压方法
*.tar用 tar -xvf 解压
*.gz用 gzip -d 或 gunzip 解压
*.tar.gz 和 *.tgz用 tar -xzf 解压
*.xz用 tar -jxvf 解压
*.bz2用 bzip2 -d 或 bunzip2 解压
*.tar.bz2用 tar -xjf 解压
*.Z用 uncompress 解压
*.tar.Z用 tar -xZf 解压
*.rar用 unrar -e 解压
*.zip用 unzip 解压
🛡️本站已安全运行2285天09时29分32秒
Bottom GIF
Top GIF