压缩类命令
linux里经典的压缩后缀名: .tar .zip .gz .bz2 .xz .tar.gz .tar.xz .tar.bz2
1.zip命令
zip [选项] 压缩包名 源文件或源目录列表
[root@localhost home]# zip hello.zip hello.py
adding: hello.py (deflated 14%)
[root@localhost home]# ll hello.zip
-rw-r--r--. 1 root root 262 12月 4 10:43 hello.zip
-r 压缩目录
[root@localhost home]# mkdir test
[root@localhost home]# zip -r test.zip test
adding: test/ (stored 0%)
adding: test/halo.txt (stored 0%)
[root@localhost home]# ls -dl test.zip
-rw-r--r--. 1 root root 314 12月 4 10:49 test.zip
解压:unzip
[root@localhost home]# unzip hello.zip
Archive: hello.zip
replace hello.py? [y]es, [n]o, [A]ll, [N]one, [r]ename: y
inflating: hello.py
Archive压缩格式的文件都称呼为归档文件
2.gzip命令
gzip [选项] 文件名
[root@localhost home]# touch a
[root@localhost home]# ls
a
[root@localhost home]# gzip a
[root@localhost home]# ls
a.gz
gzip命令直接对文件进行压缩,并生成xx.gz文件
[root@localhost home]# mkdir a
[root@localhost home]# gizp a
-bash: gizp: 未找到命令
只能给文件进行压缩
若要保留源文件
[root@localhost home]# touch a.txt
[root@localhost home]# gzip -c a.txt >a.gz
[root@localhost home]# ls
a.gz a.txt
解压:gunzip
[root@localhost home]# ls
a.gz test
[root@localhost home]# gunzip a.gz
[root@localhost home]# ls
a test
查看:zcat(可以查看.gz的被压缩的文本文件里的内容)
[root@localhost home]# zcat a.gz
this a
this a
3.xz命令
xz [选项] 文件名 ->.xz文件
[root@localhost home]# touch b
[root@localhost home]# ls
a.gz b
[root@localhost home]# xz b
[root@localhost home]# ls
a.gz b.xz
解压:uzip
[root@localhost home]# unxz b.xz
[root@localhost home]# ls
a.gz b
查看:xzcat
[root@localhost home]# xzcat b.xz
this bbbb!
4.bzip2命令
bzip2 [选项] 文件名 ->.bz2文件
[root@localhost home]# touch c
[root@localhost home]# ls
b.xz c
[root@localhost home]# bzip2 c
[root@localhost home]# ls
b.xz c.bz2
解压:bunzip2
[root@localhost home]# bunzip2 c.bz2
[root@localhost home]# ls
b.xz c
5.tar命令
可以对文件和文件夹进行压缩
tar [选项] 归档文件名 源文件或目录
-c:创建.tar格式的包文件
-x:解开.tar格式的包文件
-v:输出详细信息
-f:表示使用归档信息
-t:列表查看包里面的内容
-z: gzip
-j:bzip2
-J: xz
(1)打开压缩包
-czf ->.tar.gz
[root@localhost home]# tar -czf passwd_c.tar.gz passwd c
[root@localhost home]# ls
c passwd passwd_c.tar.gz
-cJf ->.tar.xz
[root@localhost home]# tar -cJf passwd_a.tar.xz passwd a
[root@localhost home]# ls passwd_a.tar.xz
passwd_a.tar.xz
-cjf ->.tar.bz2
[root@localhost home]# tar cjf passwd_b.tar.bz2 passwd b
[root@localhost home]# ls
b c passwd passwd_b.tar.bz2 passwd_c.tar.gz
2.查看
-tf
[root@localhost home]# tar -tf passwd_c.tar.gz
passwd
c
3.解压
[root@localhost home]# tar -xf passwd_c.tar.gz
[root@localhost home]# ll
总用量 8
-rw-r--r--. 1 root root 0 12月 5 13:16 c
-rw-r--r--. 1 root root 1374 12月 5 13:16 passwd
-rw-r--r--. 1 root root 714 12月 5 13:16 passwd_c.tar.gz
解压与打包都会替换掉源文件
创建文件的时候指定路径:使用绝对路径
[root@localhost home]# tar czf /backup/boot_pwd.tar.gz /boot /etc/passwd
tar: 从成员名中删除开头的“/”
tar: 从硬连接目标中删除开头的“/”