预习内容
6.1 压缩打包介绍
6.2 gzip压缩工具
6.3 bzip2压缩工具
6.4 xz压缩工具
6.5 zip压缩工具
6.6 tar打包
6.7 打包并压缩
6.1 压缩打包介绍
在windows下,压缩文件的格式有.rar .zip .7z。我们接触最多的压缩文件是.rar格式的,但在linux系统下,是不能识别这种格式的,它有自己独特的压缩工具,但zip格式的文件在windows和linux下都能使用。使用压缩文件,不仅能节省磁盘空间,而且在传输时还能节省网络带宽。
linux下最常见的压缩文件通常都是.tar .gz格式的。除此之外,还有.tar .gz .bz2 .zip等格式。之前说过linux下的文件后缀名可加可不加,但压缩文件最好加上后缀名,这是为了判断压缩文件是由那种压缩工具所压缩的,而后才能正确地解压这个文件。下面就是linux下常见的后缀名所对应的压缩工具:
.gz:表示由gzip压缩工具压缩的文件 .bz2:表示由.bz2压缩工具压缩的文件 .tar:表示由tar打包程序打包的文件(tar并没有压缩功能,只是把一个目录合并成一个文件) .tar.gz:可以理解为先由tar打包,然后在由gzip压缩。 .tar.bz2:可以理解为先由tar打包,然后在由bzip2压缩 .tar.xz:可以理解成先由tar打包,然后再xz压缩。
6.2 gzip压缩工具
gzip格式为gzip【-d#】filename,其中#为1~9数字
-d:该参数在解压缩时使用。
-#:表示压缩等级,1为最差,9为最好,6为默认。
举例说明:
- 首先我在/tmp下创建一个目录xqw,然后tree一下该/tmp下。
[root@localhost ~]# mkdir /tmp/xqw [root@localhost ~]# tree /tmp /tmp ├── 1.txt │ └── dnsmasq.conf ├── 2.test ├── 3.txt ├── hongwei.123 ├── hongweilinux │ └── 2 ├── hongwei.txt ├── hw │ ├── 2 │ ├── 22 │ └── 23 │ └── 1234 ├── newdisk ├── passwd ├── qwer ├── test_mv │ ├── dirb │ │ └── dira │ └── dirc ├── tset │ └── abc └── xqw 13 directories, 10 files
- 然后我们找到/etc下所有conf文件,并把这些文件载到xqw目录下,创建文件名字是22.txt
[root@localhost ~]# cd /tmp [root@localhost tmp]# cd xqw/ [root@localhost xqw]# ls [root@localhost xqw]# find /etc/ -type f -name "*conf" -exec cat {} >> 22.txt \; [root@localhost xqw]# ls 22.txt
- 然后我们压缩22.txt文件。gzip后面直接跟文件名,表示当前目录下压缩该文件,而源文件也会消失
[root@localhost xqw]# gzip 22.txt [root@localhost xqw]# ls 22.txt.gz
[root@localhost xqw]# du -sh 22.txt.gz 68K 22.txt.gz 可以看到压缩文件的大小
- 我们给22.txt文件进行解压:
[root@localhost xqw]# gzip -d 22.txt.gz [root@localhost xqw]# ls 22.txt
- 用gzip解压时除了用-d也可以使用gunzip命令:
[root@localhost xqw]# gzip 22.txt [root@localhost xqw]# ls 22.txt.gz [root@localhost xqw]# gunzip 22.txt.gz [root@localhost xqw]# ls 22.txt
- 我们使用wc -l 可以查看压缩文件的总行数:
[root@localhost xqw]# wc -l 22.txt.gz 235 22.txt.gz
- 我们可以使用file查看压缩文件的类型:
[root@localhost xqw]# file 22.txt.gz 22.txt.gz: gzip compressed data, was "22.txt", from Unix, last modified: Sat May 26 02:51:52 2018
- 使用命令zcat查看压缩文件。因为cat是不能直接查看压缩文件的.用cat命令查看会显示乱码.
[root@localhost xqw]# zcat 22.txt.gz # Format of the login access control table is three fields separated by a # ":" character: # # [Note, if you supply a 'fieldsep=|' argument to the pam_access.so # module, you can change the field separation character to be # '|'. This is useful for configurations where you are trying to use # pam_access with X applications that provide PAM_TTY values that are # the display variable like "host:0".] # # permission : users : origins # # The first field should be a "+" (access granted) or "-" (access denied) # character. # # The second field should be a list of one or more login names, group # names, or ALL (always matches). A pattern of the form user@host is # matched when the login name matches the "user" part, and when the # "host" part matches the local machine name. # # The third field should be a list of one or more tty names (for # non-networked logins), host names, domain names (begin with "."), host # addresses, internet network numbers (end with "."), ALL (always # matches), NONE (matches no tty on non-networked logins) or # LOCAL (matches any string that does not contain a "." character).
- 我们在用gzip压缩时原文件是被覆盖了,那我们可以使用-c >把压缩文件的原文件不删除,如下:
[root@localhost xqw]# gunzip 22.txt.gz [root@localhost xqw]# ls 22.txt [root@localhost xqw]# gzip -c 22.txt > /tmp/xqw/22.txt.gz [root@localhost xqw]# ls 22.txt 22.txt.gz [root@localhost xqw]#
- 我们也可以用-d -c 解压文件到指定一个目录里,且原文件不删除,并且解压后重命名一个文件名,如下:
[root@localhost xqw]# gzip -d -c /tmp/xqw/22.txt.gz > /tmp/xqw/2.txt [root@localhost xqw]# ls 22.txt 22.txt.gz 2.txt
- 用gzip压缩时,不支持压缩一个目录,压缩目录会报错.如下:
[root@localhost xqw]# mkdir 22 [root@localhost xqw]# tree . ├── 22 ├── 22.txt ├── 22.txt.gz └── 2.txt 1 directory, 3 files [root@localhost xqw]# gzip 22 gzip: 22 is a directory -- ignored 我们在xqw目录下新创建一个目录,然后压缩目录会提示报错,说22是一个目录
6.3 bzip2压缩工具
- bzip2命令的格式为 bzip2[-dz] filename,它只有-z(压缩)和-d(解压缩)二个选项.压缩级别有1~9,默认是9。压缩时,加或者不加-z选项都可以压缩文件。没有bzip2压缩工具使用命令安装:
[root@localhost xqw]# yum install -y bzip2 已加载插件:fastestmirror base | 3.6 kB 00:00:00 extras | 3.4 kB 00:00:00 ^Chttp://mirrors.tuna.tsinghua.edu.cn/centos/7.5.1804/updates/x86_64/repodata/repomd.xml: [Errno -1] Error importing repomd.xml for updates: Damaged repomd.xml file 正在尝试其它镜像。 updates | 3.4 kB 00:00:00 updates/7/x86_64/primary_db | 2.0 MB 00:00:07 Loading mirror speeds from cached hostfile * base: mirrors.aliyun.com * extras: mirrors.nju.edu.cn * updates: centos.ustc.edu.cn 正在解决依赖关系 --> 正在检查事务 ---> 软件包 bzip2.x86_64.0.1.0.6-13.el7 将被 安装 --> 解决依赖关系完成 依赖关系解决 ============================================================================================================= Package 架构 版本 源 大小 ============================================================================================================= 正在安装: bzip2 x86_64 1.0.6-13.el7 base 52 k 事务概要 ============================================================================================================= 安装 1 软件包 总下载量:52 k 安装大小:82 k Downloading packages: bzip2-1.0.6-13.el7.x86_64.rpm | 52 kB 00:00:05 Running transaction check Running transaction test Transaction test succeeded Running transaction 正在安装 : bzip2-1.0.6-13.el7.x86_64 1/1 验证中 : bzip2-1.0.6-13.el7.x86_64 1/1 已安装: bzip2.x86_64 0:1.0.6-13.el7 完毕!
- 示例如下:
[root@localhost xqw]# ls 22.txt 22.txt.gz 2.txt [root@localhost xqw]# bzip2 22.txt [root@localhost xqw]# ls 22.txt.bz2 22.txt.gz 2.txt [root@localhost xqw]# bzip2 -d 22.txt.bz2 [root@localhost xqw]# ls 22.txt 22.txt.gz 2.txt [root@localhost xqw]# bzip2 -z 22.txt [root@localhost xqw]# ls 22.txt.bz2 22.txt.gz 2.txt
- bzip2用法跟gzip用法类似,压缩时不让原文件删除掉。命令如下:
- bzip2 -c 22.txt > /tmp/xqw/22.txt.bz2
[root@localhost xqw]# bzip2 -c 22.txt > /tmp/xqw/22.txt.bz2 [root@localhost xqw]# ls 22.txt 22.txt.bz2 22.txt.gz 2.txt
- 我们同样也可以使用-d -c 命令去解压文件然后重新命名一个新文件名字。如下:
[root@localhost xqw]# bzip2 -d -c /tmp/xqw/22.txt.bz2 > 3.txt [root@localhost xqw]# ls 22.txt 22.txt.bz2 22.txt.gz 2.txt 3.txt
- 解压时也可以使用bunzip2:
[root@localhost xqw]# ls 22.txt 22.txt.gz 2.txt.bz2 3.txt [root@localhost xqw]# bunzip2 2.txt.bz2 [root@localhost xqw]# ls 22.txt 22.txt.gz 2.txt 3.txt
- 想要查看压缩文件使用 bzcat 如下:
[root@localhost xqw]# bzcat 22.txt.bz2
- 同样bzip2也不能压缩目录,压缩目录也会报错。
6.4 xz压缩工具
- xz命令的格式为xz 【-dz】filename ,和bzip2类似,常用的也是-z (压缩),-d(解压缩)这二个选项,压缩时-z加或者不加都能压缩。
- 示例如下:
[root@localhost xqw]# xz 22.txt [root@localhost xqw]# ls 22.txt.gz 22.txt.xz 2.txt 3.txt [root@localhost xqw]# du -sh 22.txt.xz 60K 22.txt.xz
- 解压缩命令如下:
[root@localhost xqw]# ls 22.txt.gz 22.txt.xz 2.txt 3.txt [root@localhost xqw]# xz -d 22.txt.xz [root@localhost xqw]# ls 22.txt 22.txt.gz 2.txt 3.txt [root@localhost xqw]#
- 同样解压也可以用unxz命令:
[root@localhost xqw]# xz 22.txt [root@localhost xqw]# ls 22.txt.gz 22.txt.xz 2.txt 3.txt [root@localhost xqw]# unxz 22.txt.xz [root@localhost xqw]# ls 22.txt 22.txt.gz 2.txt 3.txt [root@localhost xqw]#
- xz压缩也是可以支持压缩时不删除原文件的。示例如下:
[root@localhost xqw]# ls 22.txt 22.txt.gz 2.txt 3.txt [root@localhost xqw]# xz -c 22.txt > /tmp/xqw/22.txt.xz [root@localhost xqw]# ls 22.txt 22.txt.gz 22.txt.xz 2.txt 3.txt [root@localhost xqw]#
- xz压缩跟gzip bzip2 一样也是可以解压缩然后不覆盖原文件 重新命名一个文件:示例如下:
[root@localhost xqw]# ls 22.txt 22.txt.gz 2.txt 3.txt [root@localhost xqw]# xz 22.txt [root@localhost xqw]# [root@localhost xqw]# xz -c -d /tmp/xqw/22.txt.xz > /tmp/xqw/4.txt [root@localhost xqw]# ls 22.txt.gz 22.txt.xz 2.txt 3.txt 4.txt 5.txt
- 查看xz压缩的文件内容使用xzcat命令:示例如下:
[root@localhost xqw]# xzcat /tmp/xqw/22.txt.xz
6.5 zip压缩工具
- zip压缩包在windows和linux中都比较常用,它可以压缩目录和文件,压缩目录时,需要指定目录下的文件。没有zip命令需要yum安装:
[root@localhost xqw]# yum install -y zip
[root@localhost xqw]# yum install -y zip 已加载插件:fastestmirror Loading mirror speeds from cached hostfile * base: centos.ustc.edu.cn * extras: mirrors.huaweicloud.com * updates: centos.ustc.edu.cn 正在解决依赖关系 --> 正在检查事务 ---> 软件包 zip.x86_64.0.3.0-11.el7 将被 安装 --> 解决依赖关系完成 依赖关系解决 ============================================================================================================= Package 架构 版本 源 大小 ============================================================================================================= 正在安装: zip x86_64 3.0-11.el7 base 260 k 事务概要 ============================================================================================================= 安装 1 软件包 总下载量:260 k 安装大小:796 k Downloading packages: zip-3.0-11.el7.x86_64.rpm | 260 kB 00:00:06 Running transaction check Running transaction test Transaction test succeeded Running transaction 正在安装 : zip-3.0-11.el7.x86_64 1/1 验证中 : zip-3.0-11.el7.x86_64 1/1 已安装: zip.x86_64 0:3.0-11.el7 完毕!
- 我们使用zip压缩一个文件。示例如下:
[root@localhost xqw]# zip 2.txt.zip 2.txt adding: 2.txt (deflated 73%) [root@localhost xqw]# ls 22.txt.gz 22.txt.xz 2.txt 2.txt.zip 3.txt 4.txt 5.txt
zip后面先跟目标文件名(就是要压缩的压缩包名),然后在跟要压缩的文件或者目录。
- 我们举例压缩目录,首先我创建一个目录qq。
[root@localhost xqw]# mkdir qq [root@localhost xqw]# ls 22.txt.gz 22.txt.xz 2.txt 2.txt.zip 3.txt 4.txt 5.txt qq [root@localhost xqw]# zip qq.zip qq adding: qq/ (stored 0%) [root@localhost xqw]# ls 22.txt.gz 22.txt.xz 2.txt 2.txt.zip 3.txt 4.txt 5.txt qq qq.zip
小结:zip压缩文件或者目录后不删除原文件
- 如果当前目录下还有二级目录甚至更多级目录,zip命令仅仅是把二级目录本身压缩而已,如果要想一并压缩二级目录下的文件,必须加上-r选项:示例如下:
[root@localhost xqw]# mkdir /tmp/xqw/qq/qw mkdir: 无法创建目录"/tmp/xqw/qq/qw": 文件已存在 [root@localhost xqw]# tree qq/ qq/ └── qw 1 directory, 0 files [root@localhost xqw]# ls 22.txt.gz 22.txt.xz 2.txt 2.txt.zip 3.txt 4.txt 5.txt qq qq.zip [root@localhost xqw]# mkdir /tmp/xqw/qq/qw/qwe [root@localhost xqw]# mkdir /tmp/xqw/qq/qw/qwe/qwer [root@localhost xqw]# tree qq/ qq/ └── qw └── qwe └── qwer 3 directories, 0 files [root@localhost xqw]#
首先我们在qq目录下创建qw目录,然后在qw目录下创建qwe目录,然后我们在压缩qq目录看一下,
[root@localhost xqw]# zip qq.zip qq updating: qq/ (stored 0%) [root@localhost xqw]# ls 22.txt.gz 22.txt.xz 2.txt 2.txt.zip 3.txt 4.txt 5.txt qq qq.zip [root@localhost xqw]# ls qq/ qw [root@localhost xqw]# tree qq/ qq/ └── qw └── qwe └── qwer 3 directories, 0 files [root@localhost xqw]#
- 我们看到压缩qq目录只是压缩目录本身,我们想要连同qq下所有的目录都压缩用-r选项看一下效果:
[root@localhost xqw]# zip -r qq.zip qq/ updating: qq/ (stored 0%) updating: qq/qw/ (stored 0%) updating: qq/qw/qwe/ (stored 0%) updating: qq/qw/qwe/qwer/ (stored 0%)
使用-r的好处是不需要使用使用*通配了。
- 我们解压时使用的是unzip命令,没有此命令,用yum安装:
[root@localhost xqw]# yum install -y unzip
示例如下:
[root@localhost xqw]# ls 22.txt.gz 22.txt.xz 2.txt 2.txt.zip 3.txt 4.txt 5.txt qq qq.zip [root@localhost xqw]# unzip 2.txt.zip Archive: 2.txt.zip replace 2.txt? [y]es, [n]o, [A]ll, [N]one, [r]ename: n [root@localhost xqw]# unzip 2.txt.zip Archive: 2.txt.zip replace 2.txt? [y]es, [n]o, [A]ll, [N]one, [r]ename: y inflating: 2.txt
我们使用unzip解压时会提示下面一行的内容,这是因为zip压缩时没有删除原文件,但是解压缩时会问你是否要覆盖原文件,y是yes 要覆盖 n是不覆盖 A是全部覆盖,视情况自己去选择。 Archive: 2.txt.zip replace 2.txt? [y]es, [n]o, [A]ll, [N]one, [r]ename: n
[root@localhost xqw]# ls 22.txt.gz 22.txt.xz 2.txt 2.txt.zip 3.txt 4.txt 5.txt qq qq.zip
- 我们使用unzip解压时也可以指定到哪个目录下去,示例如下:
[root@localhost xqw]# unzip 2.txt.zip -d qq/ Archive: 2.txt.zip inflating: qq/2.txt [root@localhost xqw]# ls qq/ 2.txt qw [root@localhost xqw]#
- 我们在实验一下看看能不能指定解压后一个新名字。示例如下:
[root@localhost xqw]# unzip 2.txt.zip -d qq/ 33.txt Archive: 2.txt.zip caution: filename not matched: 33.txt [root@localhost xqw]# ls qq/ 2.txt qw [root@localhost xqw]#
它提示了错误,也就是说unzip不能指定解压后的名字。
- 我们可以看到zip类型的文件列表不能看文件内容:使用如下命令:
[root@localhost xqw]# unzip -l 2.txt.zip Archive: 2.txt.zip Length Date Time Name --------- ---------- ----- ---- 250633 05-26-2018 03:23 2.txt --------- ------- 250633 1 file
6.6 tar打包
tar本身是一个打包工具,可以把目录打包成一个文件,它把所有的文件整合成一个大文件,方便我们复制或者粘贴,该命令的格式为tar 【-zjxcvfpP】filename tar,它有多个选项。
- -z:表示同时用gzip压缩
- -j:表示同时用bzip2压缩
- -J:表示同时用xz压缩
- -x:表示解包或者解压缩
- -t:表示查看tar包里的文件
- -c:表示建立一个tar包或者压缩文件包
- -v:表示可视化
- -f:后面跟文件名(即-f filename,表示压缩后的文件名为filename,或者解压文件filename需要注意的是,如果是多个参数组合的情况下,请把-f 参数写到最后面)
- -p:表示使用原文件的属性,压缩前什么属性压缩后就是什么属性。(不常用)
- -P:表示可以使用绝对路径。
- --exclude filename:表示在打包或者压缩时,不要将filename文件包括在内。(不常用)
- 首先我们使用tar -cvf 命令去打包一个目录:示例如下:
[root@localhost xqw]# tar -cvf qq.tar qq qq/ qq/qw/ qq/qw/qwe/ qq/qw/qwe/qwer/ qq/2.txt [root@localhost xqw]# ls 22.txt.gz 22.txt.xz 2.txt 2.txt.zip 3.txt 4.txt 5.txt qq qq.tar qq.zip
使用tar命令打包后,原文件不会消失。
- 我们使用tar打包文件时,-v选项可加可不加:如下:
[root@localhost xqw]# ls 22.txt.gz 22.txt.xz 2.txt 2.txt.zip 3.txt 4.txt 5.txt qq qq.zip [root@localhost xqw]# cd qq/ [root@localhost qq]# ls 2.txt qw [root@localhost qq]# tar -cf 2.txt.tar 2.txt [root@localhost qq]# ls 2.txt 2.txt.tar qw [root@localhost qq]#
- 我们已经打包完事了,那么我们需要解包,使用tar -xvf 如下:
[root@localhost qq]# ls 2.txt 2.txt.tar qw [root@localhost qq]# tar -xvf 2.txt.tar 2.txt [root@localhost qq]# ls 2.txt 2.txt.tar qw [root@localhost qq]#
上面例子中,我们可以看到不管是打包还是解包都是不会删除原文件的。
- 在以后的工作中,我们会使用到--exclude 选项,下面举例说明它的用法:
[root@localhost xqw]# tar -cvf qq.tar qq qq/ qq/qw/ qq/qw/qwe/ qq/qw/qwe/qwer/ qq/qw/qw1/ qq/qw/qw2/ qq/qw/qw3/ qq/2.txt qq/qw1/ qq/qw1/qw1/ qq/qw1/qw2/ qq/qw1/qw3/ qq/qw2/ [root@localhost xqw]# tar -cvf qq.tar --exclude qw1 qq qq/ qq/qw/ qq/qw/qwe/ qq/qw/qwe/qwer/ qq/qw/qw2/ qq/qw/qw3/ qq/2.txt qq/qw2/
上面例子中,我们可以看到在打包qq目录时,下面有很多qw1目录,那么我们使用--exclude 选项之后发现,qw1目录不见了,这是因为--exclude选项是可以过滤不用的目录或者文件。
- 我们也可以使用tar -tf 查看打包的文件列表:
[root@localhost xqw]# tar tf qq.tar qq/ qq/qw/ qq/qw/qwe/ qq/qw/qwe/qwer/ qq/qw/qw2/ qq/qw/qw3/ qq/2.txt qq/qw2/ [root@localhost xqw]#
6.7 打包并压缩
tar命令非常好用的一个功能就是可以在打包时直接压缩文件,它支持gzip压缩,bzip2和xz压缩。下面我们分别来介绍不同的压缩使用选项:
- 使用-z选项,可以压缩成gzip格式的文件,如下:
[root@localhost xqw]# tar -zcvf qq.tar.gz qq qq/ qq/qw/ qq/qw/qwe/ qq/qw/qwe/qwer/ qq/qw/qw1/ qq/qw/qw2/ qq/qw/qw3/ qq/2.txt qq/qw1/ qq/qw1/qw1/ qq/qw1/qw2/ qq/qw1/qw3/ qq/qw2/ [root@localhost xqw]# ls 22.txt.gz 22.txt.xz 2.txt 2.txt.zip 3.txt 4.txt 5.txt qq qq.tar.gz qq.zip xqw.tar
- 解压时使用tar -zxvf 选项:如下:
[root@localhost xqw]# tar -zxvf qq.tar.gz qq/ qq/qw/ qq/qw/qwe/ qq/qw/qwe/qwer/ qq/qw/qw1/ qq/qw/qw2/ qq/qw/qw3/ qq/2.txt qq/qw1/ qq/qw1/qw1/ qq/qw1/qw2/ qq/qw1/qw3/ qq/qw2/ [root@localhost xqw]# ls 22.txt.gz 22.txt.xz 2.txt 2.txt.zip 3.txt 4.txt 5.txt qq qq.tar.gz qq.zip xqw.tar
- 打包时同时使用bzip2压缩,使用选项是tar -cjvf :如下:
[root@localhost xqw]# tar -jcvf qq.tar.bz2 qq qq/ qq/qw/ qq/qw/qwe/ qq/qw/qwe/qwer/ qq/qw/qw1/ qq/qw/qw2/ qq/qw/qw3/ qq/qw1/ qq/qw1/qw1/ qq/qw1/qw2/ qq/qw1/qw3/ qq/qw2/ qq/2.txt [root@localhost xqw]# ls 22.txt.gz 22.txt.xz 2.txt 2.txt.zip 3.txt 4.txt 5.txt qq qq.tar.bz2 qq.tar.gz qq.zip xqw.tar
- 解压时使用tar -xjvf 选项:命令如下:
[root@localhost xqw]# tar -jxvf qq.tar.bz2 qq/ qq/qw/ qq/qw/qwe/ qq/qw/qwe/qwer/ qq/qw/qw1/ qq/qw/qw2/ qq/qw/qw3/ qq/qw1/ qq/qw1/qw1/ qq/qw1/qw2/ qq/qw1/qw3/ qq/qw2/ qq/2.txt [root@localhost xqw]# ls 22.txt.gz 22.txt.xz 2.txt 2.txt.zip 3.txt 4.txt 5.txt qq qq.tar.bz2 qq.tar.gz qq.zip xqw.tar
- 打包时同时使用xz压缩,使用命令是tar -cJvf 。命令如下:
[root@localhost xqw]# tar -cJvf qq.tar.xz qq qq/ qq/qw/ qq/qw/qwe/ qq/qw/qwe/qwer/ qq/qw/qw1/ qq/qw/qw2/ qq/qw/qw3/ qq/qw1/ qq/qw1/qw1/ qq/qw1/qw2/ qq/qw1/qw3/ qq/qw2/ qq/2.txt [root@localhost xqw]# ls 22.txt.gz 2.txt 3.txt 5.txt qq.tar.bz2 qq.tar.xz xqw.tar 22.txt.xz 2.txt.zip 4.txt qq qq.tar.gz qq.zip
- 解压时使用tar -xJvf 选项,命令如下:
[root@localhost xqw]# tar -xJvf qq.tar.xz qq/ qq/qw/ qq/qw/qwe/ qq/qw/qwe/qwer/ qq/qw/qw1/ qq/qw/qw2/ qq/qw/qw3/ qq/qw1/ qq/qw1/qw1/ qq/qw1/qw2/ qq/qw1/qw3/ qq/qw2/ qq/2.txt [root@localhost xqw]# ls 22.txt.gz 2.txt 3.txt 5.txt qq.tar.bz2 qq.tar.xz xqw.tar 22.txt.xz 2.txt.zip 4.txt qq qq.tar.gz qq.zip [root@localhost xqw]#
注:我在使用-cvf -xvf压缩或者解压时 记不住字母的话,我有个记住字母的小技巧就是c可以理解为创建拼音(chuangjian)的首写字母c,x因为为解压,可以理解为卸载拼音(xiazai)的首写字母,个人觉得很好记,仅供参考。
拓展知识
- 利用tar 通过网络拷贝数据
# cd /data // data目录下有我们要拷贝的目标文件目录 test
# tar cvf - test| ssh 10.0.1.11 "cd /copy1/; tar xvf -" //首先将要拷贝的目录test打包,"-" 代表标准输出,然后再ssh 到目标主机 10.0.1.11 ,运行相应的命令。其中tar xvf - 意思是,将前面的标准输出内容作为解包的对象。
总结:其实就是想拷贝的目录先打包,然后将打包好的文件拷贝到目标主机,最后在目标主机上解包。只不过,我们用一条命令实现了边打包边解包的过程
Tar打包、压缩与解压缩到指定目录的方法
tar在linux上是常用的打包、压缩、加压缩工具,他的参数很多,折里仅仅列举常用的压缩与解压缩参数
参数:
-c :create 建立压缩档案的参数;
-x : 解压缩压缩档案的参数;
-z : 是否需要用gzip压缩;
-v: 压缩的过程中显示档案;
-f: 置顶文档名,在f后面立即接文件名,不能再加参数
举例: 一,将整个/home/www/images 目录下的文件全部打包为 /home/www/images.tar
[root@xoaocom ~]# tar -cvf /home/www/images.tar /home/www/images ← 仅打包,不压缩
[root@xoaocom ~]# tar -zcvf /home/www/images.tar.gz /home/www/images ← 打包后,以gzip压缩
在参数f后面的压缩文件名是自己取的,习惯上用tar来做,如果加z参数,则以tar.gz 或tgz来代表gzip压缩过的tar file文件
举例: 二,将 /home/www/images.tar.gz 解压到/home/www下面
[root@xoaocom ~]# cd /home/www
[root@xoaocom ~]# tar -zxvf /home/images.tar.gz
解压到指定的目录
[root@xoaocom ~]# tar -zxvf /home/images.tar.gz -C /specific dir
- linux下不支持解压大于4G的zip压缩包
error: Zip file too big (greater than 4294959102 bytes)
以前没有注意过,linux竟然不支持大于4G的zip文件的解压。解决办法如下:
wget -c http://packages.sw.be/p7zip/p7zip-9.13-1.el5.rf.i386.rpm
wget -c http://packages.sw.be/p7zip/p7zip-plugins-9.13-1.el5.rf.i386.rpm
下载完后安装:
rpm -ivh p7zip-9.13-1.el5.rf.i386.rpm
rpm -ivh p7zip-plugins-9.13-1.el5.rf.i386.rpm
解压命令:
7z x 123.zip