大纲:
文件的归档和压缩
1、tar命令的使用及参数解析
tar、gz、bz/bz2文件的创建、查看及解压
zip/unzip命令的使用

一、文件的归档和压缩

在我们的计算机中,经常会遇到有好多文件名相似或作用相似的文件,所有文件都摆放到一起
看着很乱,而且不易于查找和管理,这是,我们会对功能或名字相似的文件分别放在不同的文件夹中,便于管理和浏览,把这些文件都放在文件夹中的操作就叫做归档!!!!

归档的好处:
1.方便使用、查询、阅读
2.易于管理 (批量删除文件)

为什么要压缩?

如图:主机A要跟主机B传输一个大小为10G的文件估计传送100s.
如果直接传输会大量的占用流量带宽.导致公司的内网访问速度缓慢.
传输前压缩--传输后解压

我把10G的文件压缩成5G,传送时间是50s.

文件压缩的好处:
1.节约硬盘的资源.
2.加快文件传输的速率.

1.1 文件的归档
在Linux中我们通常使用tar命令,将文件归档
归档文件的类型有:tar、gz、bz/bz2、Z、zip等

tar、gz、bz2/bz文件的创建、查看及解压---tar命令的使用
我们可以使用命令man tar 或 tar --help命令查看帮助信息,
查看命令如何使用:

[root@xiaogan ~]# tar --help
Usage: tar [OPTION...] [FILE]...
GNU `tar' saves many files together into a single tape or disk archive, and can
restore individual files from the archive. Examples:
tar -cf archive.tar foo bar # 创建tar包,将foo和bar归档到archive.tar中
tar -tvf archive.tar # 查看archive.tar文件中归档的文件有哪些
tar -xf archive.tar # 将archive.tar文件解包

-A # 向压缩包文件中添加tar包文件 append tar files to an archive
-c # 创建
-r # 将要归档的文件添加到本文件的末尾 append files to the end of an archive
-t # 列出压缩包中文件目录
-u # 对tar包中文件进行升级(只对tar包中有改动的文件进行添加)
only append files newer than copy in archive
-d # 比较tar包与文件系统中的不通,删除tar包中的不通内容
-x # 解包 extract files from an archive

-f # 指定创建or解包归档文件的名字
-v # 显示详细信息、or显示进度
-k # 解包时,跳过已经存在的文件,不覆盖
-m # 解包时,设定文件的修改时间为现在

-z # gz文件创建及解压参数 filter the archive through gzip
-j # bz/bz2文件创建及解压参数 filter the archive through bzip2
-J # xz文件创建及解压参数 filter the archive through xz
-C # --directory=DIR指定解压文件路径 change to directory DIR
zip文件的创建、查看及解压---zip、unzip命令的使用

zip --help

[root@xiaogan ~]# zip --help
Copyright (c) - Info-ZIP - Type 'zip "-L"' for software license.
Zip 3.0 (July 5th ). Usage:
zip [-options] [-b path] [-t mmddyyyy] [-n suffixes] [zipfile list] [-xi list]
The default action is to add or replace zipfile entries from list, which
can include the special name - to compress standard input.
If zipfile and list are omitted, zip compresses stdin to stdout.
-f freshen: only changed files -u update: only changed or new files
-d delete entries in zipfile -m move into zipfile (delete OS files)
-r recurse into directories -j junk (don't record) directory names
- store only -l convert LF to CR LF (-ll CR LF to LF)
- compress faster - compress better
-q quiet operation -v verbose operation/print version info
-c add one-line comments -z add zipfile comment
-@ read names from stdin -o make zipfile as old as latest entry
-x exclude the following names -i include only the following names
-F fix zipfile (-FF try harder) -D do not add directory entries
-A adjust self-extracting exe -J junk zipfile prefix (unzipsfx)
-T test zipfile integrity -X eXclude eXtra file attributes
-y store symbolic links as the link instead of the referenced file
-e encrypt -n don't compress these suffixes
-h2 show more help

格式:zip [-options] [-b path] [-t mmddyyyy] [-n suffixes] [zipfile list] [-xi list]
unzip --help

[root@xiaogan ~]# unzip --help
UnZip 6.00 of April , by Info-ZIP. Maintained by C. Spieler. Send
bug reports using http://www.info-zip.org/zip-bug.html; see README for details. Usage: unzip [-Z] [-opts[modifiers]] file[.zip] [list] [-x xlist] [-d exdir]
Default action is to extract files in list, except those in xlist, to exdir;
file[.zip] may be a wildcard. -Z => ZipInfo mode ("unzip -Z" for usage). -p extract files to pipe, no messages -l list files (short format)
-f freshen existing files, create none -t test compressed archive data
-u update files, create if necessary -z display archive comment only
-v list verbosely/show version info -T timestamp archive to latest
-x exclude files that follow (in xlist) -d extract files into exdir
modifiers:
-n never overwrite existing files -q quiet mode (-qq => quieter)
-o overwrite files WITHOUT prompting -a auto-convert any text files
-j junk paths (do not make directories) -aa treat ALL files as text
-U use escapes for all non-ASCII Unicode -UU ignore any Unicode fields
-C match filenames case-insensitively -L make (some) names lowercase
-X restore UID/GID info -V retain VMS version numbers
-K keep setuid/setgid/tacky permissions -M pipe through "more" pager
See "unzip -hh" or unzip.txt for more help. Examples:
unzip data1 -x joe => extract all files except joe from zipfile data1.zip
unzip -p foo | more => send contents of foo.zip via pipe into program more
unzip -fo foo ReadMe => quietly replace existing ReadMe if archive file newer

实验:

1、归档文件创建、查看及解包

[root@xiaogan ~]# tar -cf grub2.tar /boot/grub2/
tar: Removing leading `/' from member names #详见注释
[root@xiaogan ~]# tar -tf grub2.tar
boot/grub2/
boot/grub2/themes/
boot/grub2/themes/system/
......
boot/grub2/fonts/unicode.pf2
boot/grub2/grubenv
boot/grub2/grub.cfg
[root@xiaogan ~]# ls
anaconda-ks.cfg Documents grub2.tar Music Public Templates
Desktop Downloads initial-setup-ks.cfg Pictures soft Videos
[root@xiaogan ~]# tar -xf grub2.tar
[root@xiaogan ~]# ls
anaconda-ks.cfg Desktop Downloads initial-setup-ks.cfg Pictures soft Videos
boot Documents grub2.tar Music Public Templates
[root@xiaogan ~]# ls boot
grub2
[root@xiaogan ~]#

