文件管理之:打包、压缩
文件管理之:打包、压缩
- 什么是打包压缩
打包指的是将多个⽂件和⽬录合并为⼀个特殊⽂件
然后将该特殊⽂件进⾏压缩
最终得到⼀个压缩包
- 为什么使⽤压缩包
1.减少占⽤的体积
2.加快⽹络的传输
- Windows的压缩和Linux的有什么不同 windows: zip rar(linux不⽀持)
linux: zip tar.gz tar.bz2 .gz
如果希望windows的软件能被linux解压,或者linux的软件包被windows能识别,选择zip.
PS: 压缩包的后缀不重要,但⼀定要携带.
- Linux下常见的压缩包类型
zip
是一个Windows和Linux中常用打包压缩工具,支持的压缩算法是zip。
zip工具需要安装
yum install zip unzip -y
zip压缩一个文件# 格式
zip [参数] 压缩包名称 文件路径
[root@abc ~]# zip 123.zip 123.log
adding: 123.log (deflated 87%)
[root@abc ~]# ls -l
total 4732
-rw-r--r-- 1 root root 646165 Mar 9 10:31 123.log
-rw-r--r-- 1 root root 85296 Mar 11 11:58 123.zip
zip压缩文件夹
# 需要一个-r参数去递归压缩文件夹下的所有内容
[root@abc ~]# zip -r dir.zip dir/
adding: dir/ (stored 0%)
adding: dir/one/ (stored 0%)
adding: dir/123.log (deflated 87%)
zip的静默输出
# -q:参数就是不输出任何打包信息
[root@abc opt]# zip -r -q etc.zip /etc/
[root@abc opt]# ls -l
total 14200
-rw-r--r-- 1 root root 13674457 Mar 11 12:15 etc.zip
zip解压命令(unzip)
# 格式
unzip [参数] 压缩包路径
# unzip解压命令只能解压由zip打包的压缩文件
[root@abc ~]# unzip dir.zip
Archive: dir.zip
inflating: dir/123.log
[root@abc ~]#
# 其他压缩包由unzip解压时随即报错。
[root@abc opt]# unzip nginx-.tar.gz
Archive: nginx-.tar.gz
End-of-central-directory signature not found. Either this file is not
a zipfile, or it constitutes one disk of a multi-part archive. In the
latter case the central directory and zipfile comment will be found on
the last disk(s) of this archive.
unzip: cannot find zipfile directory in one of nginx-.tar.gz or
nginx-.tar.gz.zip, and cannot find nginx-.tar.gz.ZIP, period.
# 查看压缩包中压缩那些内容,不解压?
# 只查看压缩包内容不解压需要使用 -l 参数
[root@abc opt]# unzip -l dir.zip
Archive: dir.zip
Length Date Time Name
0 03-11-2021 12:04 dir/
0 1 file
# 解压到指定目录(-d)
[root@abc ~]# unzip -d /root/ etc.zip
[root@abc opt]# cd /root/
[root@abc ~]# ls
] anaconda-ks.cfg dir.zip index.html test.pdf.gz xxxeth0xxx 系统优化.md
123.log demo.txt etc nginx-0.1.22.tar.gz test.txt 上传与下载.md
123.zip dir eth0xxx test xxxeth0 文件管理_(高级).pdf
# 静默输出(-q)
[root@abc ~]# rm -rf etc
[root@abc ~]# unzip -q -d /root/ /opt/etc.zip
[root@abc ~]# ls -l
total 4828
drwxr-xr-x 91 root root 8192 Mar 11 11:16 etc
gzip
通过gzip压缩算法,将文件压缩一定体积,有利于传输, 不支持打包
[root@abc ~]# ls -l
total 4828
-rw-r--r-- 1 root root 244977 Mar 10 12:12 index.html
[root@abc ~]# gzip index.html
[root@abc ~]# ls -l
total 4612
-rw-r--r-- 1 root root 22652 Mar 10 12:12 index.html.gz
gzip压缩一个目录
[root@abc etc]# gzip -r /etc
[root@abc etc]# ls
abrt GREP_COLORS.gz my.cnf.d security
adjtime.gz groff my.cnf.gz selinux
aliases.db.gz group-.gz NetworkManager services.gz
aliases.gz group.gz networks.gz sestatus.conf.gz
alternatives grub2.cfg nsswitch.conf.bak.gz sgml
anacrontab.gz grub.d nsswitch.conf.gz shadow
gzip解压(-d)
[root@abc ~]# ls -l
-rw-r--r-- 1 0 0 22652 Mar 10 12:12 index.html.gz
[root@abc ~]# gzip -d index.html.gz
[root@abc ~]# ls -l
total 4828
-rw-r--r-- 1 0 0 244977 Mar 10 12:12 index.html
bzip2
使用bzip2 压缩算法来压缩一定体积的文件。
[root@abc ~]# ls -l
total 4828
-rw-r--r-- 1 root root 646165 Mar 9 10:31 123.log
[root@abc ~]# bzip2 123.log
[root@abc ~]# ls -l
total 4240
-rw-r--r-- 1 root root 0 Mar 10 12:04 ]
-rw-r--r-- 1 root root 42210 Mar 9 10:31 123.log.bz2
bzip2解压(-d)
bzip2解压是针对于bzip2压缩的压缩包来进行解压。
[root@abc ~]# ls -l
total 4240
-rw-r--r-- 1 root root 42210 Mar 9 10:31 123.log.bz2
[root@abc ~]# bzip2 -d 123.log.bz2
[root@abc ~]# ls -l
total 4828
-rw-r--r-- 1 root root 646165 Mar 9 10:31 123.log
tar
tar其实是一个打包工具,不具备压缩功能,但是可以使用参数调用压缩工具来进行解压。
参数
-c : 创建压缩包
-f : 指定压缩包名称
[root@abc ~]# tar -c -f test.tar 123.log
[root@abc ~]# ls -l
total 5468
-rw-r--r-- 1 root root 655360 Mar 11 15:49 test.tar
-z : 指定使用gzip压缩工具进行压缩
[root@abc ~]# tar -c -z -f test-one.tar 123.log
[root@abc ~]# ls -l
total 5084
-rw-r--r-- 1 root root 85279 Mar 11 15:56 test-one.tar
# 注:使用-z参数,不会自动添加.gz后缀
[root@abc ~]# tar -c -z -f anaconda.tar.gz anaconda-ks.cfg
[root@abc ~]# ls -l
total 5084
-rw-r--r-- 1 root root 1010 Mar 11 15:58 anaconda.tar.gz
-j : 指定使用bzip2压缩工具进行压缩
[root@abc ~]# tar -c -j -f 123-bask-one.tar 123.log
[root@abc ~]# ls -l
total 5172
-rw-r--r-- 1 root root 42328 Mar 11 16:00 123-bak.tar.bz2
-rw-r--r-- 1 root root 42328 Mar 11 16:01 123-bask-one.tar
-t : 查看压缩包内容
[root@abc ~]# tar -t -f 123-bak.tar.bz2
123.log
-v : 显示压缩包压缩过程
[root@abc ~]# tar -x -v -f etc.tar -C /opt/
/etc/centos-release
/etc/DIR_COLORS.lightbgcolor
/etc/libaudit.conf
/etc/mail.rc
-P : 允许使用绝对路径进行打包
[root@abc ~]# tar -c -P -f 123-three.tar /etc/passwd
[root@abc ~]# tar -c -f 123-three.tar /etc/passwd
tar: Removing leading `/' from member names
-x : 解压
# tar解压是按照原来的路径进行解压
[root@abc test]# tar -x -f etc.tar
# tar会自动识别压缩功能
C : 指定解压路径
[root@abc ~]# tar -x -f etc.tar -C /opt/
tar: Removing leading `/' from member names
[root@abc ~]# cd /opt/
[root@abc opt]# ls
abc23 dir dir.zip etc nginx-0.1.22.tar.gz nginx-.tar.gz xxx
--exclude : 排除某些文件
[root@abc test-tar]# tar -c -f abc.tar ./* --exclude=abc7 --exclude=abc5 --exclude=abc1
[root@abc test-tar]# tar -t -f abc.tar
./abc2
./abc3
./abc4
./abc6
./abc8
./abc9
--exclude-from : 根据某个文件列表排除多个文件
[root@abc test-tar]# cat list.txt
abc995
abc996
abc997
abc998
abc999
[root@abc test-tar]# tar -c -f abc.tar ./* --exclude-from=list.txt
-h : 打包软连接
[root@abc test-tar]# tar -c -h -f bin-h.tar /bin
总结tar参数
tar参数
-c : 创建压缩
-f ; 指定压缩包名称
-z : 使用gzip压缩工具进行压缩
-j : 使用bzip2压缩工具进行压缩
-J : 使用xz压缩工具进行压缩
-t : 显示压缩包内容,不解压
-v : 显示压缩过程
-P : 允许使用绝对路径进行压缩
-x : 解压
-C : 指定解压路径
-h : 打包软连接
--exclude : 排除某些文件
--exclude-from : 根据文件列表排除多个文件
习题
1.linux下常见的压缩包类型有哪些
zip
gz
bz2
tar.gz
tar.bz
tar
2.将/etc/hosts文件用tar格式打包。
tar -c -P -f hosts.tar /etc/hosts
3.查看打包之后的/etc/hosts的文件内容,在不解压的情况下查看。
tar -t -f hosts.tar
4.使用tar打包/var/log/目录。
tar -c -P -f hosts.tar /var/log/
5.使用zip打包/etc目录。
zip -r etc.zip /etc
6.查看/var/log/abc.zip目录的压缩包中有哪些内容。
unzip -l /var/log/abc.zip
7.将/var/log/abc.zip目录解压到/opt目录中。
unzip -d /opt /var/log/abc.zip
10.解压/etc/abc.tar.gz目录到/opt目录中。
tar -xf /etc/abc.tar.gz -C /opt
11.用zip打包/opt目录,要求不显示打包过程。
zip -q opt.zip /opt
zip [参数] 压缩包名称 压缩文件路径
12.打包/etc/目录,要求是.bz2格式
tar -c -j -f etc.tar /etc
13.打包/var/log目录,要求是.xz格式
tar -c -J -f log.tar.xz /var/log
14.使用tar命令打包/etc/时,会出现一个删根的操作,怎样打包不会进行删根的操作
tar -c -P -f etc.tar /etc
15.打包/etc/目录,要求不打包/etc/hosts这个文件。
tar -c -f 15.tar /etc/ --exclude=/etc/hosts
16.打包/etc/目录,要求不打包/etc/hosts和/etc/hostname这两个文件。
tar -c -f 16.tar /etc/ --exclude=/etc/hosts --exclude=/etc/hostname
17.打包/etc/目录,但要排除passwd,shadow,group,gshadow,hosts,hostname这些文件。(你能用两种方法实现吗)
tar -c -f 17.tar /etc/ --exclude-from=list.txt
18.已知/etc/grub2.cfg文件是个软连接文件,在你不知道的情况下,请问怎么打包该文件的真实文件。
tar -chf 18.tar /etc/grub2.cfg
19.把/var/log/目录中所有.log的文件进行打包成一个压缩包,名称定义为log.tar.gz的压缩包。
find /var/log/ -name ".log" | xargs tar -c -z -f log.tar.gz
find /var/log/ -name ".log" -exec tar -czf log.tar.gz2 {} ;
20.已知文件oldboy.gz,请问在不解压的情况下,怎样查看该文件的内容。
gzip -l
21.打包/etc/目录,当前时间方式的压缩包:比如: 2019-12-24_etc.tar.gz
tar -czPf $(date +%F)_etc.tar.gz /etc
tar -czf date +%Y-%m-%d
_etc.tar.gz /etc (适合备份每天的日志)(反引号代表先执行)
22.创建/data/bak目录,然后复制如下文件到/data/bak目录下
[root@localhost ~]# mkdir -p /data/bak
[root@localhost ~]# cp abc.txt /data/bak
23.接22题,使用tar命令对/data/bak目录下的文件及目录以gzip的格式进行归档压缩到/data目录下压缩包的名字以自己名字命名)
cd /data
tar -czf bak.tar.gz -C /data
24.使用tar命令查看上题/data目录下压缩包内的内容。
tar -tf bak.tar.gz
25.把第23题/data目录下的压缩包,解压到/backup目录下
tar -xf /date/bak.tar.gz -C /backup
26.再次使用tar命令把/data/bak目录下的文件及目录以gzip的格式进行归档压缩到/data目录下,但是在进行归档压缩时,排除文件“sudoers”,然后查看该压缩包内容是否存在文件“sudoers”(压缩包名自行拟定)
tar -czPf test.tar.gz --exclude=/data/bak/sudoers /data/bak
tar --exclude tar -t
27.打包/etc目录下所有普通文件到root用户家目录。
finf /etc -type f -exec tar -cf etc.tar {} -C /toot/ ;
28.打包/etc/目录到/opt/目录下,名称要求以当前主机名命名,例:oldboy_10.0.0.100.tar.gz
cd /opt
文件--zip 30.zip 1.txt 解压;unzip 30.zip
31.创建一个自己名字的文件至/opt目录
32.打包opt整个目录,并命名test_opt.tar.gz
tar -czf test_opt.tar.gz /opt
33.查看打包好的test_opt.tar.gz里的文件
tar -t -f test_opt.tar.gz
34.将打包好的test_opt.tar.gz内容指定解压至/tmp目录
tar -xf test_opt.tar.gz -C /tmp
35.打包etc目录下的所有文件,不要目录只要文件
find /etc ! -type d -exec tar -cf etc1.tar { } ;
36.打包etc目录下的所有文件,排除passwd,shadow
find /etc ! -type d |xargs tar -czf etc2.tar.gz --exclude =passwd --exclude=shadow
37.打包etc目录下的所有以p开头的文件
find /etc -name "p*" -exec tar -cf 111.tar {} ;
38.打包etc目录下所有大于1M的文件
find /etc -size +1M |xargs tar -czf etc4.tar.gz
文件管理之:打包、压缩相关推荐
- 文件管理-Linux系统压缩打包
ZIP压缩工具 TAR压缩工具 TAR实践案例 windows下我们接触最多的压缩文件就是.rar格式, 但Linux有自己所特有的压缩工具 如果希望windows和Linux互相能使用的压缩工具, ...
- 第二十天: Linux文件管理+Linux备份压缩+网络与磁盘管理+shell与安装
Linux第二天 1 Linux文件管理 1.1 touch命令 在Windows系统中,我们如果想创建一个文本文档或者word文件的时候,通常的做法是 鼠标右键---新建---文本文档,这样的话,我 ...
- Linux基础命令-tar打包压缩文件
Linux基础命令-echo输出信息_Linux学习中的博客-CSDN博客 Linux三剑客-grep命令_Linux学习中的博客-CSDN博客 Linux文件管理命令(3)-mv改动文件_Linux ...
- Linux打包压缩:zcat、compress、gzip、bzip、xz、zip、tar、cpio
文章目录 常见解压/压缩命令 压缩.解压缩工具 一.zcat 显示压缩包中文件的内容 (一).语法 (二).参数 (三).常用命令查看压缩包内容命令: 二.compress/uncompress压缩工 ...
- html如何打包压缩,所有css打包压缩到一个js里面
所有css打包压缩到一个js里面 打包css文件的意义:最终把css文件压缩到最终生成的js文件里,页面不需要再加载css文件,并且是压缩过的 打包css文件,安装style-loader css-l ...
- python打开指定文件-python打包压缩、读取指定目录下的指定类型文件
下面通过代码给大家介绍python打包压缩指定目录下的指定类型文件,具体代码如下所示: import os import datetime import tarfile import fnmatch ...
- find文件,tar打包和打包压缩
[root@localhost ~]# find . -name "*.txt*" -name "*test*" -type f |xargs tar -cvf ...
- 【转】JPG打包压缩后比原来尺寸还大
[转]JPG打包压缩后比原来尺寸还大 作者:刘源 链接:https://www.zhihu.com/question/40371280/answer/86262934 来源:知乎 著作权归作者所有.商 ...
- linux 如何对文件解压或打包压缩
tar命令用与对文件打包压缩或解压,格式: tar [选项] [文件] 打包并压缩文件: tar -czvf 压缩包名 .tar.gz 解压并展开压缩包: tar -xzvf 压缩包名 .tar. ...
- ubuntu 打包压缩
打包 tar -cvf test.tar *.txt tar -cvf test.tar *.txt 解包 tar -xvf test.tar tar -xvf test.tar -C abc/ ...
最新文章
- layUI 学习记录
- 如果成为一名高级安卓开发_什么是高级开发人员,我如何成为一名开发人员?
- 图解高内聚与低耦合,傻瓜都能看懂!
- SimUDuck 策略模式
- 通过错误的sql来测试推理sql的解析过程
- unity 存档插件_【Unity消息】5月1日到5月15日 Unity资源商店大促
- 安卓学习笔记04:安卓平台架构
- 深入java虚拟机需要读吗_《深入理解Java虚拟机》读后总结(一)JVM内存模型
- 美国 CISA 和 NIST 联合发布软件供应链攻击相关风险及缓解措施
- java判断两个矩形是否相交_判断矩形相交以及求出相交的区域
- 数学分析原理 定理 6.8
- shrink_page_list 函数分析
- 开关电源/LDO反馈电阻辅助计算工具PowerHelper使用介绍
- JAVA算法: 给定一个整数转换成对应的罗马字符(Integer to Roman)
- Android如何绘制矩形方框,绘制矩形(方法二、空心的)
- 3.8086/8088微处理器结构
- python中幂运算_python 计算幂
- 006输出9行内容,第1行输出1,第2行输出12,第3行输出123,以此类推,第9行输出123456789。
- 使用python进行分布分析(算数平均、几何平均、偏度、峰度,绘制直方图),以2022年上半年沪深300指数为例
- Line-based Automatic Extrinsic Calibration of LiDAR and Camera论文阅读
热门文章
- Unity 屏幕分辨率的设置
- vue项目pc端页面适配
- linux关闭监听端口命令,如何在Linux系统中监听和关闭端口
- android- activity,Application,activity渲染xml文件
- C语言——计算标准差公式
- SMTP协议?SMTP端口号?SMTP服务器?
- 非度量多维标度_用R语言做非度量多维尺度分析(NMDS)
- 亲测Trimble X7三维扫描仪
- 学术汇报(academic presentation)/PPT应该怎么做?
- 怎么把php转成bt_php能不能转换成bt种子