大纲:
文件的归档和压缩
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命令查看帮助信息,
查看命令如何使用:
[[email protected] ~]# 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
[[email protected] ~]# zip --help Copyright (c) 1990-2008 Info-ZIP - Type ‘zip "-L"‘ for software license. Zip 3.0 (July 5th 2008). 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 -0 store only -l convert LF to CR LF (-ll CR LF to LF) -1 compress faster -9 compress better -q quiet operation -v verbose operation/print version info -c add one-line comments -z add zipfile comment [email protected] 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
[[email protected]n ~]# unzip --help UnZip 6.00 of 20 April 2009, 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、归档文件创建、查看及解包
[[email protected] ~]# tar -cf grub2.tar /boot/grub2/ tar: Removing leading `/‘ from member names #详见注释 [[email protected] ~]# 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 [[email protected] ~]# ls anaconda-ks.cfg Documents grub2.tar Music Public Templates Desktop Downloads initial-setup-ks.cfg Pictures soft Videos [[email protected] ~]# tar -xf grub2.tar [[email protected] ~]# ls anaconda-ks.cfg Desktop Downloads initial-setup-ks.cfg Pictures soft Videos boot Documents grub2.tar Music Public Templates [[email protected] ~]# ls boot grub2 [[email protected] ~]#
2、gz压缩文件的创建、查看及解包
[[email protected] ~]# tar -zcf grub2.tar.gz /boot/grub2/ tar: Removing leading `/‘ from member names #详见注释 [[email protected] ~]# ls anaconda-ks.cfg Desktop Downloads grub2.tar.gz Music Public Templates boot Documents grub2.tar initial-setup-ks.cfg Pictures soft Videos [[email protected] ~]# tar -tvf grub2.tar.gz #查看命令效果,与实验一对比有木有发现,加V和不加V的区别?! drwx------ root/root 0 2016-07-27 21:54 boot/grub2/ drwxr-xr-x root/root 0 2016-07-27 21:45 boot/grub2/themes/ drwxr-xr-x root/root 0 2015-10-13 00:57 boot/grub2/themes/system/ ...... -rw-r--r-- root/root 2560080 2016-07-27 21:53 boot/grub2/fonts/unicode.pf2 -rw-r--r-- root/root 1024 2016-07-27 21:54 boot/grub2/grubenv -rw-r--r-- root/root 4257 2016-07-27 21:54 boot/grub2/grub.cfg [[email protected] ~]# rm -rf boot [[email protected] ~]# ls anaconda-ks.cfg Documents grub2.tar initial-setup-ks.cfg Pictures soft Videos Desktop Downloads grub2.tar.gz Music Public Templates [[email protected] ~]# tar -zxf grub2.tar.gz [[email protected] ~]# ls anaconda-ks.cfg Desktop Downloads grub2.tar.gz Music Public Templates boot Documents grub2.tar initial-setup-ks.cfg Pictures soft Videos [[email protected] ~]# ls boot/grub2/ device.map fonts grub.cfg grubenv i386-pc locale themes [[email protected] ~]#
3、bz/bz2压缩文件的创建、查看及解包
[[email protected] ~]# tar -jcf grub2.tar.bz2 /boot/grub2/ tar: Removing leading `/‘ from member names #详见注释 [[email protected] ~]# 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 [[email protected] ~]# 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 [[email protected] ~]# 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 [[email protected] ~]# rm -rf boot [[email protected] ~]# 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 [[email protected] ~]# tar -jxf grub2.tar.bz2 [[email protected] ~]# 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 [[email protected] ~]# ls boot/grub2/ device.map fonts grub.cfg grubenv i386-pc locale themes [[email protected] ~]#
注释:tar: Removing leading `/‘ from member names
不是错误,是tar删除了绝对路径最开始 / 而进行的提示。
压缩包里面的文件是绝对路径很容易害死人。
我就因为解压一个 cpio 包,里面文件竟然是绝对路径文件而不得不重装系统。
4、zip/unzip命令的使用
创建:(压缩目录或文件)
[[email protected] ~]# zip -r boot.zip boot adding: boot/ (stored 0%) adding: boot/grub2/ (stored 0%) adding: boot/grub2/themes/ (stored 0%) adding: boot/grub2/themes/system/ (stored 0%) adding: boot/grub2/device.map (deflated 25%) ...... adding: boot/grub2/fonts/unicode.pf2 (deflated 56%) adding: boot/grub2/grubenv (deflated 89%) adding: boot/grub2/grub.cfg (deflated 68%) [[email protected] ~]# zip passwd.zip /etc/passwd adding: etc/passwd (deflated 61%)
解压:
[[email protected] ~]# mkdir test [[email protected] ~]# 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/[email protected] creating: ./test/boot/grub2/fonts/ inflating: ./test/boot/grub2/fonts/unicode.pf2 inflating: ./test/boot/grub2/grubenv inflating: ./test/boot/grub2/grub.cfg [[email protected] ~]# unzip passwd.zip -d ./test/ Archive: passwd.zip inflating: ./test/etc/passwd [[email protected] ~]#
4、比较四种创建方式的不同
[[email protected] ~]# ls -lh *.tar* *.zip -rw-r--r-- 1 root root 3.2M 8月 10 23:03 boot.zip -rw-r--r-- 1 root root 7.7M 8月 10 21:42 grub2.tar -rw-r--r-- 1 root root 2.5M 8月 10 22:06 grub2.tar.bz2 -rw-r--r-- 1 root root 3.1M 8月 10 22:05 grub2.tar.gz -rw-r--r-- 1 root root 1.1K 8月 10 23:03 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 #探测压缩过的文件类型
[[email protected] ~]# file -z grub2.tar.gz grub2.tar.gz: POSIX tar archive (GNU) (gzip compressed data, from Unix, last modified: Wed Aug 10 22:05:35 2016)
-L #直接显示符号连接所指向文件的类别
[[email protected] ~]# ls /etc/sysconfig/selinux /etc/sysconfig/selinux [[email protected] ~]# file -L /etc/sysconfig/selinux /etc/sysconfig/selinux: ASCII text [[email protected] ~]# file /etc/sysconfig/selinux /etc/sysconfig/selinux: symbolic link to `../selinux/config‘
-f <文件名称> #从指定的文件中读取要分析的文件名列表
-F <分隔符号> #设定名称文件中文件之间的分隔号,缺省为一行一个文件
[[email protected] ~]# touch a.txt [[email protected] ~]# echo grub2.tar > a.txt [[email protected] ~]# echo grub2.tar.gz >> a.txt [[email protected] ~]# echo grub2.tar.bz2 >> a.txt [[email protected] ~]# file -f a.txt grub2.tar: POSIX tar archive (GNU) grub2.tar.gz: gzip compressed data, from Unix, last modified: Wed Aug 10 22:05:35 2016 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 可执行的目标代码文件