Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2396993
  • 博文数量: 328
  • 博客积分: 4302
  • 博客等级: 上校
  • 技术积分: 5486
  • 用 户 组: 普通用户
  • 注册时间: 2010-07-01 11:14
个人简介

悲剧,绝对的悲剧,悲剧中的悲剧。

文章分类

全部博文(328)

文章存档

2017年(6)

2016年(18)

2015年(28)

2014年(73)

2013年(62)

2012年(58)

2011年(55)

2010年(28)

分类:

2011-01-26 14:07:34



Unzipping a File Using gunzip and tar

gunzip -c foo.tar.gz | tar xvf -
gunzip -c foo.tar.gz | tar xvf - -C /somedir

or alternatively:

gunzip < foo.tar.gz | tar xvf -

Both commands write the data produced from the gunzip command to standard out. (-c in the first example and using < in the second), using a pipe, the data is used as input in the tar command. The dash "-" represents standard input.

Zipping

Since you can unzip, you can also zip with a single command. For example, to zip the foodir directory into the foo.tar.gz archive you can use the following command:

tar cvf - foodir | gzip > foo.tar.gz

我们可以用gunzip结合tar命令从tar包里解出所有名为test的文件
gunzip -dc 2010-07-31.tar.gz |tar xvf - test.*
执行结果:
test.2010-07-31.00.192.168.1.231
test.2010-07-31.01.192.168.1.231
。。。。。


另外tar是不支持解开时用通配字符 (*,?) 来找符合项目的,除非使用 --wildchards 搭配:
tar --wildcards -zxvf 1.tar.gz "bin/a*"
bin/ash
bin/awk
bin/arch


tar命令高级应用


  tar -cvf  tar文件   源文件
 

源文件可以是*,可以是目录,可以是多个目录)
tar –czvf sqlhosts.tar  *
tar –czvf sqlhosts.tar  /.rhosts
tar –czvf sqlhosts.tar  /.rhosts  /home1/scadm/.rhosts  /home1/unoadmin/.rhosts


  tar命令无法自动生成.tar.gz扩展名,得自己加
[macg@localhost ~]$ tar -czvf wwwtar www
[macg@localhost ~]$ ls -l
drwxrwxr-x  7 macg macg    4096 Dec  1 12:46 www
-rw-rw-r--  1 macg macg 1866761 Dec  3 18:38 wwwtar


   tar三个重要参数
-f      允许指定文件名
        不带f参数,后面的文件名参数就不管用,系统会取缺省文件名
-v    Verbose , 显示每个被处理的文件名
      如无此选项,tar不报告文件信息
-z    用gzip来压缩/解压缩文件


    tar 是连文件属性一起tar和解 tar的,相当于cp –rp,所以tar是非常好的备分工具
[root@localhost tiptest]# ls -l www
total 608
-rw-r--r--  1 macg macg  6396 Nov 29 15:12 book.TXT
-rw-r--r--  1 macg macg  1161 Nov 29 19:25 car-right1.html
-rw-r--r--  1 macg macg  1096 Nov 29 15:12 car-right.html
-rw-r--r--  1 macg macg  3242 Nov 29 15:12 cc.html
-rw-r--r--  1 macg macg  3259 Nov 29 15:12 cc.html.bak
drwxr-xr-x  4 macg macg  4096 Feb  9 13:53 data
[root@localhost tiptest]# tar -cvf tip www

[root@localhost]# tar -xvf tip

[root@localhost]# ls -l www
total 608
-rw-r--r--  1 macg macg  6396 Nov 29 15:12 book.TXT
-rw-r--r--  1 macg macg  1161 Nov 29 19:25 car-right1.html
-rw-r--r--  1 macg macg  1096 Nov 29 15:12 car-right.html
-rw-r--r--  1 macg macg  3242 Nov 29 15:12 cc.html
-rw-r--r--  1 macg macg  3259 Nov 29 15:12 cc.html.bak
drwxr-xr-x  4 macg macg  4096 Feb  9 13:53 data


    Tar几个不常用的选项
-t    列出tar文件内内容
-r    追加到档案文件的未尾
      例如用户已经做好备份文件,又发现还有一个目录或是一些文件忘记备份了,这时可以使用该选项,将忘记的目录或文件追加到备份文件中
-u    更新
      相同文件会比较更新日期,修改过的文件会被tar入


       tar -tvf
