Linux-文件归档

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]$ 

# 在Linux中 bzip2压缩效率比gzip压缩效率高一些
[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]$ 

# -d(也可以解压)
[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]$ 

# -k(压缩/解压过程中保留原文件)
[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]$ 

# gzip 需要使用-c 和 > 
[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
## bzip2/gzip 无法压缩多个文件,无法压缩文件目录,该种情况需要先将多个文件/目录进行tar打包,打包成一个文件之后再压缩。

# bzip2/gzip 无法压缩多个文件
[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]$ 

# bzip2/gzip无法压缩文件目录
[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 指定解压到的目录
# -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]$ 

# 解包文件(-x)
[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]$ 

# -C(解包指定到目标路径)
[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.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压缩或解压
# 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]$ 

# bzip2解包带解压缩
[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]$ 
#*************************************************************************************

# gzip打包带压缩
[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]$ 

# gzip解包带解压缩
[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]$ 
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值