posts - 262,  comments - 221,  trackbacks - 0
【1.打包与压缩】
首先我们有必要区分一下“打包”和“压缩”这两个概念。在Windows平台用久了,很容易把这两个概念混在一起。Linux下的打包仅仅是把多个文件“捆绑”成一个文件(实际上可以理解为用一个文件夹来保存这些文件),但不降低这些文件总体的磁盘占用空间。而压缩则是通过诸如空白bit压缩等方式来减少这些文件所占用的磁盘空间。

【2.gzip和zcat】
gzip和zcat命令分别用来压缩文件和读取压缩文件中的内容。其语法格式如下:
[root@www ~]# gzip [-cdtv#] 文件名
[root@www 
~]# zcat 文件名.gz

-c:将压缩的资料输出到屏幕上,可以将输出流重定向到其它地方
-d:解压缩的参数
-v:显示压缩比
-#:压缩等级,-1最快,-9最慢,预设-6

注意:使用gzip时默认会把原文件压缩成.gz文件,而原文件会被删除。如果要保持原文件的存在,则要使用参数-c和输出流重定向符>。
[root@www tmp]# gzip  -cv man.config > man.config.gz

这里我们使用了输出流重定向,将压缩后的内容重定向到当前目录下的man.config.gz,而保留源文件不变。如果需要解压文件我们也可以选择用gzip而不需要用gunzip。
[root@www tmp]# gzip -d man.config.gz

我们也可以不解压而直接查看压缩文档的内容,方法就是使用zcat命令
[root@www tmp]# zcat man.config.gz

【3.tar命令】
tar命令用于对若干文件进行打包,具体用法参考如下(参见《鸟哥的Linux私房菜》)
[root@www ~]# tar [-j|-z] [cv] [-f 建立的檔名] filename <==打包與壓縮
[root@www ~]# tar [-j|-z] [tv] [-f 建立的檔名]             <==察看檔名
[root@www ~]# tar [-j|-z] [xv] [-f 建立的檔名] [-C 目錄]   <==解壓縮
選項與參數:
-c:建立打包檔案,可搭配 -v 來察看過程中被打包的檔名(filename)
-t:察看打包檔案的內容含有哪些檔名,重點在察看『檔名』就是了;
-x:解打包或解壓縮的功能,可以搭配 -C (大寫) 在特定目錄解開
      特別留意的是, 
-c, -t, -x 不可同時出現在一串指令列中。
-j:透過 bzip2 的支援進行壓縮/解壓縮:此時檔名最好為 *.tar.bz2
-z:透過 gzip  的支援進行壓縮/解壓縮:此時檔名最好為 *.tar.gz
-v:在壓縮/解壓縮的過程中,將正在處理的檔名顯示出來
-f filename:-f 後面要立刻接要被處理的檔名!建議 -f 單獨寫一個選項囉!
-C 目錄:這個選項用在解壓縮,若要在特定目錄解壓縮,可以使用這個選項。

注意:打包后的文件名需要我们自己加后缀名区分以利于区别,tar命令并不会帮我们加后缀名

①使用tar命令压缩文件并查看信息
[root@localhost /]# tar -jcpv -f test.tar.bz2 test
test/
test/test1/
test/test1/test2/
test/test1/test2/test3/
test/test1/test2/test3/test4/

[root@localhost /]# tar -zcpv -f test.tar.gz test
test/
test/test1/
test/test1/test2/
test/test1/test2/test3/
test/test1/test2/test3/test4/

[root@localhost /]# ll test.tar.*
-rw-r--r--  
1 root root 183  2月 19 10:04 test.tar.bz2
-rw-r--r--  
1 root root 170  2月 19 10:05 test.tar.gz

[root@localhost /]# tar -jtv -f test.tar.bz2
tar: Record size 
= 8 blocks
drwxr-xr-x root/root         
0 2009-02-19 09:52:45 test/
drwxr-xr-x root/root         
0 2009-02-19 09:52:45 test/test1/
drwxr-xr-x root/root         
0 2009-02-19 09:52:45 test/test1/test2/
drwxr-xr-x root/root         
0 2009-02-19 09:52:45 test/test1/test2/test3/
drwxr-xr-x root/root         
0 2009-02-19 09:52:45 test/test1/test2/test3/test4/

 
[root@localhost /]# tar -zt -f test.tar.gz
test/
test/test1/
test/test1/test2/
test/test1/test2/test3/
test/test1/test2/test3/test4/
[root@localhost /]#

