30.文件的压缩和解压-tar+zip+gzip+bzip2+xz

文件的压缩和解压

tar命令的压缩和解压

语法:tar [参数] 压缩包名.tar 被压缩文件

参数:

-c:创建,压缩文件

-x:提取,解压还原文件

-v:显示执行的详细过程

-f:指定备份文件

-t:列出压缩包中包括哪些文件,不解包,仅查看包中的内容

-C:(大写C)指定解压后文件存储路径

 

注意:压缩时,压缩包名后必须有.tar后缀

tar –cvf 压缩包名.tar 文件/目录

将dir_file文件压缩成dir_file.tar文件,存放在当前目录下

[root@bogon Desktop]# ll

total 18916

drwxr-xr-x+ 4 root root       76 Dec  4 21:10 dir_file

[root@bogon Desktop]# tar -cvf dir_file.tar dir_file/

[root@bogon Desktop]# ll

total 18916

drwxr-xr-x+ 4 root root       76 Dec  4 21:10 dir_file

-rw-r--r--  1 root root 19363840 Dec  4 21:14 dir_file.tar

 

tar –xvf 压缩包名.tar

将dir_file.tar文件压缩成dir_file文件,存放在当前目录下

[root@bogon Desktop]# ll

total 18916

-rw-r--r--  1 root root 19363840 Dec  4 21:14 dir_file.tar

[root@bogon Desktop]# tar -xvf dir_file.tar

[root@bogon Desktop]# ll

total 18916

drwxr-xr-x  4 root root       76 Dec  4 21:10 dir_file

-rw-r--r--  1 root root 19363840 Dec  4 21:14 dir_file.tar

 

tar –xvf 压缩包名 -C 路径

将dir_file.tar文件解压至/home目录下

[root@bogon home]# tar -xvf dir_file.tar -C /home/

[root@bogon home]# ls

ai  dir_file  wang  wangbin

 

tar –cvf 压缩包名.tar 文件 目录

将多个文件、目录压缩到一个.tar文件下

[root@bogon Desktop]# tar -cvf back.tar dir_file/ test.sh wangbin.txt

[root@bogon Desktop]# ll

total 18916

-rw-r--r--  1 root root 19363840 Dec  4 21:37 back.tar

 

tar –tvf 压缩包名.tar

查看压缩包中的内容,但不解压

[root@bogon Desktop]# tar -tvf back.tar

 

tar命令的归档压缩和解压

语法:tar [参数] 压缩包名 被压缩文件

参数:

-z:以gzip方式压缩,其扩展名为.tar.gz

-j:以bz2方式压缩,其扩展名为.tar.bz2

-J:以xz方式压缩,其扩展名为.tar.xz

 

注意:压缩时,压缩包名后必须有.tar.gz/.tar.bz2/.tar.xz后缀

tar –zcvf 压缩包名.tar.gz 被压缩的文件/目录

将文件/目录压缩成.tar.gz格式

[root@bogon Desktop]# ll

total 24056

drwxr-xr-x  4 root root       76 Dec  4 21:10 dir_file

[root@bogon Desktop]# tar -zcvf dir_file.tar.gz dir_file/

[root@bogon Desktop]# ll

total 24056

drwxr-xr-x  4 root root       76 Dec  4 21:10 dir_file

-rw-r--r--  1 root root  5263141 Dec  4 21:49 dir_file.tar.gz

 

tar –zxvf 压缩包名.tar.gz

将.tar.gz格式压缩包解压

[root@bogon Desktop]# ll

total 24056

-rw-r--r--  1 root root  5263141 Dec  4 21:49 dir_file.tar.gz

[root@bogon Desktop]# tar -zxvf dir_file.tar.gz

[root@bogon Desktop]# ll

total 24056

drwxr-xr-x  4 root root       76 Dec  4 21:10 dir_file

-rw-r--r--  1 root root  5263141 Dec  4 21:49 dir_file.tar.gz

 

