linux系统上怎么从tar包里解压单个文件

原文地址,感谢博主:http://blog.itpub.net/23249684/viewspace-1242060/

首先我创建了一个tar包:

[oracle@goolentest]$ls-l

total80084

-rw-r--r--1oracleoinstall81998360Jul3009:52test.tar.gz

我们要想解压tar包中一个文件,就需要先知道这个tar包里都有什么些文件:

[oracle@goolentest]$tar--help|more

Usage:tar[OPTION...][FILE]...

GNU`tar'savesmanyfilestogetherintoasingletapeordiskarchive,andcan

restoreindividualfilesfromthearchive.

Examples:

tar-cfarchive.tarfoobar#Createarchive.tarfromfilesfooandbar.

tar-tvfarchive.tar#Listallfilesinarchive.tarverbosely.

tar-xfarchive.tar#Extractallfilesfromarchive.tar.

-t,--listlistthecontentsofanarchive

-t参数可以列出tar包里的所有文件:

[oracle@goolentest]$tar-tvftest.tar.gz

drwxr-xr-xoracle/oinstall02014-06-2409:48home/oracle/xiaoming/bbed/

-rw-r--r--oracle/oinstall184322014-04-2509:18home/oracle/xiaoming/bbed/bifile.bbd

-rw-r--r--oracle/oinstall4022014-04-1715:35home/oracle/xiaoming/bbed/filelist.txt.bk

-rw-r--r--oracle/oinstall52510722014-04-1415:07home/oracle/xiaoming/bbed/users01.dbf

-rw-r--r--oracle/oinstall5242961922014-04-1715:45home/oracle/xiaoming/bbed/undotbs02.dbf

-rw-r--r--oracle/oinstall2072014-06-2409:48home/oracle/xiaoming/bbed/filelist.txt

-rw-r--r--oracle/oinstall672462014-06-2410:24home/oracle/xiaoming/bbed/log.bbd

-rw-r--r--oracle/oinstall472013-10-1717:52home/oracle/xiaoming/bbed/bbed.par

[oracle@goolentest]$tarztftest.tar.gz

home/oracle/xiaoming/bbed/

home/oracle/xiaoming/bbed/bifile.bbd

home/oracle/xiaoming/bbed/filelist.txt.bk

home/oracle/xiaoming/bbed/users01.dbf

home/oracle/xiaoming/bbed/undotbs02.dbf

home/oracle/xiaoming/bbed/filelist.txt

home/oracle/xiaoming/bbed/log.bbd

home/oracle/xiaoming/bbed/bbed.par

查看一下可以知道以上命令并没有实际解压tar包:

[oracle@goolentest]$ls

test.tar.gz

+++解压bbed.par这个文件

[oracle@goolentest]$tarzxfvtest.tar.gz/home/oracle/xiaoming/bbed/bbed.par

tar:/home/oracle/xiaoming/bbed/bbed.par:Notfoundinarchive

tar:Exitingwithfailurestatusduetopreviouserrors

[oracle@goolentest]$ls

test.tar.gz

[oracle@goolentest]$tarzxfvtest.tar.gzhome/oracle/xiaoming/bbed/bbed.par

home/oracle/xiaoming/bbed/bbed.par

+++注意,tar命令后面接的是-t参数list出来的文件路径,前面不要再加一个“/”,否则就报错了

查看解压出来的文件,我们可以看到,连同文件所在的路径也一起创建了,也不是把文件解压到当前目录下,连同目录一起创建应该是为了避免同名文件被覆盖的危险吧。

[oracle@goolentest]$ls

hometest.tar.gz

[oracle@goolentest]$lshome/oracle/xiaoming/bbed/bbed.par

home/oracle/xiaoming/bbed/bbed.par

相关推荐