linux文档的打包和压缩

  

一、gzip:压缩工具

gzip只能压缩文件,不可以压缩目录后面直接跟要压缩的文件就可以进行压缩。

文件压缩后默认会带一个后缀名,作用是为了区分是什么工具压缩的。

压缩和解压之后源文件都会消失。


1、压缩passwd文件

[root@ftp cheng]# gzip passwd 
[root@ftp cheng]# ls
passwd.gz


2、gzipz -d:解压文件

[root@ftp cheng]# gzip -d passwd.gz 
[root@ftp cheng]# ls
passwd


3、zcat:查看.gz压缩包的内容

   查看passwd.gz包中的文件

[root@ftp cheng]# zcat passwd.gz 
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin

二、bzip2压缩工具

    压缩和解压之后源文件也会消失,使用的后缀名是.bz2。

    bzip2 -z:压缩文件,-z不加也可以压缩,都只能压缩文件

[root@ftp cheng]# bzip2 passwd 
[root@ftp cheng]# ls
passwd.bz2


1、bzip2 -d:解压文件

[root@ftp cheng]# bzip2 -d passwd.bz2 
[root@ftp cheng]# ls
passwd


2、bzcat:查看.bz2的压缩文件中的内容

[root@ftp cheng]# bzcat passwd.bz2 
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin


三、xz压缩工具,这个工具和gzip、bzip2的用相同

    后缀名使用的是.xz。

    压缩和解压之后源文件也会消失。


1、xz压缩文件

root@ftp cheng]# xz passwd 
[root@ftp cheng]# ls
passwd.xz


2、xz -d:解压文件

[root@ftp cheng]# xz -d passwd.xz 
[root@ftp cheng]# ls
passwd


3、xzcat;查看.xz文件中的内容

[root@ftp cheng]# xzcat passwd.xz
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin

四、zip压缩工具

    用zip压缩工具压缩的文件可以在Windows中解压打开。

    zip可以压缩目录。

    zip压缩或解压文件之后源文件不会消失,就是说目标文件和源文件存在的。


1、压缩文件

语法:zip [目标文件名] [源文件]

root@ftp cheng]# zip passwd.zip passwd 
  adding: passwd (deflated 60%)
[root@ftp cheng]# ls
passwd  passwd.zip

2、压缩目录

语法:zip -r [目标文件名] [源文件]

[root@ftp cheng]# zip -r wang.zip wang
  adding: wang/ (stored 0%)
[root@ftp cheng]# ls
passwd  passwd.zip  wang  wang.zip


可以将多个文件压缩在一起

[root@ftp cheng]# zip a.zip wang passwd
  adding: wang/ (stored 0%)
  adding: passwd (deflated 60%)
[root@ftp cheng]# ls
a.zip  passwd  passwd.zip  wang  wang.zip

3、unzip:解压文件

[root@ftp cheng]# unzip wang.zip 
Archive:  wang.zip
   creating: wang/
[root@ftp cheng]# ls
passwd  passwd.zip  wang  wang.zip

4、unzip -d:指定解压路径

   把passwd.zip文件解药到/date1目录中

root@ftp cheng]# unzip -d /date1/ passwd.zip 
Archive:  passwd.zip
  inflating: /date1/passwd           
[root@ftp cheng]# ls /date1
passwd


5、unzip -l:查看.zip文件中都有哪些文件

[root@ftp cheng]# unzip -l a.zip 
Archive:  a.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
        0  03-27-2015 22:38   wang/
     1791  03-27-2015 22:09   passwd
---------                     -------
     1791                     2 files

五、tar打包工具

    tar可以把目录打包成一个文件,方便移动或者拷贝,打包的同时还可以进行压缩。

    使用语法:tar [-zjxcvfpP] 源文件

           -z:同时使用gzip压缩

           -j:同时使用bzip2压缩

           -J:同时使用xz压缩

           -x:解包或者压缩

           -t:查看tar包里面的文件

           -c:建立一个tar包或者压缩文件

           -v:可视化

           -t:后面跟文件名,压缩时跟 “-f 文件名”,意思是压缩后的文件filename,解压时跟 “-f 文件名”,意思是解压filename. 请注意,如果是多个参数组合的情况下带有 “-f”,请把 “-f” 写到最后面。

           -p: 使用原文件的属性,压缩前什么属性压缩后还什么属性。(不常用)

            -P: 可以使用绝对路径。(不常用)


1、打包

tar -cvf b.tar passwd wang
[root@ftp cheng]# tar -cvf b.tar passwd wang
passwd
wang/


2、tar tvf b.tar:查看b.tar包里的文件

root@ftp cheng]# tar tvf b.tar 
-rw-r--r-- root/root     10240 2015-03-27 23:35 passwd
drwxr-xr-x root/root         0 2015-03-27 22:38 wang/

3、解包

tar xvf b.tar
[root@ftp cheng]# tar xvf b.tar 
passwd
wang


3、打包并且使用gzip压缩

tar -zcvf a.tar.gz passwd
[root@ftp cheng]# tar -zcvf a.tat.gz passwd 
passwd
[root@ftp cheng]# ls
a.tat.gz  passwd  wang
[root@ftp cheng]#

解压

tar -zxvf a.tat.gz

[root@ftp cheng]# tar -zxvf a.tat.gz 
passwd
[root@ftp cheng]# ls
a.tat.gz  passwd  wang
[root@ftp cheng]#

4、打包并且使用bzip2压缩

tar -zcvf a.tar.bz2 passwd

[root@ftp cheng]# tar -jcvf a.tat.bz2 passwd 
passwd
[root@ftp cheng]# ls
a.tat.bz2  passwd  wang
[root@ftp cheng]#

解压

tar -jxvf a.tat.gz

[[root@ftp cheng]# tar -xvf a.tar.bz2 
passwd
[root@ftp cheng]# ls
a.tar.bz2  passwd  wang
[root@ftp cheng]#


六、--exclude的用法

1、过滤文件

   打包a目录,但a目录下有一个passwd的文件,但是我不要他,使用--exclude来过滤掉

[root@ftp wang]# tar -cvf 1.tar --exclude "passwd" a
a/
a/b/
[root@ftp wang]# tar tvf 1.tar 
drwxr-xr-x root/root         0 2015-03-28 00:14 a/
drwxr-xr-x root/root         0 2015-03-28 00:13 a/b/

2、过滤目录

[root@ftp wang]# tar -cvf 2.tar --exclude "b" a
a/
a/passwd
[root@ftp wang]# tar tvf 2.tar 
drwxr-xr-x root/root         0 2015-03-28 00:14 a/
-rw-r--r-- root/root      1791 2015-03-28 00:14 a/passwd
[root@ftp wang]#


七、查看某个命令属于那个包

    yum provides "vim"





                        小白学习笔记,有不足之处,还请大神指正

                        博客地址:http://www.aminglinux.com/bbs/thread-7671-1-1.html