2、gz压缩文件的创建、查看及解包

[root@xiaogan ~]# tar -zcf grub2.tar.gz /boot/grub2/
tar: Removing leading `/' from member names #详见注释
[root@xiaogan ~]# ls
anaconda-ks.cfg Desktop Downloads grub2.tar.gz Music Public Templates
boot Documents grub2.tar initial-setup-ks.cfg Pictures soft Videos
[root@xiaogan ~]# tar -tvf grub2.tar.gz #查看命令效果,与实验一对比有木有发现,加V和不加V的区别?!
drwx------ root/root -- : boot/grub2/
drwxr-xr-x root/root -- : boot/grub2/themes/
drwxr-xr-x root/root -- : boot/grub2/themes/system/
......
-rw-r--r-- root/root -- : boot/grub2/fonts/unicode.pf2
-rw-r--r-- root/root -- : boot/grub2/grubenv
-rw-r--r-- root/root -- : boot/grub2/grub.cfg
[root@xiaogan ~]# rm -rf boot
[root@xiaogan ~]# ls
anaconda-ks.cfg Documents grub2.tar initial-setup-ks.cfg Pictures soft Videos
Desktop Downloads grub2.tar.gz Music Public Templates
[root@xiaogan ~]# tar -zxf grub2.tar.gz
[root@xiaogan ~]# ls
anaconda-ks.cfg Desktop Downloads grub2.tar.gz Music Public Templates
boot Documents grub2.tar initial-setup-ks.cfg Pictures soft Videos
[root@xiaogan ~]# ls boot/grub2/
device.map fonts grub.cfg grubenv i386-pc locale themes
[root@xiaogan ~]#

3、bz/bz2压缩文件的创建、查看及解包

[root@xiaogan ~]# tar -jcf grub2.tar.bz2 /boot/grub2/
tar: Removing leading `/' from member names #详见注释
[root@xiaogan ~]# ls
anaconda-ks.cfg Desktop Downloads grub2.tar.bz2 initial-setup-ks.cfg Pictures soft Videos
boot Documents grub2.tar grub2.tar.gz Music Public Templates
[root@xiaogan ~]# tar -tf grub2.tar.bz2
boot/grub2/
boot/grub2/themes/
boot/grub2/themes/system/
......
boot/grub2/fonts/unicode.pf2
boot/grub2/grubenv
boot/grub2/grub.cfg
[root@xiaogan ~]# ls
anaconda-ks.cfg Desktop Downloads grub2.tar.bz2 initial-setup-ks.cfg Pictures soft Videos
boot Documents grub2.tar grub2.tar.gz Music Public Templates
[root@xiaogan ~]# rm -rf boot
[root@xiaogan ~]# ls
anaconda-ks.cfg Documents grub2.tar grub2.tar.gz Music Public Templates
Desktop Downloads grub2.tar.bz2 initial-setup-ks.cfg Pictures soft Videos
[root@xiaogan ~]# tar -jxf grub2.tar.bz2
[root@xiaogan ~]# ls
anaconda-ks.cfg Desktop Downloads grub2.tar.bz2 initial-setup-ks.cfg Pictures soft Videos
boot Documents grub2.tar grub2.tar.gz Music Public Templates
[root@xiaogan ~]# ls boot/grub2/
device.map fonts grub.cfg grubenv i386-pc locale themes
[root@xiaogan ~]#

