第十天 1-9 rhel7-文件的归档和压缩
大纲:
文件的归档和压缩
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-文件的归档和压缩的更多相关文章
- RHEL7文件归档与压缩
本文介绍RHEL7.2文件的归档和压缩 文件归档 归档的好处:方便使用.查询.阅读,易于管理 (批量删除文件) 常用操作 命令:tar 作用:将许多文件一起保存至一个单独的磁带或磁盘归档,并能从归档中 ...
- linux专题一之文件归档和压缩(tar、file、zip)
本文主要从以下几个方便来说明文件的归档和压缩,同时比较几种不同压缩方法的压缩比率及特点. 文件归档命令tar,tar.gz源码包的安装管理 创建tar包-解压-查询tar包内容 zip命令的用法 为 ...
- NeHe OpenGL教程 第三十八课:资源文件
转自[翻译]NeHe OpenGL 教程 前言 声明,此 NeHe OpenGL教程系列文章由51博客yarin翻译(2010-08-19),本博客为转载并稍加整理与修改.对NeHe的OpenGL管线 ...
- 孤荷凌寒自学python第三十五天python的文件操作之针对文件操作的os模块的相关内容
孤荷凌寒自学python第三十五天python的文件操作之针对文件操作的os模块的相关内容 (完整学习过程屏幕记录视频地址在文末,手写笔记在文末) 一.打开文件后,要务必记得关闭,所以一般的写法应当 ...
- 孤荷凌寒自学python第三十四天python的文件操作对file类的对象学习
孤荷凌寒自学python第三十四天python的文件操作对file类的对象学习 (完整学习过程屏幕记录视频地址在文末,手写笔记在文末) 一.close() 当一个file对象执行此方法时,将关闭当前 ...
- JS&CSS文件请求合并及压缩处理研究(一)
在我们日常的网站开发工作中,一个页面难免会引用到各种样式及脚本文件.了解Web开发的朋友们都知道,页面引用的每一个: <link href="style.css" rel=& ...
- ASP.NET MVC 4 Optimization的JS/CSS文件动态合并及压缩
JS/CSS文件的打包合并(Bundling)及压缩(Minification)是指将多个JS或CSS文件打包合并成一个文件,并在网站发布之后进行压缩,从而减少HTTP请求次数,提高网络加载速度和页面 ...
- JS&CSS文件请求合并及压缩处理研究(五)
接上篇.在我们最终调用 @Html.RenderResFile(ResourceType.Script) 或者 @Html.RenderResFile(ResourceType.StyleSheet) ...
- Linux:文件解压与压缩
文件打包与压缩 常见压缩文件格式: |文件后缀名 |说明| |.zip |zip程序打包压缩的文件| |.rar |rar程序压缩的文件| |.7z |7zip程序压缩的文件| |.tar |tar程 ...
随机推荐
- docker镜像上传到阿里云
目前上传本地镜像到网上有多种途径,一个是上传到hub上,一个是阿里云镜像仓库,还要其他服务器. 上传到hub上实在是太慢了,我的服务器用的是阿里云,所以选择上传到阿里云镜像仓库中. 前提条件:linu ...
- vue的项目结构
一. 准备工作 1. 初始化项目 vue init webpack itany cd itany cnpm install cnpm install less less-loa ...
- $python用装饰器实现一个计时器
直接上代码: import time from functools import wraps # 定义装饰器 def fn_timer(function): @wraps(function) def ...
- Ubuntu16.04中查看硬盘的型号和读取速度
最近在测试FTP服务器,上传和下载的速度与很多因数有关,其中,硬盘的读取速度就是其中不同的区别点,我同时用了三台不用的服务器架设FTP服务,一台是出来ftp服务外还含平台其他管理软件,一台是全新的系统 ...
- genisoimage命令用法
功能说明:建立ISO 9660映像文件. 常用命令:genisoimage -o imagename.iso file 语 法:mkisofs [-adDfhJlLNrRTvz][-print-si ...
- PHP jsonencode 已经json中包含 汉字的处理
<?php $arr = array ( 'Name'=>'希亚', ); $jsonencode = json_encode($arr); echo $jsonencode; ?> ...
- Qt大小端
转:http://blog.csdn.net/usownh/article/details/42614185 大端模式和小端模式是计算机中经常涉及到的两种字节序,也有大端对齐.小端对齐.大尾.小尾等叫 ...
- 用python打印99乘法口诀表
代码如下 #!/usr/bin/env python # encoding: utf-8 __author__ = 'Nicholas.Cage' i = 1 j = 1 while i <= ...
- 在vim中的复制,剪切,粘贴
1. 剪切和粘贴 定位鼠标到剪切的开始位置 输入v键开始选择剪切的字符,或者V键是为了选择 整行 移动方向键到结束的地方 d键是剪切,y键是复制 移动鼠标到粘贴的位置 输入P是在鼠标位置前粘贴,输入p ...
- 入手sm961
测速: 发现这个测速软件不同版本测试还不一样 下面是我的intel750的,用最新版本测试软件测的 淘宝买了一个散热片