[macg@localhost ~]$ tar -tvf wwwtar
drwxrwxr-x macg/macg         0 2006-12-01 12:46:07 www/
-rw-r--r-- macg/macg      6396 2006-11-29 15:12:04 www/book.TXT
-rw-r--r-- macg/macg      1096 2006-11-29 15:12:04 www/car-right.html
-rw-r--r-- macg/macg      3242 2006-11-29 15:12:04 www/cc.html
-rw-r--r-- macg/macg     58368 2006-11-29 15:12:04 www/fire2.xls
-rw-r--r-- macg/macg     25371 2006-11-29 15:12:05 www/resume.html
  

    tar -rvf 追加
$ tar -rvf mysqlt mysqltmp/testtar1.file 追加一个文件
mysqltmp/testtar1.file           

$ tar -tvf mysqlt
drwxrwxr-x macg/macg         0 2006-12-03 18:52:34 mysqltmp/
-rw-rw-r-- macg/macg      3140 2006-11-14 13:38:41 mysqltmp/pp.c
-rw-rw-r-- macg/macg      2687 2006-11-14 13:38:36 mysqltmp/pp.c~
-rw-rw-r-- macg/macg         0 2006-12-03 18:52:34 mysqltmp/testtar.file
-rw-rw-r-- macg/macg         0 2006-12-03 19:09:30 mysqltmp/testtar1.file


    tar -uvf          比较tar文件和目录中文件的最近更新时间,修改过的才tar入
   常用于定期对目录tar备分
   · 文件更新不替换旧文件

$ tar -cvf txing.tar tiptest/        建立一个tar文件
tiptest/
tiptest/tt.c
tiptest/slave1.sh
tiptest/testm.c
tiptest/ttt1

 改变目录中一文件大小
$ cd tiptest/
[macg@localhost tiptest]$ ls -l ttt1
-rw-rw-r--  1 macg macg 529 Feb  7 19:16 ttt1
$ echo hahahahahahahah >> ttt1
$ ls -l ttt1
-rw-rw-r--  1 macg macg 545 Feb  7 19:37 ttt1

 $ cd ..
$ tar -uvf txing.tar tiptest/           tar更新
tiptest/
tiptest/ttt1               -uvf 只把新文件加入
 
$ tar -tvf txing.tar         看结果,新旧两个文件都在,新文件不覆盖旧文件,
。。。
-rw-rw-r-- macg/macg       365 2006-12-22 12:56:57 tiptest/testm.c
-rw-rw-r-- macg/macg       529 2007-02-07 19:16:57 tiptest/ttt1
drwxrwxr-x macg/macg         0 2007-02-07 19:36:41 tiptest/
-rw-rw-r-- macg/macg       545 2007-02-07 19:37:28 tiptest/ttt1   

  · -utar更新,对新文件就是追加

 $ cd tiptest/
$ touch gogo
$ cd ..
$ tar -uvf txing.tar tiptest/
tiptest/
tiptest/gogo   



    追加-rvf,更新-uvf都只支持tar文件,不支持tar.gz
即使不带-z也不行,因为本身文件是被压缩过的,也打不开

    --directory  先到目录..,然后再tar
tar -zcvf test.tar.gz --directory /home
先cd /home
再tar文件,但source缺省就是当前目录(/home)
本参数主要是一种“傻瓜式”的指令,在任何路径都能执行tar

    --exclude=     不要备份所指定的档案或目录
tar -zcvpf /archive/full-backup-`date '+%d-%B-%Y'`.tar.gz --directory / --exclude=mnt --exclude=proc --exclude=var/spool
--exclude和--directory合用:除exclude外,directory下所有文件目录都tar入


tar -xzvf  tar文件   解tar
      只需要tar文件作参数


    tar时,自动把路径名的第一个/给去掉,这样解tar时,就可以在当前目录,建立目录路径