注释:tar: Removing leading `/' from member names

不是错误,是tar删除了绝对路径最开始 / 而进行的提示。

压缩包里面的文件是绝对路径很容易害死人。

我就因为解压一个 cpio 包,里面文件竟然是绝对路径文件而不得不重装系统。

4、zip/unzip命令的使用
创建:(压缩目录或文件)

[root@xiaogan ~]# zip -r boot.zip boot
adding: boot/ (stored %)
adding: boot/grub2/ (stored %)
adding: boot/grub2/themes/ (stored %)
adding: boot/grub2/themes/system/ (stored %)
adding: boot/grub2/device.map (deflated %)
......
adding: boot/grub2/fonts/unicode.pf2 (deflated %)
adding: boot/grub2/grubenv (deflated %)
adding: boot/grub2/grub.cfg (deflated %)
[root@xiaogan ~]# zip passwd.zip /etc/passwd
adding: etc/passwd (deflated %)

解压:

[root@xiaogan ~]# mkdir test
[root@xiaogan ~]# unzip -d ./test boot.zip
Archive: boot.zip
creating: ./test/boot/
creating: ./test/boot/grub2/
creating: ./test/boot/grub2/themes/
creating: ./test/boot/grub2/themes/system/
inflating: ./test/boot/grub2/device.map
creating: ./test/boot/grub2/i386-pc/
inflating: ./test/boot/grub2/i386-pc/acpi.mod
......
inflating: ./test/boot/grub2/locale/en@piglatin.mo
creating: ./test/boot/grub2/fonts/
inflating: ./test/boot/grub2/fonts/unicode.pf2
inflating: ./test/boot/grub2/grubenv
inflating: ./test/boot/grub2/grub.cfg
[root@xiaogan ~]# unzip passwd.zip -d ./test/
Archive: passwd.zip
inflating: ./test/etc/passwd
[root@xiaogan ~]#

4、比较四种创建方式的不同

[root@xiaogan ~]# ls -lh *.tar* *.zip
-rw-r--r-- root root 3.2M 8月 : boot.zip
-rw-r--r-- root root 7.7M 8月 : grub2.tar
-rw-r--r-- root root 2.5M 8月 : grub2.tar.bz2
-rw-r--r-- root root 3.1M 8月 : grub2.tar.gz
-rw-r--r-- root root .1K 8月 : passwd.zip

有木有注意到?!三种创建归档文件的方式的不同?!

tar不压缩
gz文件压缩,但是压缩效率没有tar.bz2效果好!!!

1.2 file命令

在Linux系统中,对文件拓展名的应用不如windows运用的那么好,我们通常可以使用
ls -l命令,查看文件的类型,也可以通过file命令来查看文件的类型。

1.2.1 用法及参数解析
用法: file [选项...] [文件...]
-b #在列出辨识结果时,不显示文件名
-c #详细显示命令执行过程,便于排错或分析程序执行的情形
-C # 检查指定的文件(缺省为/etc/magic文件)有无格式错误。此验证一般不进行
-m #
-d #将任何缺省系统测试应用到文件
-z #探测压缩过的文件类型

[root@xiaogan ~]# file -z grub2.tar.gz
grub2.tar.gz: POSIX tar archive (GNU) (gzip compressed data, from Unix, last modified: Wed Aug :: )

-L #直接显示符号连接所指向文件的类别

[root@xiaogan ~]# ls /etc/sysconfig/selinux
/etc/sysconfig/selinux
[root@xiaogan ~]# file -L /etc/sysconfig/selinux
/etc/sysconfig/selinux: ASCII text
[root@xiaogan ~]# file /etc/sysconfig/selinux
/etc/sysconfig/selinux: symbolic link to `../selinux/config'

