ICode9

精准搜索请尝试: 精确搜索
首页 > 系统相关> 文章详细

[linux]循序渐进学运维-基础命令篇-文件的归档和压缩

2021-04-13 23:52:43  阅读:171  来源: 互联网

标签:学运 zmgaosh tar zip linux etc 归档 txt root


写在前面:
博主是一名投身教培事业的标准八零后,叛逆而且追求自由,昵称取自于苏轼的《水调歌头》中的“高处不胜寒”,时刻提醒自己要耐得住寂寞,受的了孤独,在技术的道路上,不畏艰难,勇往直前。
我已经将全部的linux运维体系整理到了gitee上,https://gitee.com/gaosh08/LinuxFamily
欢迎star,投稿,交流,后续还会有python系列和java系列。


文章目录


文件的归档和压缩命令有很多,我们今天着重来介绍以下内容:


  • tar命令进行文件的归档和压缩
  • zip管理压缩文件

归档和压缩文件的好处:节约硬盘的资源 ,加快文件传输速率

我们先来看tar命令

1 . tar: 作用打包压缩文件

用法: tar 【参数】 file

参数:

参数作用
-ccreate创建文件
-x-extract [ˈekstrækt] 提取 解压还原文件
-v–verbose显示执行详细过程
-f–file指定备份文件
-t–list 列出压缩包中包括哪些文件,不解包,查看包中的内容
-C(大写)–directory 指定解压位置

举例:

1) 把当前的路径下的文件打包,命名为loacl.tar
[root@zmgaosh ~]# tar -cvf local.tar ./[root@zmgaosh ~]# ls2     a.txt  b.txt.patch  ***local.tar***  passwd1  zmedu.txt
a.sh  b.txt  file         passwd     test     zmeduv2.txt

解压缩:

[root@zmgaosh ~]# mkdir test1[root@zmgaosh ~]# mv local.tar test1/[root@zmgaosh ~]# cd test1[root@zmgaosh test1]# tar xvf local.tar [root@zmgaosh test1]# ls2     a.txt  b.txt.patch  file       passwd   test       zmeduv2.txt
a.sh  b.txt  etc.tar      local.tar  passwd1  zmedu.txt[root@zmgaosh test1]#

2) 指定解压位置 -C
[root@zmgaosh ~]# ls2     a.txt  b.txt.patch  file       passwd   test   zmedu.txt
a.sh  b.txt  etc.tar      local.tar  passwd1  test1  zmeduv2.txt[root@zmgaosh ~]# tar xvf local.tar  -C /opt/    #解压到/opt下[root@zmgaosh ~]# ls /opt/   #查看是否解压成功2  a.sh  a.txt  b.txt  b.txt.patch  etc.tar  file  passwd  passwd1  test  test1  xinsz1  zmedu.txt  zmeduv2.txt[root@zmgaosh ~]#

3) 不解包查看tar包中的内容
[root@zmgaosh ~]# tar tvf local.tar dr-xr-x--- root/root         0 2020-06-20 19:57 ./
-rw------- root/root      5461 2020-06-19 20:55 ./.viminfo
-rw-r--r-- root/root        91 2020-06-19 20:17 ./passwd
-rwxrwxrwx root/root       157 2020-06-17 23:22 ./a.sh
-rw-r--r-- root/root       176 2013-12-29 10:26 ./.bashrc

2. tar 归档+压缩

语法: tar czvf newfile.tar.gz

常用参数:

-z, --gzip 以gzip方式压缩 扩展名: tar.gz
-j : 以bz2方式压缩的 扩展名:tar.bz2
-J : 以xz 方式压缩 扩展名:tar.xz

举例:

1.创建tar.gz的包
[root@zmedu16 ~]# tar cvf etc.tar /etc [root@localhost test]# tar zcvf etc.tar.gz /etc  #归档,注意备份的名字后缀[root@localhost test]# tar zxvf etc.tar.gz   #解压缩

2. 创建tar.bz2包
语法: #tar jcvf newfile.tar.bz2  SOURCE[root@zmedu16 ~]#  tar -jcvf etc.tar.bz2 /etc   [root@zmedu16 ~]#  tar -jxvf etc.tar.bz2 /etc    #解压缩[root@zmedu16 ~]#  tar jxvf etc.tar.bz2 -C  /opt    #解压到opt目录下

3. 创建.tar.xz的包
[root@zmedu16 ~]#  tar -Jcvf etc.tar.xz /etc[root@zmedu16 ~]#  tar -xvf etc.tar.xz    #tar.xz 这类包,解压缩或:[root@zmedu16 ~]#  tar -Jxvf etc.tar.xz

三种压缩方式中, tar.gz tar.bz2是比较常用的
tar.xz 压缩比例最高,压缩时间最长,压缩完文件最小

3. zip 管理压缩文件

zip是压缩程序,unzip是解压程序

安装zip
[root@zmgaosh zip]# yum install zip

压缩:

[root@zmgaosh zip]# zip passwd.zip /etc/passwd
  adding: etc/passwd (deflated 61%)[root@zmgaosh zip]# lspasswd.zip

解压缩:

[root@zmgaosh zip]# unzip passwd.zip Archive:  passwd.zip
  inflating: etc/passwd              
[root@zmgaosh zip]# lsetc  passwd.zip

如果要指定目标解压目录可以加参数-d

[root@zmgaosh zip]# unzip passwd.zip  -d /opt/

小技巧

如何查看看压缩文件的内容

1. 使用vim直接查看压缩包的内容

[root@zmgaosh zip]# vim passwd.zip

在这里插入图片描述

2. 使用file文件查看
[root@zmgaosh zip]# file passwd.zip passwd.zip: Zip archive data, at least v2.0 to extract

总结

Linux下压缩解压命令还有很多,但最常见的还是tar命令和zip命令

比如:

1) ,gz的包:

解压:gunzip filename.gz

2).bz2 的包

bzip2 -d filename.bz2

3).tar.bz2的包

tar jxvf filename.tar.bz2

4).bz的包

bzip2 filename.bz

5).Z的包

uncompress filename.Z

6).rar的包

rar x filename.rar

白嫖不好,创作不易,各位的点赞就是胜寒创作的最大动力,我们下篇文章再见!

标签:学运,zmgaosh,tar,zip,linux,etc,归档,txt,root
来源: https://blog.51cto.com/xinsz08/2704524

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有