tar –jcvf 压缩包名.tar.bz2 被压缩的文件/目录

将文件/目录压缩成.tar.bz2格式

[root@bogon Desktop]# ll

total 4072

drwxr-xr-x  4 root root      76 Dec  4 21:10 dir_file

[root@bogon Desktop]# tar -jcvf dir_file.tar.bz2 dir_file/

[root@bogon Desktop]# ll

total 4072

drwxr-xr-x  4 root root      76 Dec  4 21:10 dir_file

-rw-r--r--  1 root root 4162552 Dec  4 21:55 dir_file.tar.bz2

 

tar –jxvf 压缩包名.tar.bz2

将.tar.bz2压缩包解压

[root@bogon Desktop]# ll

total 4072

drwxr-xr-x  4 root root      76 Dec  4 21:10 dir_file

[root@bogon Desktop]# tar -jxvf dir_file.tar.bz2

[root@bogon Desktop]# ll

total 4072

drwxr-xr-x  4 root root      76 Dec  4 21:10 dir_file

-rw-r--r--  1 root root 4162552 Dec  4 21:55 dir_file.tar.bz2

tar –Jcvf 压缩包名.tar.xz 被压缩的文件/目录

将文件/目录压缩成.tar.xz格式

[root@bogon Desktop]# ll

total 4

drwxr-xr-x  4 root root   76 Dec  4 21:10 dir_file

[root@bogon Desktop]# tar -Jcvf dir_file.tar.xz dir_file/

[root@bogon Desktop]# ll

total 2368

drwxr-xr-x  4 root root      76 Dec  4 21:10 dir_file

-rw-r--r--  1 root root 2417068 Dec  4 22:07 dir_file.tar.xz

 

tar –Jxvf 压缩包名.tar.xz

将.tar.xz格式文件解压

[root@bogon Desktop]# ll

total 2368

drwxr-xr-x  4 root root      76 Dec  4 21:10 dir_file

[root@bogon Desktop]# tar -Jxvf dir_file.tar.xz

[root@bogon Desktop]# ll

total 2368

drwxr-xr-x  4 root root      76 Dec  4 21:10 dir_file

-rw-r--r--  1 root root 2417068 Dec  4 22:07 dir_file.tar.xz

 

三种压缩文件对比

[root@bogon Desktop]# ll

total 30488

drwxr-xr-x  4 root root       76 Dec  4 21:10 dir_file

-rw-r--r--  1 root root 19363840 Dec  4 22:11 dir_file.tar

-rw-r--r--  1 root root  4162552 Dec  4 22:11 dir_file.tar.bz2

-rw-r--r--  1 root root  5263141 Dec  4 22:11 dir_file.tar.gz

-rw-r--r--  1 root root  2417068 Dec  4 22:07 dir_file.tar.xz

 

.tar.xz这个压缩比例最高,压缩时间最长

.tar.bz2和.tar.gz是最常用的两种方法

 

三种压缩文件的解压,也都可以使用-C指定文件的解压路径

.zip压缩和解压

zip 压缩包名.zip 文件/目录

将文件/目录压缩成.zip格式

[root@bogon Desktop]# ll

total 30492

drwxr-xr-x  4 root root       76 Dec  4 21:10 dir_file

[root@bogon Desktop]# zip dir_file.zip dir_file

  adding: dir_file/ (stored 0%)

[root@bogon Desktop]# ll

total 30492

drwxr-xr-x  4 root root       76 Dec  4 21:10 dir_file

-rw-r--r--  1 root root      168 Dec  4 22:16 dir_file.zip

 

unzip 压缩包名.zip

将.zip格式文件解压

[root@bogon Desktop]# ll

total 30492

-rw-r--r--  1 root root      168 Dec  4 22:16 dir_file.zip

[root@bogon Desktop]# unzip dir_file.zip

Archive:  dir_file.zip

   creating: dir_file/

[root@bogon Desktop]# ll

total 30492

drwxr-xr-x  4 root root       76 Dec  4 21:10 dir_file

