20180618 Task 6.1 - 6.7
6.18 TASK
6.1 压缩打包介绍
6.2 gzip 压缩工具
6.3 bzip2 压缩工具
6.4 xz 压缩工具
6.5 zip 压缩工具
6.6 tar 打包
6.7 打包并压缩
6.1 压缩打包介绍
Windows == .rar .zip .7z
Linux == .zip,.gz,.bz2,.xz,.tar.gz,.tar.bz2,.tar.xz
- 压缩后的文件, 大小, 传输的带宽, 传输的时间都会减少; 家用联通带宽, 下载是 100M, 但是上传的带宽是 10-20M; 虽然说压缩文件的后缀名各异, 但是有心照不宣的约定, 这些约定是为了让彼此更好的沟通
6.2 gzip 压缩工具
- 压缩工具包括 gzip, bzip2, xz , zip 四种; gzip 可以压缩文件, 但是不能压缩目录 , gzip压缩工具如下:
gzip 1.txt
gzip -d 1.txt.gz / gunzip 1.txt.gz
gzip -# 1.txt //#范围1-9,默认6
不能压缩目录
zcat 1.txt.gz
gzip -c 1.txt > /root/1.txt.gz
gunzip -c /root/1.txt.gz > /tmp/1.txt.new
- 压缩与解压缩
gzip filename , gzip -d filename
[root@arron-01 ~]# cd /tmp/;ls
fstab passwd.sh vmware-root
ks-script-GS2P4L systemd-private-46a0a52f38434d8994f386664836afd2-chronyd.service-UkllDR yum.log
passwd systemd-private-9f4382a001114cb9950dbfe33c348144-chronyd.service-48u71P
[root@arron-01 tmp]# mkdir d6z
## 将找到的文件追加到一个文件中去
[root@arron-01 d6z]# find /etc/ -type f -name "*conf"
/etc/resolv.conf
/etc/libaudit.conf
/etc/depmod.d/dist.conf
/etc/dracut.conf
......
## 多次使用 find 命令去追加文件到 1.txt
[root@arron-01 d6z]# find /etc/ -type f -name "*conf" -exec cat {} >> 1.txt \;
[root@arron-01 d6z]# find /etc/ -type f -name "*conf" -exec cat {} >> 1.txt \;
[root@arron-01 d6z]# du -sh 1.txt
2.2M 1.txt
[root@arron-01 d6z]# ls -lh 1.txt
-rw-r--r--. 1 root root 1.3M 6月 18 18:38 1.txt
[root@arron-01 d6z]# wc -l 1.txt
31968 1.txt
## 压缩文件并查看大小
[root@arron-01 d6z]# gzip 1.txt
[root@arron-01 d6z]# ls
1.txt.gz
[root@arron-01 d6z]# du -sh 1.txt.gz
316K 1.txt.gz
[root@arron-01 d6z]# ls -lh 1.txt.gz
-rw-r--r--. 1 root root 314K 6月 18 18:38 1.txt.gz
## 解压缩 gzip -d /gunzip , 解压缩后发觉 ls -lh 与 du -sh 查看的大小一样
[root@arron-01 d6z]# gzip -d 1.txt.gz
[root@arron-01 d6z]# ls
1.txt
[root@arron-01 d6z]# du -sh 1.txt
1.3M 1.txt
[root@arron-01 d6z]# ls -lh 1.txt
-rw-r--r--. 1 root root 1.3M 6月 18 18:38 1.txt
## 使用 gunzip 解压缩文件
[root@arron-01 d6z]# gunzip 1.txt.gz;ls
1.txt
## -[1-9] 指定压缩级别 [1-9], 默认是 6 级别, 数字越大 , 不一定压缩的越厉害, 但是一般情况下是这样
[root@arron-01 d6z]# gzip -1 1.txt ;ls -lh 1.txt.gz
-rw-r--r--. 1 root root 370K 6月 18 18:38 1.txt.gz
## 可以看到 9 级别压缩的最多, 压缩后的文件也越小
[root@arron-01 d6z]# gzip -9 1.txt;ls -lh 1.txt.gz
-rw-r--r--. 1 root root 312K 6月 18 18:38 1.txt.gz
## file 可以查看一个压缩文件的最大压缩比
[root@arron-01 d6z]# file 1.txt.gz
1.txt.gz: gzip compressed data, was "1.txt", from Unix, last modified: Mon Jun 18 18:38:19 2018, max compression
zcat
查看 *.gz 文件 ; 实际上是先解压, 再 cat
[root@arron-01 d6z]# zcat 1.txt.gz
# RPM packages drop log rotation information into this directory
include /etc/logrotate.d
- 保留源文件且压缩后的文件制定到目标目录下
gzip -c filename > dir path
, 解压缩时也可以保留源文件gzip -d -c filename
## 保留源文件的压缩
[root@arron-01 d6z]# ls ; ls /tmp/1.txt.gz; file /tmp/1.txt.gz
1.txt
/tmp/1.txt.gz
/tmp/1.txt.gz: gzip compressed data, was "1.txt", from Unix, last modified: Mon Jun 18 18:38:19 2018
## 保留源文件的解压缩
[root@arron-01 d6z]# gzip -d -c /tmp/1.txt.gz > /tmp/d6z/2.txt;ls /tmp/d6z/
1.txt 2.txt
## 可以看到解压缩后的文件跟文件一样大小
[root@arron-01 d6z]# du -sh *.txt ; ls -ls *.txt ; wc -l *.txt
1.3M 1.txt
1.3M 2.txt
1252 -rw-r--r--. 1 root root 1280334 6月 18 18:38 1.txt
1252 -rw-r--r--. 1 root root 1280334 6月 18 19:48 2.txt
31968 1.txt
31968 2.txt
63936 总用量
6.3 bzip2 压缩工具
bzip2 支持压缩文件 , 但是不支持压缩目录
- 安装 bzip2 工具 -
yum install -y bzip2
[root@arron-01 ~]# yum install -y bzip2
## 压缩并查看大小
[root@arron-01 d6z]# bzip2 1.txt;ls;du -sh 1.txt.bz2
1.txt.bz2 2.txt
132K 1.txt.bz2
2.解压缩并查看大小, bzip2 -d filename / bunzip2 filename
[root@arron-01 d6z]# bzip2 1.txt;bunzip2 1.txt.bz2;ls -lh
总用量 2.5M
-rw-r--r--. 1 root root 1.3M 6月 18 18:38 1.txt
-rw-r--r--. 1 root root 1.3M 6月 18 19:48 2.txt
- bzip2 -c filename > filename ; 支持保留源文件的压缩到指定路径下, 也支持保留源文件的解压缩到指定路径
## 支持保留源文件的压缩到指定路径下
[root@arron-01 d6z]# bzip2 -c 1.txt > /tmp/1.txt.bz2;du -sh /tmp/1.txt.bz2
132K /tmp/1.txt.bz2
[root@arron-01 d6z]# ls -lh
总用量 2.5M
-rw-r--r--. 1 root root 1.3M 6月 18 18:38 1.txt
-rw-r--r--. 1 root root 1.3M 6月 18 19:48 2.txt
## 支持保留源文件的解压缩到指定路径
[root@arron-01 d6z]# bzip2 -d -c /tmp/1.txt.bz2 > 3.txt ;du -sh . ; ls -l /tmp/*
3.7M .
-rw-r--r--. 1 root root 131420 6月 18 22:46 /tmp/1.txt.bz2
-rw-r--r--. 1 root root 320597 6月 18 19:45 /tmp/1.txt.gz
-rw-r--r--. 1 root root 597 6月 18 10:19 /tmp/fstab
-rwx------. 1 root root 836 6月 15 09:05 /tmp/ks-script-GS2P4L
-rw-r--r--. 1 root root 934 6月 18 10:28 /tmp/passwd
-rw-r--r--. 1 root root 982 6月 18 13:51 /tmp/passwd.sh
-rw-------. 1 root root 0 6月 15 08:48 /tmp/yum.log
/tmp/d6z:
总用量 3756
-rw-r--r--. 1 root root 1280334 6月 18 18:38 1.txt
-rw-r--r--. 1 root root 1280334 6月 18 19:48 2.txt
-rw-r--r--. 1 root root 1280334 6月 18 22:54 3.txt
/tmp/systemd-private-46a0a52f38434d8994f386664836afd2-chronyd.service-UkllDR:
总用量 0
drwxrwxrwt. 2 root root 6 6月 15 17:06 tmp
/tmp/systemd-private-9f4382a001114cb9950dbfe33c348144-chronyd.service-48u71P:
总用量 0
drwxrwxrwt. 2 root root 6 6月 18 09:59 tmp
/tmp/vmware-root:
总用量 0
- bzip2 -[1-9] filename ; 指定级别压缩文件
[root@arron-01 d6z]# bzip2 -9 1.txt ;du -sh 1.txt.bz2 ; file 1.txt.bz2
132K 1.txt.bz2
1.txt.bz2: bzip2 compressed data, block size = 900k
## 不同的压缩级别, 压缩后的文件大小也会不同
[root@arron-01 d6z]# bzip2 -1 1.txt ; ls -lh .
总用量 2.8M
-rw-r--r--. 1 root root 303K 6月 18 18:38 1.txt.bz2
-rw-r--r--. 1 root root 1.3M 6月 18 19:48 2.txt
-rw-r--r--. 1 root root 1.3M 6月 18 22:54 3.txt
- 如果文件特殊, 查看不了, 可以借助于
file
命令
[root@arron-01 d6z]# mv 1.txt.bz2 1.txt; less 1.txt
"1.txt" may be a binary file. See it anyway?
[root@arron-01 d6z]# file 1.txt
1.txt: bzip2 compressed data, block size = 900k
[root@arron-01 d6z]# file 2.txt
2.txt: UTF-8 Unicode text
[root@arron-01 d6z]# less 2.txt
- 查看压缩文件,
bzcat filename
[root@arron-01 d6z]# bzcat 1.txt.bz2
# RPM packages drop log rotation information into this directory
include /etc/logrotate.d
.......
6.4 xz 压缩工具
- xz 压缩工具和 gzip, bzip2 差不多, 在讲到 tar 包时会用到这个命令, 可以看到 xz 压缩的更狠了 , 同时更耗费 CPU 资源
[root@arron-01 d6z]# xz 2.txt;ls -lh . ;
总用量 1.4M
-rw-r--r--. 1 root root 129K 6月 18 18:38 1.txt.bz2
-rw-r--r--. 1 root root 48K 6月 18 19:48 2.txt.xz
-rw-r--r--. 1 root root 1.3M 6月 18 22:54 3.txt
## 查看打包后的文件占用空间大小
[root@arron-01 d6z]# du -sh 2.txt.xz
48K 2.txt.xz
[root@arron-01 d6z]# du -sh 3.txt
1.3M 3.txt
- 支持 1-9 的压缩级别
[root@arron-01 d6z]# xz -1 2.txt ; xz -9 3.txt;ls -lh
总用量 232K
-rw-r--r--. 1 root root 129K 6月 18 18:38 1.txt.bz2
-rw-r--r--. 1 root root 52K 6月 18 19:48 2.txt.xz
-rw-r--r--. 1 root root 48K 6月 18 22:54 3.txt.xz
- 解压缩 unxz filename / xz -d filename
[root@arron-01 d6z]# ls
1.txt.bz2 2.txt.xz 3.txt
[root@arron-01 d6z]# unxz 2.txt.xz; ls -l
总用量 2636
-rw-r--r--. 1 root root 131420 6月 18 18:38 1.txt.bz2
-rw-r--r--. 1 root root 1280334 6月 18 19:48 2.txt
-rw-r--r--. 1 root root 1280334 6月 18 22:54 3.txt
- 将几种压缩做比较, 可以看到压缩严厉度 xz > bzip2 > gzip , 不过不过的文件用不同的压缩工具也不尽是这样
## xz 压缩
[root@arron-01 d6z]# xz -1 2.txt ; xz -9 3.txt;ls -lh
总用量 232K
-rw-r--r--. 1 root root 129K 6月 18 18:38 1.txt.bz2
-rw-r--r--. 1 root root 52K 6月 18 19:48 2.txt.xz
-rw-r--r--. 1 root root 48K 6月 18 22:54 3.txt.xz
## bzip2 压缩
[root@arron-01 d6z]# bzip2 -1 1.txt ; ls -lh .
总用量 2.8M
-rw-r--r--. 1 root root 303K 6月 18 18:38 1.txt.bz2
[root@arron-01 d6z]# bzip2 -9 1.txt ;du -sh 1.txt.bz2 ;
132K 1.txt.bz2
## gip 压缩
[root@arron-01 d6z]# gzip -1 1.txt ;ls -lh 1.txt.gz
-rw-r--r--. 1 root root 370K 6月 18 18:38 1.txt.gz
[root@arron-01 d6z]# gzip -9 1.txt;ls -lh 1.txt.gz
-rw-r--r--. 1 root root 312K 6月 18 18:38 1.txt.gz
- 保留源文件的压缩 ——
xz -c filename > filename
[root@arron-01 d6z]# xz -c 2.txt > /tmp/2.txt.xz;ls -l /tmp/;ls -l .
总用量 512
-rw-r--r--. 1 root root 131420 6月 18 22:46 1.txt.bz2
-rw-r--r--. 1 root root 320597 6月 18 19:45 1.txt.gz
-rw-r--r--. 1 root root 48408 6月 18 23:33 2.txt.xz
drwxr-xr-x. 2 root root 49 6月 18 23:29 d6z
-rw-r--r--. 1 root root 597 6月 18 10:19 fstab
-rwx------. 1 root root 836 6月 15 09:05 ks-script-GS2P4L
-rw-r--r--. 1 root root 934 6月 18 10:28 passwd
-rw-r--r--. 1 root root 982 6月 18 13:51 passwd.sh
drwx------. 3 root root 17 6月 15 17:06 systemd-private-46a0a52f38434d8994f386664836afd2-chronyd.service-UkllDR
drwx------. 3 root root 17 6月 18 09:59 systemd-private-9f4382a001114cb9950dbfe33c348144-chronyd.service-48u71P
drwx------. 2 root root 6 6月 15 09:24 vmware-root
-rw-------. 1 root root 0 6月 15 08:48 yum.log
总用量 2636
-rw-r--r--. 1 root root 131420 6月 18 18:38 1.txt.bz2
-rw-r--r--. 1 root root 1280334 6月 18 19:48 2.txt
-rw-r--r--. 1 root root 1280334 6月 18 22:54 3.txt
- 支持保留源文件的解压缩 ——
xz -d -c filename > filename
[root@arron-01 d6z]# unxz -c /tmp/2.txt.xz > /5.txt;ls -l . ; ls -l /tmp/
总用量 2636
-rw-r--r--. 1 root root 131420 6月 18 18:38 1.txt.bz2
-rw-r--r--. 1 root root 1280334 6月 18 19:48 2.txt
-rw-r--r--. 1 root root 1280334 6月 18 22:54 3.txt
总用量 512
-rw-r--r--. 1 root root 131420 6月 18 22:46 1.txt.bz2
-rw-r--r--. 1 root root 320597 6月 18 19:45 1.txt.gz
-rw-r--r--. 1 root root 48408 6月 18 23:33 2.txt.xz
drwxr-xr-x. 2 root root 49 6月 18 23:29 d6z
-rw-r--r--. 1 root root 597 6月 18 10:19 fstab
-rwx------. 1 root root 836 6月 15 09:05 ks-script-GS2P4L
-rw-r--r--. 1 root root 934 6月 18 10:28 passwd
-rw-r--r--. 1 root root 982 6月 18 13:51 passwd.sh
drwx------. 3 root root 17 6月 15 17:06 systemd-private-46a0a52f38434d8994f386664836afd2-chronyd.service-UkllDR
drwx------. 3 root root 17 6月 18 09:59 systemd-private-9f4382a001114cb9950dbfe33c348144-chronyd.service-48u71P
drwx------. 2 root root 6 6月 15 09:24 vmware-root
-rw-------. 1 root root 0 6月 15 08:48 yum.log
- 查看压缩文件 ——
xzcat filename
[root@arron-01 d6z]# xzcat /tmp/2.txt.xz
6.5 zip 压缩工具
跟 gzip, bzip2, xz 不同, zip 支持压缩目录
zip 1.txt.zip 1.txt
zip -r 123.zip 123/
unzip 1.txt.zip
unzip 123.zip -d /root/456/
unzip -l 123.zip
Linux 默认不解压 Windows 下的 RAR 文件
- 压缩工具 zip (yum install -y zip)
trait : a. 可以压缩目录, b. 不要加选项 -c 就能保留源文件(跟 gzip, bzip2, xz 不同)
[root@arron-01 d6z]# tree /tmp/arronlinux
/tmp/arronlinux
├── 2
│ └── 2.txt
└── arron2
├── 2
│ └── 2.txt
├── arron1
│ ├── 2
│ │ └── 2.txt
│ └── arronlinux
│ └── 2
│ └── 2.txt
└── ls2
[root@arron-01 d6z]# cp -r /tmp/arronlinux .
[root@arron-01 d6z]# ls
1.txt.bz2 2.txt 3.txt arronlinux
[root@arron-01 d6z]# cp 3.txt arronlinux/2/
[root@arron-01 d6z]# tree arronlinux/
arronlinux/
├── 2
│ ├── 2.txt
│ └── 3.txt
└── arron2
├── 2
│ └── 2.txt
├── arron1
│ ├── 2
│ │ └── 2.txt
│ └── arronlinux
│ └── 2
│ └── 2.txt
└── ls2
8 directories, 5 files
## 这样 arronlinux 看起来会有点大, 达到了 1.3M
[root@arron-01 d6z]# du -sh arronlinux
1.3M arronlinux
## zip 压缩文件
[root@arron-01 d6z]# zip 2.txt.zip 2.txt;ls;du -sh 2.txt.zip
adding: 2.txt (deflated 75%)
1.txt.bz2 2.txt 2.txt.zip 3.txt arronlinux
316K 2.txt.zip
## zip 压缩目录, -r 可以查看每个文件/目录的压缩过程
[root@arron-01 d6z]# zip -r arron.zip 3.txt arronlinux;ls . ; du -sh arron.zip
adding: 3.txt (deflated 75%)
adding: arronlinux/ (stored 0%)
adding: arronlinux/2/ (stored 0%)
adding: arronlinux/2/2.txt (stored 0%)
adding: arronlinux/2/3.txt (deflated 75%)
adding: arronlinux/arron2/ (stored 0%)
adding: arronlinux/arron2/2/ (stored 0%)
adding: arronlinux/arron2/2/2.txt (stored 0%)
adding: arronlinux/arron2/arron1/ (stored 0%)
adding: arronlinux/arron2/arron1/2/ (stored 0%)
adding: arronlinux/arron2/arron1/2/2.txt (stored 0%)
adding: arronlinux/arron2/arron1/arronlinux/ (stored 0%)
adding: arronlinux/arron2/arron1/arronlinux/2/ (stored 0%)
adding: arronlinux/arron2/arron1/arronlinux/2/2.txt (stored 0%)
adding: arronlinux/arron2/ls2/ (stored 0%)
1.txt.bz2 2.txt 2.txt.zip 3.txt arronlinux arron.zip
632K arron.zip
## 且 加 -r 选项后压缩的大小明显是不加 -r 的两倍
[root@arron-01 d6z]# zip arron1.zip 3.txt arronlinux;ls . ; du -sh arron1.zip
adding: 3.txt (deflated 75%)
adding: arronlinux/ (stored 0%)
1.txt.bz2 2.txt 2.txt.zip 3.txt arron1.zip arronlinux arron.zip
316K arron1.zip
-
解压缩 unzip (yum install -y unzip)
a. -d 选项 指定要某个路径下 b. 压缩前的文件名与解压缩后的文件名一致 c. 会有是否替换的提示
[root@arron-01 d6z]# mkdir test;unzip 2.txt.zip -d test/;tree .
Archive: 2.txt.zip
inflating: test/2.txt
.
├── 1.txt.bz2
├── 2.txt
├── 2.txt.zip
├── 3.txt
├── arron1.zip
├── arronlinux
│ ├── 2
│ │ ├── 2.txt
│ │ └── 3.txt
│ └── arron2
│ ├── 2
│ │ └── 2.txt
│ ├── arron1
│ │ ├── 2
│ │ │ └── 2.txt
│ │ └── arronlinux
│ │ └── 2
│ │ └── 2.txt
│ └── ls2
├── arron.zip
└── test
└── 2.txt
10 directories, 12 files
## 可以看出又生成了一个更名的目录, 下边是压缩前的文件
[root@arron-01 d6z]# tree test/
test/
├── 2.txt
└── 619.txt
└── 2.txt
1 directory, 2 files
[root@arron-01 d6z]# file test/619.txt
test/619.txt: directory
[root@arron-01 d6z]# ls -l test
总用量 1252
-rw-r--r--. 1 root root 1280334 6月 18 19:48 2.txt
drwxr-xr-x. 2 root root 19 6月 19 15:24 619.txt
[root@arron-01 d6z]# unzip arron1.zip
Archive: arron1.zip
replace 3.txt? [y]es, [n]o, [A]ll, [N]one, [r]ename: n
- 不可以查看文件, 只能查看文件列表,
unzip -l filename
[root@arron-01 d6z]# unzip -l arron.zip
Archive: arron.zip
Length Date Time Name
--------- ---------- ----- ----
1280334 06-18-2018 22:54 3.txt
0 06-19-2018 11:33 arronlinux/
0 06-19-2018 11:34 arronlinux/2/
0 06-19-2018 11:33 arronlinux/2/2.txt
1280334 06-19-2018 11:34 arronlinux/2/3.txt
0 06-19-2018 11:33 arronlinux/arron2/
0 06-19-2018 11:33 arronlinux/arron2/2/
0 06-19-2018 11:33 arronlinux/arron2/2/2.txt
0 06-19-2018 11:33 arronlinux/arron2/arron1/
0 06-19-2018 11:33 arronlinux/arron2/arron1/2/
0 06-19-2018 11:33 arronlinux/arron2/arron1/2/2.txt
0 06-19-2018 11:33 arronlinux/arron2/arron1/arronlinux/
0 06-19-2018 11:33 arronlinux/arron2/arron1/arronlinux/2/
0 06-19-2018 11:33 arronlinux/arron2/arron1/arronlinux/2/2.txt
0 06-19-2018 11:33 arronlinux/arron2/ls2/
--------- -------
2560668 15 files
6.6 tar 打包
tar -cvf 123.tar 123
tar -cvf aming.tar 1.txt 123
tar -xvf aming.tar
tar -tf aming.tar
tar -cvf aming.tar --exclude 1.txt --exclude 2 123
我们谈的带宽单位实际上是 bit, 不是 byte, 1 byte=8 bit
- tar 类似于 zip 用法
tar -cvf filename.tar file/dir
;v
可加可不加 , 不管是打包还是解包, 只要名称一样, 就不会过问直接覆盖之前的文件
[root@arron-01 d6z]# tar -cvf arronlinux.tar arronlinux/
arronlinux/
arronlinux/2/
arronlinux/2/2.txt
arronlinux/2/3.txt
arronlinux/arron2/
arronlinux/arron2/2/
arronlinux/arron2/2/2.txt
arronlinux/arron2/arron1/
arronlinux/arron2/arron1/2/
arronlinux/arron2/arron1/2/2.txt
arronlinux/arron2/arron1/arronlinux/
arronlinux/arron2/arron1/arronlinux/2/
arronlinux/arron2/arron1/arronlinux/2/2.txt
arronlinux/arron2/ls2/
[root@arron-01 d6z]# ls
1.txt.bz2 2.txt 2.txt.zip 3.txt arron1.zip arronlinux arronlinux.tar arron.zip test
## 可以文件和目录一起打包
[root@arron-01 d6z]# tar -cvf arronlinux.tar arronlinux 3.txt 2.txt;ls .
arronlinux/
arronlinux/2/
arronlinux/2/2.txt
arronlinux/2/3.txt
arronlinux/arron2/
arronlinux/arron2/2/
arronlinux/arron2/2/2.txt
arronlinux/arron2/arron1/
arronlinux/arron2/arron1/2/
arronlinux/arron2/arron1/2/2.txt
arronlinux/arron2/arron1/arronlinux/
arronlinux/arron2/arron1/arronlinux/2/
arronlinux/arron2/arron1/arronlinux/2/2.txt
arronlinux/arron2/ls2/
3.txt
2.txt
1.txt.bz2 2.txt 2.txt.zip 3.txt arron1.zip arronlinux arronlinux.tar arron.zip test
- 解包
tar xvf filename.tar file/dir
, 解包时会覆盖以前的文件或目录, 不会如 unzip 那般提示.
[root@arron-01 d6z]# tar -xvf arronlinux.tar
arronlinux/
arronlinux/2/
arronlinux/2/2.txt
arronlinux/2/3.txt
arronlinux/arron2/
arronlinux/arron2/2/
arronlinux/arron2/2/2.txt
arronlinux/arron2/arron1/
arronlinux/arron2/arron1/2/
arronlinux/arron2/arron1/2/2.txt
arronlinux/arron2/arron1/arronlinux/
arronlinux/arron2/arron1/arronlinux/2/
arronlinux/arron2/arron1/arronlinux/2/2.txt
arronlinux/arron2/ls2/
[root@arron-01 d6z]# ls
1.txt.bz2 2.txt 2.txt.zip 3.txt arron1.zip arronlinux arronlinux.tar arron.zip test
- 查看 tar 包,
tar -tf filename.tar
[root@arron-01 d6z]# tar -tf arronlinux.tar
arronlinux/
arronlinux/2/
arronlinux/2/2.txt
arronlinux/2/3.txt
arronlinux/arron2/
arronlinux/arron2/2/
arronlinux/arron2/2/2.txt
arronlinux/arron2/arron1/
arronlinux/arron2/arron1/2/
arronlinux/arron2/arron1/2/2.txt
arronlinux/arron2/arron1/arronlinux/
arronlinux/arron2/arron1/arronlinux/2/
arronlinux/arron2/arron1/arronlinux/2/2.txt
arronlinux/arron2/ls2/
3.txt
2.txt
- --exclude file 建立打包文件时会过滤文件/目录而不去打包,
tar -cvf filename.tar --exclude file
, --exclude 后边只能接一个文件或者目录, 不能接多个
[root@arron-01 d6z]# tar -cvf arronlinux.tar arronlinux --exclude 2.txt --exclude arron1
arronlinux/
arronlinux/2/
arronlinux/2/3.txt
arronlinux/arron2/
arronlinux/arron2/2/
arronlinux/arron2/ls2/
## 不管它是单独的或是某个父目录下的子文件/目录, 过滤这个文件或目录时都会排除在外
[root@arron-01 d6z]# tar -cvf arronlinux.tar arronlinux 2.txt 3.txt --exclude arron1 --exclude 2.txt;ls .; tar -tf arronlinux.tar
arronlinux/
arronlinux/2/
arronlinux/2/3.txt
arronlinux/arron2/
arronlinux/arron2/2/
arronlinux/arron2/ls2/
3.txt
1.txt.bz2 2.txt 2.txt.zip 3.txt arron1.zip arronlinux arronlinux.tar arron.zip test
arronlinux/
arronlinux/2/
arronlinux/2/3.txt
arronlinux/arron2/
arronlinux/arron2/2/
arronlinux/arron2/ls2/
3.txt
## arronlinux 目录中的文件/m目录如下
[root@arron-01 d6z]# tree arronlinux/
arronlinux/
├── 2
│ ├── 2.txt
│ └── 3.txt
└── arron2
├── 2
│ └── 2.txt
├── arron1
│ ├── 2
│ │ └── 2.txt
│ └── arronlinux
│ └── 2
│ └── 2.txt
└── ls2
8 directories, 5 files
## 支持通配符, 但是有通配符的文件要加 ""
[root@arron-01 d6z]# tar -cvf arronlinux.tar arronlinux 1.txt.bz2 2.txt.zip --exclude "*.txt" --exclude arron1; tar -tf arronlinux.tar
arronlinux/
arronlinux/2/
arronlinux/arron2/
arronlinux/arron2/2/
arronlinux/arron2/ls2/
1.txt.bz2
2.txt.zip
arronlinux/
arronlinux/2/
arronlinux/arron2/
arronlinux/arron2/2/
arronlinux/arron2/ls2/
1.txt.bz2
2.txt.zip
6.7 打包并压缩
tar -zcvf 123.tar.gz 123
tar -zxvf 123.tar.gz
tar -jcvf 123.bz2 123
tar -jxvf 123.bz2
tar -Jcvf 123.xz 123
tar -Jxvf 123.xz
tar -tf 123.bz2 / tar -tf 123.gz / tar -tf 123.xz
- 从如下的实例可看出,
压缩能力 tar.gz < tar.bz2 < tar.xz
## 分别打包并压缩为 tar.gz , tar.bz2 , tar.xz
tar -czvf filename.tar.gz
tar -cjvf filename.tar.bz2
tar -cJvf filename.tar.xz
### 打包并压缩为 tar.gz
[root@arron-01 d6z]# tar czvf arronlinux.tar.gz arronlinux 2.txt 3.txt ;du -sh 2.txt 3.txt arronlinux ; du -sh arronlinux.tar.gz
arronlinux/
arronlinux/2/
arronlinux/2/2.txt
arronlinux/2/3.txt
arronlinux/arron2/
arronlinux/arron2/2/
arronlinux/arron2/2/2.txt
arronlinux/arron2/arron1/
arronlinux/arron2/arron1/2/
arronlinux/arron2/arron1/2/2.txt
arronlinux/arron2/arron1/arronlinux/
arronlinux/arron2/arron1/arronlinux/2/
arronlinux/arron2/arron1/arronlinux/2/2.txt
arronlinux/arron2/ls2/
2.txt
3.txt
1.3M 2.txt
1.3M 3.txt
1.3M arronlinux
940K arronlinux.tar.gz
### 打包并压缩为 tar.bz2
[root@arron-01 d6z]# tar -cvjf arronlinux.tar.bz2 arronlinux 2.txt 3.txt ; du -sh arronlinux 2.txt 3.txt ;du -sh arronlinux.tar.bz2
arronlinux/
arronlinux/2/
arronlinux/2/2.txt
arronlinux/2/3.txt
arronlinux/arron2/
arronlinux/arron2/2/
arronlinux/arron2/2/2.txt
arronlinux/arron2/arron1/
arronlinux/arron2/arron1/2/
arronlinux/arron2/arron1/2/2.txt
arronlinux/arron2/arron1/arronlinux/
arronlinux/arron2/arron1/arronlinux/2/
arronlinux/arron2/arron1/arronlinux/2/2.txt
arronlinux/arron2/ls2/
2.txt
3.txt
1.3M arronlinux
1.3M 2.txt
1.3M 3.txt
320K arronlinux.tar.bz2
### 打包并压缩为 tar.xz
[root@arron-01 d6z]# tar -cvJf arronlinux.tar.xz arronlinux 2.txt 3.txt ; du -sh arronlinux 2.txt 3.txt ;du -sh arronlinux.tar.xz
arronlinux/
arronlinux/2/
arronlinux/2/2.txt
arronlinux/2/3.txt
arronlinux/arron2/
arronlinux/arron2/2/
arronlinux/arron2/2/2.txt
arronlinux/arron2/arron1/
arronlinux/arron2/arron1/2/
arronlinux/arron2/arron1/2/2.txt
arronlinux/arron2/arron1/arronlinux/
arronlinux/arron2/arron1/arronlinux/2/
arronlinux/arron2/arron1/arronlinux/2/2.txt
arronlinux/arron2/ls2/
2.txt
3.txt
1.3M arronlinux
1.3M 2.txt
1.3M 3.txt
48K arronlinux.tar.xz
- 查看
*.tar
文件列表 tar -tf filename , 和不压缩(但是要打包)前是一样的
[root@arron-01 d6z]# tar -tf arronlinux.tar.gz;tar -tf arronlinux.tar.bz2;tar -tf arronlinux.tar.xz
arronlinux/
arronlinux/2/
arronlinux/2/2.txt
arronlinux/2/3.txt
arronlinux/arron2/
arronlinux/arron2/2/
arronlinux/arron2/2/2.txt
arronlinux/arron2/arron1/
arronlinux/arron2/arron1/2/
arronlinux/arron2/arron1/2/2.txt
arronlinux/arron2/arron1/arronlinux/
arronlinux/arron2/arron1/arronlinux/2/
arronlinux/arron2/arron1/arronlinux/2/2.txt
arronlinux/arron2/ls2/
2.txt
3.txt
arronlinux/
arronlinux/2/
arronlinux/2/2.txt
arronlinux/2/3.txt
arronlinux/arron2/
arronlinux/arron2/2/
arronlinux/arron2/2/2.txt
arronlinux/arron2/arron1/
arronlinux/arron2/arron1/2/
arronlinux/arron2/arron1/2/2.txt
arronlinux/arron2/arron1/arronlinux/
arronlinux/arron2/arron1/arronlinux/2/
arronlinux/arron2/arron1/arronlinux/2/2.txt
arronlinux/arron2/ls2/
2.txt
3.txt
arronlinux/
arronlinux/2/
arronlinux/2/2.txt
arronlinux/2/3.txt
arronlinux/arron2/
arronlinux/arron2/2/
arronlinux/arron2/2/2.txt
arronlinux/arron2/arron1/
arronlinux/arron2/arron1/2/
arronlinux/arron2/arron1/2/2.txt
arronlinux/arron2/arron1/arronlinux/
arronlinux/arron2/arron1/arronlinux/2/
arronlinux/arron2/arron1/arronlinux/2/2.txt
arronlinux/arron2/ls2/
2.txt
3.txt