-f <文件名称> #从指定的文件中读取要分析的文件名列表

-F <分隔符号> #设定名称文件中文件之间的分隔号,缺省为一行一个文件

[root@xiaogan ~]# touch a.txt
[root@xiaogan ~]# echo grub2.tar > a.txt
[root@xiaogan ~]# echo grub2.tar.gz >> a.txt
[root@xiaogan ~]# echo grub2.tar.bz2 >> a.txt
[root@xiaogan ~]# file -f a.txt
grub2.tar: POSIX tar archive (GNU)
grub2.tar.gz: gzip compressed data, from Unix, last modified: Wed Aug ::
grub2.tar.bz2: bzip2 compressed data, block size = 900k

-I #如果文件不是普通文件,则不尝试对文件类型进行分来

-v #在标准输出设备中显示版本信息并退出

1.2.2 file命令返回结果以及含义:
empty 空文件
English text 英文正式文件
directory 目录文件
assembler program text 汇编语言程序的文本文件
ascii text ASCII编码的文本文件
command text 命令语言编写的命令正文程序
c program c语言正文程序
relocation text 用于连接的目标文件
executable 可执行的目标代码文件

第十天 1-9 rhel7-文件的归档和压缩的更多相关文章

  1. RHEL7文件归档与压缩

    本文介绍RHEL7.2文件的归档和压缩 文件归档 归档的好处:方便使用.查询.阅读,易于管理 (批量删除文件) 常用操作 命令:tar 作用:将许多文件一起保存至一个单独的磁带或磁盘归档,并能从归档中 ...

  2. linux专题一之文件归档和压缩(tar、file、zip)

     本文主要从以下几个方便来说明文件的归档和压缩,同时比较几种不同压缩方法的压缩比率及特点. 文件归档命令tar,tar.gz源码包的安装管理 创建tar包-解压-查询tar包内容 zip命令的用法 为 ...

  3. NeHe OpenGL教程 第三十八课:资源文件

    转自[翻译]NeHe OpenGL 教程 前言 声明,此 NeHe OpenGL教程系列文章由51博客yarin翻译(2010-08-19),本博客为转载并稍加整理与修改.对NeHe的OpenGL管线 ...

  4. 孤荷凌寒自学python第三十五天python的文件操作之针对文件操作的os模块的相关内容

     孤荷凌寒自学python第三十五天python的文件操作之针对文件操作的os模块的相关内容 (完整学习过程屏幕记录视频地址在文末,手写笔记在文末) 一.打开文件后,要务必记得关闭,所以一般的写法应当 ...

  5. 孤荷凌寒自学python第三十四天python的文件操作对file类的对象学习

     孤荷凌寒自学python第三十四天python的文件操作对file类的对象学习 (完整学习过程屏幕记录视频地址在文末,手写笔记在文末) 一.close() 当一个file对象执行此方法时,将关闭当前 ...

  6. JS&CSS文件请求合并及压缩处理研究(一)

    在我们日常的网站开发工作中,一个页面难免会引用到各种样式及脚本文件.了解Web开发的朋友们都知道,页面引用的每一个: <link href="style.css" rel=& ...

  7. ASP.NET MVC 4 Optimization的JS/CSS文件动态合并及压缩

    JS/CSS文件的打包合并(Bundling)及压缩(Minification)是指将多个JS或CSS文件打包合并成一个文件,并在网站发布之后进行压缩,从而减少HTTP请求次数,提高网络加载速度和页面 ...

  8. JS&CSS文件请求合并及压缩处理研究(五)

    接上篇.在我们最终调用 @Html.RenderResFile(ResourceType.Script) 或者 @Html.RenderResFile(ResourceType.StyleSheet) ...

  9. Linux:文件解压与压缩

    文件打包与压缩 常见压缩文件格式: |文件后缀名 |说明| |.zip |zip程序打包压缩的文件| |.rar |rar程序压缩的文件| |.7z |7zip程序压缩的文件| |.tar |tar程 ...

