Linux文件管理-压缩和解压

Linux文件管理-压缩和解压

压缩的好处

1.节省磁盘空间占用率
2.节省网络传输带宽消耗Linux系统常见的压缩包有哪些类型

格式压缩工具
.zipzip命令压缩工具
.gzgzip压缩工具,只能压缩文件,会删除源文件(通常配合tar使用)
.bz2bzip2压缩工具,只能压缩文件,会删除源文件(通常配合tar使用)
.tar.gz先使用tar命令归档打包,然后使用gzip压缩
.tar.bz2先使用tar命令归档打包,然后使用bzip压缩

注意:
1.Linux下常用压缩文件以.tar.gz结尾.
2.Linux下压缩文件必须带后缀.

gzip命令

yum install -y gzip

## 语法
gzip	选项	源文件名...
gzip [option] [file...]
## 选项
-r:递归处理,将指定目录下的所有文件及子目录下的所有文件全部单独压缩,只压缩文件不压缩目录。
-d: decompress 解压缩
# 1.gzip压缩之后,源文件被删除
# 2.gzip压缩文件,每个文件都是单独的压缩包
[root@oldboy /tmp]# gzip file1 file2 file3
[root@oldboy /tmp]# ll
-rw-r--r--. 1 root root 26 Jun 21 10:42 file1.gz
-rw-r--r--. 1 root root 26 Jun 21 10:42 file2.gz
-rw-r--r--. 1 root root 26 Jun 21 10:42 file3.gz

# 3.gzip默认只能压缩文件(不能压缩目录)
[root@oldboy /tmp]# ll
total 0
drwxr-xr-x. 3 root root 57 Jun 21 15:03 dirs
-rw-r--r--. 1 root root  0 Jun 21 10:42 file1

[root@oldboy /tmp]# ll /tmp/dirs
total 0
-rw-r--r--. 1 root root  0 Jun 21 10:42 file1
-rw-r--r--. 1 root root  0 Jun 21 10:42 file2
-rw-r--r--. 1 root root  0 Jun 21 10:42 file3
drwxr-xr-x. 2 root root 54 Jun 21 14:58 test

[root@oldboy /tmp]# gzip -r /tmp/dirs
[root@oldboy /tmp]# ll /tmp/dirs
total 12
-rw-r--r--. 1 root root 26 Jun 21 10:42 file1.gz
-rw-r--r--. 1 root root 26 Jun 21 10:42 file2.gz
-rw-r--r--. 1 root root 26 Jun 21 10:42 file3.gz
drwxr-xr-x. 2 root root 54 Jun 21 14:58 test

# 4.如何查看gzip包中文件的内容:zcat
[root@oldboy /tmp]# zcat /tmp/dirs/file1.gz 
hello world

# 5.gzip解压缩之后,源文件出来,压缩包被删除

zip命令

yum install -y zip unzip

# zip 压缩
语法:zip [option] [压缩包名] [源文件名...]
zip [option] [压缩包名] [源文件名...]
[root@oldboy /tmp]# zip file1.zip file1
  adding: file1 (stored 0%)
  
# unzip 解压缩
unzip [压缩包名] -d [指定目录]
[root@oldboy /tmp]# unzip dirs.zip -d /opt
Archive:  dirs.zip
   creating: /opt/tmp/dirs/
   
-r:递归处理,把目录下的所有文件和子目录及子目录中的文件都压缩进去 
-m 压缩完毕后,删除源文件
# 1.zip压缩后,源文件还在
[root@oldboy /tmp]# zip file1.zip file1
  adding: file1 (stored 0%)
[root@oldboy /tmp]# ll
-rw-r--r--. 1 root root   0 Jun 21 10:42 file1
-rw-r--r--. 1 root root 160 Jun 21 15:51 file1.zip

# 2.zip可以指定多个文件,压缩到一个包里
[root@oldboy /tmp]# zip -m ld123.zip ld1 ld2 ld3
updating: ld1 (stored 0%)
updating: ld2 (stored 0%)
updating: ld3 (stored 0%)