即:“tar是绝对路径,解tar是相对路径”
[root@nm ntshellhttp]# tar -cvf ntshellhttp.tar /var/www/html/* /var/www/cgi-bin/* /etc/sudoers /etc/mime.types /etc/httpd/conf/httpd.conf
tar: Removing leading `/' from member names
/var/www/html/addr.shtml
/var/www/html/banner.jpg
/var/www/cgi-bin/modiaddr
/var/www/cgi-bin/pass
/var/www/cgi-bin/power
/var/www/cgi-bin/showint
/var/www/cgi-bin/snmp
/var/www/cgi-bin/up.inc
/etc/sudoers
/etc/mime.types
/etc/httpd/conf/httpd.conf

[root@nm ntshellhttp]# tar -tvf ntshellhttp.tar
-rw-r----- apache/apache   238 2007-11-01 12:48:45 var/www/html/addr.shtml
-rw-r----- apache/apache 23782 2007-10-29 07:47:07 var/www/html/banner.jpg
-rw-r----- apache/apache   237 2007-11-01 12:46:52 var/www/html/date.shtml
-rw-r----- apache/apache   239 2007-11-01 12:46:51 var/www/html/dbinit.shtml
-rwxr----- apache/apache   134 2007-11-01 12:46:40 var/www/cgi-bin/showint
-rwxr----- apache/apache  1707 2007-11-01 12:46:40 var/www/cgi-bin/snmp
-rwxr--r-- apache/apache    49 2007-10-29 05:02:17 var/www/cgi-bin/up.inc
-r--r----- root/root       627 2007-10-31 13:42:51 etc/sudoers
-rw-r--r-- root/root     13167 2007-08-20 13:45:17 etc/mime.types
-rw-r--r-- root/root     34670 2007-11-01 12:45:37 etc/httpd/conf/httpd.conf


    因为tar的时候,去掉文件路径最前面的“/”,所以解tar不会将文件解TAR到原目录,只能将这些文件解tar到当前目录下(以当前目录为根目录,然后建立该建的目录)
所以如果想原位置覆盖,必须先 cd 到原来的目录
cd /
tar -xvf ntshellhttp.tar

tar -kx    解压时不会覆盖overwrite
把某个文件还原,在还原的过程中遇到相同的文件,不会进行覆盖
[macg@localhost ~]$ tar -kxzvf mysqlsample.tar.gz
mysqltmp/
mysqltmp/liu.c
tar: mysqltmp/liu.c: Cannot open: File exists
mysqltmp/makefile
tar: mysqltmp/makefile: Cannot open: File exists
mysqltmp/liu.o
tar: mysqltmp/liu.o: Cannot open: File exists        文件存在,就不覆盖
mysqltmp/main.c
tar: mysqltmp/main.c: Cannot open: File exists
mysqltmp/liu
tar: mysqltmp/liu: Cannot open: File exists
mysqltmp/main.o
tar: mysqltmp/main.o: Cannot open: File exists
mysqltmp/tt.c
tar: mysqltmp/tt.c: Cannot open: File exists
mysqltmp/tt
tar: mysqltmp/tt: Cannot open: File exists
tar: Error exit delayed from previous errors
如果不是刻意要覆盖原文件的话,解压最好用-kxzvf代替-xzvf,不覆盖


    解tar,如何解到正确的位置
·1。tar -t 看路径
·2。cd 当前位置到合适的路径
·3。截tar,就会覆盖
比如/root/test.tar
里面文件/etc,/usr
则:
cd /
tar -xvf /root/test.tar


    部分解tar -----------把tar文件中指定文件解出
# tar xvf backup.tar tt.txt


    部分解tar -----------从tar文件中tar出一个子目录来
# tar -tvf export.tar | grep kkk
drwxr-xr-x 102/1        0 2006 12月  7 15:54 /export/home/kkk/
-rw-r--r-- 102/1      144 2006 12月  7 15:53 /export/home/kkk/.profile
-rw-r--r-- 102/1      136 2006 12月  7 15:53 /export/home/kkk/local.cshrc
-rw-r--r-- 102/1      157 2006 12月  7 15:53 /export/home/kkk/local.login
-rw-r--r-- 102/1      174 2006 12月  7 15:53 /export/home/kkk/local.profile
-rw-r--r-- 102/1        0 2006 12月  7 15:54 /export/home/kkk/go
-rw-r--r-- 101/1        0 2006 12月  7 14:52 /export/test/kkk

# tar -xvf export.tar /export/home/kkk       
                                                                        
x /export/home/kkk, 0 bytes, 0磁带块
x /export/home/kkk/.profile, 144 bytes, 1磁带块
x /export/home/kkk/local.cshrc, 136 bytes, 1磁带块
x /export/home/kkk/local.login, 157 bytes, 1磁带块
x /export/home/kkk/local.profile, 174 bytes, 1磁带块
x /export/home/kkk/go, 0 bytes, 0磁带块


    从tar包中提取单个文件/目录的缺陷
    缺陷一:单个文件必须加全路径提取
# tar -tvf etc.tar| grep defaultrouter
-rw-r--r--   0/0       12 2006 12月  7 02:10 /etc/defaultrouter    先查-tvf
# tar -xvf etc.tar defaultrouter               
tar: 1 文件未提取   
# tar -xvf etc.tar /etc/defaultrouter
 /etc/defaultrouter, 12 bytes, 1磁带块   
    缺陷二:只能放到tar前的原始路径处(可能会覆盖),不能先提出到本地目录

 




阅读(4475) | 评论(0) | 转发(0) |
0

上一篇:磁盘概念

下一篇:Linux 用户管理

给主人留下些什么吧!~~