-rw-r--r--  1 root root      168 Dec  4 22:16 dir_file.zip

 

unzip 压缩包名.zip –d 路径

[root@bogon Desktop]# unzip dir_file.zip -d /home/

Archive:  dir_file.zip

 

gzip、bzip2、xz压缩命令

这三个命令一般用于压缩文件/解压,很少压缩/解压目录,但也有压缩/解压目录的功能。

gzip 文件/目录

使用gzip将文件/目录压缩成.gz格式,并删除源文件/目录。压缩速度最快

[root@bogon Desktop]# ll

total 4

-rw-rwxrwx+ 1 root root 1009 Dec  4 21:03 test.sh

[root@bogon Desktop]# gzip test.sh

[root@bogon Desktop]# ll

total 4

-rw-rwxrwx 1 root root 563 Dec  4 21:03 test.sh.gz

 

gzip –d压缩包

将.gz文件/目录解压

[root@bogon Desktop]# ll

total 4

-rw-rwxrwx 1 root root 563 Dec  4 21:03 test.sh.gz

[root@bogon Desktop]# gzip -d test.sh.gz

[root@bogon Desktop]# ll

total 4

-rw-rwxrwx 1 root root 1009 Dec  4 21:03 test.sh

bzip2 文件/目录

使用bzip2将文件/目录压缩成.bz2格式,并删除源文件/目录。(压缩生成的文件比gzip小)

[root@bogon Desktop]# ll

total 4

-rw-rwxrwx+ 1 root root 1009 Dec  4 21:03 test.sh

[root@bogon Desktop]# bzip2 test.sh

[root@bogon Desktop]# ll

total 4

-rw-rwxrwx 1 root root 602 Dec  4 21:03 test.sh.bz2

 

bzip2 –d压缩包

将.bz2文件解压,并覆盖原文件

[root@bogon Desktop]# ll

total 4

-rw-rwxrwx 1 root root 602 Dec  4 21:03 test.sh.bz2

[root@bogon Desktop]# bzip2 -d test.sh.bz2

[root@bogon Desktop]# ll

total 4

-rw-rwxrwx 1 root root 1009 Dec  4 21:03 test.sh

 

bzip2 –d压缩包 –k 文件/目录

将.bz2解压,不覆盖原文件

[root@bogon Desktop]# bzip2 -d test.sh.bz2 -k test.sh

bzip2: Output file test.sh already exists.

bzip2: Can't guess original name for test.sh -- using test.sh.out

bzip2: test.sh is not a bzip2 file.

[root@bogon Desktop]# ll

total 8

-rw-rwxrwx 1 root root 1009 Dec  4 21:03 test.sh

-rw-rwxrwx 1 root root  602 Dec  4 21:03 test.sh.bz2

 

xz 文件/目录

将文件压缩成.xz格式

[root@bogon Desktop]# ll

total 4

-rw-rwxrwx 1 root root 1009 Dec  4 21:03 test.sh

[root@bogon Desktop]# xz test.sh

[root@bogon Desktop]# ll

total 4

-rw-rwxrwx 1 root root 628 Dec  4 21:03 test.sh.xz

 

xz –d 压缩包

将.xz文件解压,并覆盖原文件

[root@bogon Desktop]# xz -d test.sh.xz

[root@bogon Desktop]# ll

total 4

-rw-rwxrwx 1 root root 1009 Dec  4 21:03 test.sh

 

xz –d 压缩包 –k 文件/目录

将.xz解压,不覆盖原文件

[root@bogon Desktop]# xz -d test.sh.xz -k test.sh

xz: test.sh: File format not recognized

[root@bogon Desktop]# ll

total 8

-rw-rwxrwx 1 root root 1009 Dec  4 21:03 test.sh

-rw-rwxrwx 1 root root  628 Dec  4 21:03 test.sh.xz

posted @ 2020-12-28 08:40  AI非AI  阅读(220)  评论(0)    收藏  举报