# 3.zip可以压缩目录,但是需要加选项-r,如果不加只是打包一个空目录
[root@oldboy /tmp]# zip dirs.zip dirs
  adding: dirs/ (stored 0%)
  
[root@oldboy /tmp]# unzip dirs.zip -d /opt/
Archive:  dirs.zip
   creating: /opt/dirs/

[root@oldboy /tmp]# ll /opt/dirs/
total 0

[root@oldboy /tmp]# zip -r dirs.zip dirs
  adding: dirs/ (stored 0%)
  adding: dirs/test/ (stored 0%)
  adding: dirs/test/test1.gz (deflated 19%)
  adding: dirs/test/test2.gz (deflated 19%)
  adding: dirs/test/test3.gz (deflated 19%)
  adding: dirs/test/ld123.zip (stored 0%)
  adding: dirs/ld123.zip (stored 0%)
# 4.zip如何解压 unzip 默认解压到当前目录
# 5.unzip可以指定解压位置 -d 指定目录	unzip [压缩包名] -d [指定目录]
[root@oldboy /opt]# unzip file.zip -d /tmp/dirs/
# 6.在.zip格式压缩包的状态下查看压缩包里的内容列表
[root@oldboy /tmp]# zip -sf dirs.zip 
Archive contains:
  dirs/
  dirs/test/
  dirs/test/test1.gz
  dirs/test/test2.gz
  dirs/test/test3.gz
  dirs/test/ld123.zip
  dirs/ld123.zip
Total 7 entries (926 bytes)

tar命令

# 语法
tar [option] [压缩包名] [文件名...]

# 选项
c:给文件归档
-C(大写):指定解压的目录
f:指定包名  # f只能放在选项的最后面,其它选项可以随意调换位置
z:以gzip的格式压缩归档文件
v:显示过程
x:解压归档文件(解压后直接覆盖同名文件,不提示)
-X:排除不想压缩的文件(写入需要排除的文件名称放入一个文件中)
t:在不解压开文件的情况的下,查看压缩包中都有哪些文件
j:以bzip2的格式压缩归档文件
J:解压以bzip2的格式压缩归档文件
h:打包软链接文件
P:以绝对路径打包  
--hard-dereference:打包硬链接
--exclude:排除文件,一条命令只能排除一个

#1.注意:其它选项可以随意调换位置,f后面一定要接包名,尽量把f放在最后
#2.解压文件xf
#3.tar安全机制是如果以绝对路径打包,则会删除绝对路径最前面的'/',避免解压后覆盖原目录内容
#4.tar解压后,直接覆盖同名文件,不提示
#5.规范的压缩操作是:要压缩哪个目录,请进入这个目录的上一级目录,使用相对路径压缩

案例

# 语法
tar [option] [压缩包名] [文件名]

# 选项
# c:给文件归档
# f:指定包名  f只能放在选项的最后面,其它选项可以随意调换位置
[root@oldboy /tmp]# tar cf file.tar file1 file2
[root@oldboy /tmp]# ll
-rw-r--r--. 1 root root     0 Jun 21 17:30 file1
-rw-r--r--. 1 root root     0 Jun 21 17:30 file2
-rw-r--r--. 1 root root 10240 Jun 21 17:33 file.tar

# z:以gzip的格式压缩归档文件
[root@oldboy /tmp]# tar zcf file.tar.gz file1 file2
[root@oldboy /tmp]# ll
-rw-r--r--. 1 root root     0 Jun 21 17:30 file1
-rw-r--r--. 1 root root     0 Jun 21 17:30 file2
-rw-r--r--. 1 root root   122 Jun 21 17:42 file.tar.gz

