1. 文件归档
文件归档: 把文件通过特定形式保存/存储,或者进行传递、数据备份等。
$# 文件压缩包
文件打包: 将多个文件存储在一个文件里面 (方便管理文件)
文件压缩: 将文件通过压缩算法,压缩为一个小文件(可以解压缩-数据还原) 占用空间小
1. 文件压缩格式: zip/7z | bzip2/gz
文件打包压缩:
2. 文件打包带压缩格式: Windows: zip/7z | Linux: tar.bz2/tar.gz
Windows: 文件打包、文件压缩可以一步到位
Linux: 文件打包、文件压缩可以分别管理
1.1 文件压缩/解压缩
在Linux中,常见文件压缩格式 bz2/gz
在Linux中,常见压缩工具bzip2/gzip
$# 1.
bzip2 压缩文件
bunzip2 解除文件压缩
[zy@localhost Desktop]$ mkdir zy
[zy@localhost Desktop]$ cd zy
[zy@localhost zy]$ touch zy.txt
[zy@localhost zy]$ ls
zy.txt
[zy@localhost zy]$ bzip2 zy.txt
[zy@localhost zy]$ ls
zy.txt.bz2
[zy@localhost zy]$ bunzip2 zy.txt.bz2
[zy@localhost zy]$ ls
zy.txt
[zy@localhost zy]$
$# 2.
gzip 压缩文件
gunzip 解除文件压缩
[zy@localhost zy]$ gzip zy.txt
[zy@localhost zy]$ ls
zy.txt.gz
[zy@localhost zy]$ gunzip zy.txt.gz
[zy@localhost zy]$ ls
zy.txt
[zy@localhost zy]$
补充: dd ---- 文件创建 | 数据传输
if = input file
of = output file
bs = block size 块大小
count = 数量
[zy@localhost zy]$ dd if=/dev/zero of=zyzip bs=1M count=100
100+0 records in
100+0 records out
104857600 bytes (105 MB) copied, 0.210287 s, 499 MB/s
[zy@localhost zy]$ ls -lh
total 100M
-rw-rw-r--. 1 zy zy 0 Jan 30 23:15 zy.txt
-rw-rw-r--. 1 zy zy 100M Jan 30 23:25 zyzip
[zy@localhost zy]$
[zy@localhost zy]$ ls
zy.txt zyzip
[zy@localhost zy]$ bzip2 zyzip
[zy@localhost zy]$ ls -lh
total 4.0K
-rw-rw-r--. 1 zy zy 0 Jan 30 23:15 zy.txt
-rw-rw-r--. 1 zy zy 113 Jan 30 23:25 zyzip.bz2
[zy@localhost zy]$ gzip zyzip
[zy@localhost zy]$ ls -lh
total 100K
-rw-rw-r--. 1 zy zy 0 Jan 30 23:15 zy.txt
-rw-rw-r--. 1 zy zy 100K Jan 30 23:25 zyzip.gz
[zy@localhost zy]$
[zy@localhost zy]$ ls
zy.txt zyzip
[zy@localhost zy]$ bzip2 zy.txt
[zy@localhost zy]$ ls
zy.txt.bz2 zyzip
[zy@localhost zy]$ bzip2 -d zy.txt.bz2
[zy@localhost zy]$ ls
zy.txt zyzip
[zy@localhost zy]$ gzip zy.txt
[zy@localhost zy]$ ls
zy.txt.gz zyzip
[zy@localhost zy]$ gzip -d zy.txt.gz
[zy@localhost zy]$ ls
zy.txt zyzip
[zy@localhost zy]$
[zy@localhost zy]$ ls
zy.txt zyzip
[zy@localhost zy]$ bzip2 -k zy.txt
[zy@localhost zy]$ ls
zy.txt zy.txt.bz2 zyzip
[zy@localhost zy]$ rm -rf zy.txt
[zy@localhost zy]$ ls
zy.txt.bz2 zyzip
[zy@localhost zy]$ bzip2 -k -d zy.txt.bz2
[zy@localhost zy]$ ls
zy.txt zy.txt.bz2 zyzip
[zy@localhost zy]$
[zy@localhost zy]$ ls
zy.txt zyzip
[zy@localhost zy]$ gzip -c zy.txt ࠗczy.txt[zy@localhost zy]$
[zy@localhost zy]$ ls
zy.txt zyzip
[zy@localhost zy]$ gzip -c zy.txt > zy.txt.gz
[zy@localhost zy]$ ls
zy.txt zy.txt.gz zyzip
[zy@localhost zy]$
[zy@localhost zy]$ ls
zy.txt zy.txt.gz zyzip
[zy@localhost zy]$ rm -rf zy.txt
[zy@localhost zy]$ ls
zy.txt.gz zyzip
[zy@localhost zy]$ gzip -c -d zy.txt.gz > zy.txt
[zy@localhost zy]$ ls
zy.txt zy.txt.gz zyzip
[zy@localhost zy]$
1.2 文件打包
$# 1. tar
[zy@localhost Desktop]$ mkdir zy
[zy@localhost Desktop]$ cd zy
[zy@localhost zy]$ touch zy{1..3}.txt
[zy@localhost zy]$ ls
zy1.txt zy2.txt zy3.txt
[zy@localhost zy]$ bzip2 -k zy1.txt zy2.txt zy3.txt
[zy@localhost zy]$ ls
zy1.txt zy1.txt.bz2 zy2.txt zy2.txt.bz2 zy3.txt zy3.txt.bz2
[zy@localhost zy]$
[zy@localhost zy]$ rm -rf *.bz2
[zy@localhost zy]$ ls
zy1.txt zy2.txt zy3.txt
[zy@localhost zy]$ bzip2 -k zy{1..3}.txt
[zy@localhost zy]$ ls
zy1.txt zy1.txt.bz2 zy2.txt zy2.txt.bz2 zy3.txt zy3.txt.bz2
[zy@localhost zy]$
[zy@localhost Desktop]$ bzip2 zy
bzip2: Input file zy is a directory.
[zy@localhost Desktop]$ gzip zy
gzip: zy is a directory -- ignored
[zy@localhost Desktop]$
$# 2. tar打包
tar [参数] [目标文件命名] [源文件/源目录名称]
-c 打包文件
-x 解包文件
-v 显示打包或解包的过程
-f 目标文件名
-C 指定解压到的目录
[zy@localhost zy]$ ls
zy1.txt zy2.txt zy3.txt
[zy@localhost zy]$ tar cvf zy.tar zy{1..3}.txt
zy1.txt
zy2.txt
zy3.txt
[zy@localhost zy]$ ls
zy1.txt zy2.txt zy3.txt zy.tar
[zy@localhost zy]$
[zy@localhost zy]$ tar -cvf zy.tar zy{1..3}.txt
zy1.txt
zy2.txt
zy3.txt
[zy@localhost zy]$ ls
zy1.txt zy2.txt zy3.txt zy.tar
[zy@localhost zy]$
[zy@localhost zy]$ rm -rf *.txt
[zy@localhost zy]$ ls
zy.tar
[zy@localhost zy]$ tar -xvf zy.tar
zy1.txt
zy2.txt
zy3.txt
[zy@localhost zy]$ ls
zy1.txt zy2.txt zy3.txt zy.tar
[zy@localhost zy]$
[zy@localhost zy]$ ls ./../
zy zy1'
[zy@localhost zy]$ ls ./../zy1
[zy@localhost zy]$
[zy@localhost zy]$ ls
zy1.txt zy2.txt zy3.txt zy.tar
[zy@localhost zy]$
[zy@localhost zy]$ tar -xvf zy.tar -C ./../zy1 # 解包到zy1路径
zy1.txt
zy2.txt
zy3.txt
[zy@localhost zy]$ ls ./../zy1
zy1.txt zy2.txt zy3.txt
[zy@localhost zy]$
# 打包到指定路径(不需要加-C)
[zy@localhost zy]$ tar -cvf ./../zy1/zy1.tar zy{1..3}.txt
zy1.txt
zy2.txt
zy3.txt
[zy@localhost zy]$ ls ./../zy1/
zy1.tar
[zy@localhost zy]$
$# # 打包文件目录的时候,建议到文件目录的上一级目录进行打包
[zy@localhost zy]$ ls
zy1.txt zy2.txt zy3.txt
[zy@localhost zy]$ tar -cvf zy.tar ./../zy
tar: Removing leading `./../' from member names
./../zy/
./../zy/zy1.txt
./../zy/zy2.txt
./../zy/zy3.txt
tar: ./../zy/zy.tar: file is the archive; not dumped
[zy@localhost zy]$
[zy@localhost zy]$ ls
zy1.txt zy2.txt zy3.txt (zy.tar)
[zy@localhost zy]$
[zy@localhost Desktop]$ ls
zy zy1
[zy@localhost Desktop]$ tar -cvf zy.tar zy
zy/
zy/zy.tar
zy/zy1.txt
zy/zy2.txt
zy/zy3.txt
[zy@localhost Desktop]$ ls
zy zy1 zy.tar
[zy@localhost Desktop]$
[zy@localhost Desktop]$ rm -rf zy
[zy@localhost Desktop]$ ls
zy1 zy.tar
[zy@localhost Desktop]$ tar -xvf zy.tar
zy/
zy/zy.tar
zy/zy1.txt
zy/zy2.txt
zy/zy3.txt
[zy@localhost Desktop]$ ls
zy zy1 zy.tar
[zy@localhost Desktop]$
1.3 文件打包+压缩
$# 1. 先打包,再压缩
[zy@localhost zy]$ ls
zy1.txt zy2.txt zy3.txt
[zy@localhost zy]$ tar -cvf zy.tar zy{1..3}.txt
zy1.txt
zy2.txt
zy3.txt
[zy@localhost zy]$ ls
zy1.txt zy2.txt zy3.txt zy.tar
[zy@localhost zy]$ bzip2 -k zy.tar
[zy@localhost zy]$ ls
zy1.txt zy2.txt zy3.txt zy.tar zy.tar.bz2
[zy@localhost zy]$
[zy@localhost zy]$ ls
zy1.txt zy2.txt zy3.txt zy.tar
[zy@localhost zy]$ gzip -c zy.tar > zy.tar.gz
[zy@localhost zy]$ ls
zy1.txt zy2.txt zy3.txt zy.tar zy.tar.gz
[zy@localhost zy]$
$# 2. 打包的同时压缩
-z 用gzip压缩或解压
-j 用bzip2压缩或解压
[zy@localhost zy]$ ls
zy1.txt zy2.txt zy3.txt
[zy@localhost zy]$ tar -jcvf zy.tar.bz2 zy{1..3}.txt
zy1.txt
zy2.txt
zy3.txt
[zy@localhost zy]$ ls
zy1.txt zy2.txt zy3.txt zy.tar.bz2
[zy@localhost zy]$
[zy@localhost zy]$ ls
zy.tar.bz2
[zy@localhost zy]$ tar -jxvf zy.tar.bz2
zy1.txt
zy2.txt
zy3.txt
[zy@localhost zy]$ ls
zy1.txt zy2.txt zy3.txt zy.tar.bz2
[zy@localhost zy]$
[zy@localhost zy]$ ls
zy1.txt zy2.txt zy3.txt
[zy@localhost zy]$ tar -zcvf zy.tar.gz zy{1..3}.txt
zy1.txt
zy2.txt
zy3.txt
[zy@localhost zy]$ ls
zy1.txt zy2.txt zy3.txt zy.tar.gz
[zy@localhost zy]$
[zy@localhost zy]$ ls
zy.tar.gz
[zy@localhost zy]$ tar -zxvf zy.tar.gz
zy1.txt
zy2.txt
zy3.txt
[zy@localhost zy]$ ls
zy1.txt zy2.txt zy3.txt zy.tar.gz
[zy@localhost zy]$
[zy@localhost zy]$ tar -zxvf zy.tar.gz -C ./../zy1
zy1.txt
zy2.txt
zy3.txt
[zy@localhost zy]$ ls ./../zy1/
zy1.tar zy1.txt zy2.txt zy3.txt
[zy@localhost zy]$