这里我们需要特别注意的是当我们使用tar [-j|-z] cpv命令压缩文件时,文件绝对路径将变成现对于当前目录的路径(也就是根路径标示符/将被移除)

②搜寻压缩包内的指定文件并解压到指定目录
[root@localhost /]# tar -jtv -f test.tar.bz2 | grep 'test4'
tar: Record size 
= 16 blocks
drwxr-xr-x root/root         
0 2009-02-19 09:52:45 test/test1/test2/test3/test4/

 
[root@localhost /]# tar -jxv -f test.tar.bz2 test/test1/test2/test3/test4
test/test1/test2/test3/test4/

在上面的例子中,我们使用tar -jtv 命令首先查看压缩包的内容,然后用管道命令|把命令的执行结果作为grep命令的搜索参数,查找文件中包含'test4'的文件。然后我们就可以在tar命令的末尾加上特定的文件名,表明我们只需要解压指定的文件。

我们也可以使用另一种方式来搜索文件
[root@localhost /]# grep 'test4' <(tar -jtvf test.tar.bz2)
tar: Record size 
= 16 blocks
drwxr-xr-x root/root         
0 2009-02-19 09:52:45 test/test1/test2/test3/test4/

③在解压缩的同时查找文件

同样的我们不单可以在查看的过程中查找,我们同样可以在解压的过程中查找(比较适合在解压后的文件一大堆,需要快速找到文件的情况)
[root@localhost /]# tar -jxv -f test.tar.bz2 | grep 'test4'
test/test1/test2/test3/test4/

[root@localhost /]# ls
bin   etc     lib         misc  proc  selinux  test     test.tar.gz  usr
boot  home    lost+found  mnt   root  srv      testdir       tmp          var
dev   initrd  media       opt   sbin  sys      test.tar.bz2  u01

④在解压时不包含某些文件或目录
[root@localhost /]# ls
bin   etc     lib         misc  proc  selinux  testdir  usr
boot  home    lost+found  mnt   root  srv      tmp      var
dev   initrd  media       opt   sbin  sys      u01

[root@localhost /]# tar -jcvf testdir.tar.bz2 testdir --exclude t1.txt \
> --exclude testdir2
testdir/
testdir/t2.txt
testdir/t3.txt

[root@localhost /]#

这里要特别注意的是:exclude前面是--,另外exclude每次只能带一个参数,如果需要剔除多个选项,要用多个--exclude组合

⑤备份在某个时间点之后更新的文件
[root@localhost /]# ls
bin   etc     lib         misc  proc  selinux  testdir          u01
boot  home    lost+found  mnt   root  srv      testdir.tar.bz2  usr
dev   initrd  media       opt   sbin  sys      tmp              var

[root@localhost /]# tar -jcpvf testdir_newer_than_090101.tar.bz2 \
--newer-mtime='2009-01-01
testdir
tar: Treating date `
2009-01-01' as 2009-01-01 00:00:00 + 0 nanoseconds
testdir/
testdir/t1.txt
testdir/t2.txt
testdir/t3.txt
testdir/testdir2/

[root@localhost /]# tar -jtvf testdir_newer_than_090101.tar.bz2
tar: Record size 
= 8 blocks
drwxr-x--x paul/root         
0 2009-02-19 14:51:49 testdir/
-rw-rw-r-- paul/paul        
21 2009-02-19 14:45:51 testdir/t1.txt
-rw-r--r-- root/root         
0 2009-02-19 11:34:21 testdir/t2.txt
-rw-r--r-- root/root         
0 2009-02-19 11:34:24 testdir/t3.txt
drwxr-xr-x root/root         
0 2009-02-19 11:34:31 testdir/testdir2/
[root@localhost /]#

--newer-mtime这个参数让我们在备份时只备份那些mtime时间比指定时间还要新的文件。如果我们只是使用了--newer,那么将比较文件的mtime和ctime。


-------------------------------------------------------------
生活就像打牌,不是要抓一手好牌,而是要尽力打好一手烂牌。
posted on 2009-02-02 23:39 Paul Lin 阅读(642) 评论(0)  编辑  收藏 所属分类: Unix / Linux

只有注册用户登录后才能发表评论。


网站导航:
 
<2009年2月>
25262728293031
1234567
891011121314
15161718192021
22232425262728
1234567

常用链接

留言簿(21)

随笔分类

随笔档案

BlogJava热点博客

好友博客

搜索

  •  

最新评论

阅读排行榜

评论排行榜