# v:显示过程
[root@oldboy /tmp]# tar zcvf file.tar.gz file1 file2
file1
file2
[root@oldboy /tmp]# ll
-rw-r--r--. 1 root root     0 Jun 21 17:30 file1
-rw-r--r--. 1 root root     0 Jun 21 17:30 file2
-rw-r--r--. 1 root root   122 Jun 21 17:42 file.tar.gz
# x:解压归档文件
[root@oldboy /tmp]# tar zxvf file.tar.gz 
file1
file2
# -C(大写):指定解压的目录(不加这个选项无法指定其它目录)
[root@oldboy /tmp]# tar zxf file.tar.gz -C /opt/test/
[root@oldboy /tmp]# tar zxf file.tar.gz /opt/test/asdf/
tar: /opt/test/asdf: Not found in archive
tar: Exiting with failure status due to previous errors

# -X:排除不想压缩的文件(写入需要排除的文件名称放入一个文件中)
[root@oldboy /tmp]# vim /opt/exclude.txt
file4
ld123.zip

[root@oldboy /tmp]# tar zvcfX dirs.tgz /opt/exclude.txt dirs 
dirs/
dirs/test/
dirs/test/test1.gz
dirs/test/test2.gz
dirs/test/test3.gz
dirs/file5
dirs/file6

[root@oldboy /tmp]# tar zvcf dirs.tgz -X /opt/exclude.txt dirs
dirs/
dirs/test/
dirs/test/test1.gz
dirs/test/test2.gz
dirs/test/test3.gz
dirs/file5
dirs/file6
# t:在不解压开文件的情况的下,查看压缩包中都有哪些文件
[root@oldboy /tmp]# tar tf file.tar.gz 
file1
file2

# j:以bzip2的格式压缩归档文件
# J:解压以bzip2的格式压缩归档文件
# h:打包软链接文件
# P:以绝对路径打包  
# --hard-dereference:打包硬链接
# --exclude:排除文件,一条命令只能排除一个
[root@oldboy /tmp]# tar zvcf dirs.tgz --exclude file1 dirs
dirs/
dirs/test/
dirs/test/test1.gz
dirs/test/test2.gz
dirs/test/test3.gz
dirs/test/ld123.zip
dirs/ld123.zip
dirs/file4
dirs/file5
dirs/file6

1.如何使用gzip命令对文件进行压缩、解压

1.压缩
[root@oldboy /tmp]# gzip file1
2.解压
[root@oldboy /tmp]# gzip -d file1

2.如何用zip命令对文件以及目录进行压缩、解压

1.压缩
[root@oldboy /tmp]# zip file1.zip file1
2.解压
[root@oldboy /tmp]# unzip file1.zip

3.创建一个自己名字的文件至/opt目录

touch ld.txt /opt

4.打包opt整个目录,并命名test_opt.tar.gz

[root@oldboy /]# tar zcf test_opt.tar.gz /opt

5.查看打包好的test_opt.tar.gz里的文件

[root@oldboy /]# tar tf test_opt.tar.gz 
opt/
opt/ld.txt

6.将打包好的test_opt.tar.gz内容指定解压至/tmp目录

[root@oldboy /]# tar zxf test_opt.tar.gz -C /tmp
drwxr-xr-x. 3 root root    67 Jun 21 19:25 opt

7.打包etc目录下的所有文件,不要目录只要文件

[root@oldboy /opt]# mkdir test001
[root@oldboy /opt/test001]# find /etc/ ! -type d|xargs cp -t /opt/test001
[root@oldboy /opt/test001]# tar zvcf f.tgz ./*

8.打包etc目录下的所有文件,排除passwd,shadow

[root@oldboy /tmp]# cp -r /etc /tmp/
[root@oldboy /tmp]# vim a.txt
/tmp/etc/passwd
/tmp/etc/shadow
~  

[root@oldboy /tmp]# tar zcfX etc.tgz a.txt etc

9.打包etc目录下的所有以p开头的文件

[root@oldboy /tmp]# find /tmp/etc/ -iname 'p*' ! -type d | xargs tar zvcf p.tgz

10.打包etc目录下所有大于1M的文件

[root@oldboy /tmp]# find /tmp/etc/ -size +1M|xargs tar zvcf gt.tgz
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值