随机推荐

  1. PAT 1077 Kuchiguse [一般]

    1077 Kuchiguse (20 分) The Japanese language is notorious for its sentence ending particles. Personal ...

  2. MySQL整理(三)

     一.简单单表操作   (1)简单CRUD 插入查询结果 insert into table1(id,name,age) select id,name,age from table2 where id ...

  3. MongoDB的固定集合

    一.MongoDB固定集合概念 固定集合指的是事先创建,并且大小固定的集合.即假设一个集合设置了固定大小为100,再添加一条文档的时候,会把最前面的文档剔除,永远只保留100条数据. 固定集合特性:固 ...

  4. 编辑器——sublime

    在这里只介绍自己经常使用的编辑器sublime 第一:安装node插件[出处:http://www.bubuko.com/infodetail-798008.html] 1.下载Nodejs插件,下载 ...

  5. python全栈开发从入门到放弃之socket并发编程多线程GIL

    一 介绍 ''' 定义: In CPython, the global interpreter lock, or GIL, is a mutex that prevents multiple nati ...

  6. TypeError: Object of type 'int32' is not JSON serializable ——已解决

    将模型用flask封装,返回json时报错:TypeError: Object of type 'int32' is not JSON serializable 网上搜索出的解决方案:重写json.J ...

  7. split_lzo_lib.sh

    split_lzo_lib.sh #!/bin/sh#输入文件名filename=$1#分割文件大小filesize=4096#输出库文件名libname="lib"$(echo ...

  8. vux在ISO中异常 this.$vux.confirm.show

    在按钮事件中调用this.$vux.confirm.show,并且启用按钮的show-loading属性 安卓正常,ios中弹窗无法显示 经过排查,iso中设置按钮的loading后,要用异步setT ...

  9. iis日志时间与本地日期不一样

    iis日志里面的时间是 UTC时间,所以要自已加时区进行转换:即 utc时间+8小时:

  10. python---自动群发邮件

    生活中我们经常发送邮件,那么我们能不能用Python写一个自动发送邮件的功能呢?答案是肯定的!!! 开始实现功能之前我们需要开启我们邮箱的 IMAP/SMTP功能,我们先了解一下什么是IMAP/SMT ...