首先我创建了一个tar包:
[oracle@goolen test]$ ls -l
total 80084
-rw-r--r-- 1 oracle oinstall 81998360 Jul 30 09:52 test.tar.gz
我们要想解压tar包中一个文件,就需要先知道这个tar包里都有什么些文件:
[oracle@goolen test]$ tar --help | more
Usage: tar [OPTION...] [FILE]...
GNU `tar' saves many files together into a single tape or disk archive, and can
restore individual files from the archive.
Examples:
tar -cf archive.tar foo bar # Create archive.tar from files foo and bar.
tar -tvf archive.tar # List all files in archive.tar verbosely.
tar -xf archive.tar # Extract all files from archive.tar.
-t, --list list the contents of an archive
-t参数可以列出tar包里的所有文件:
[oracle@goolen test]$ tar -tvf test.tar.gz
drwxr-xr-x oracle/oinstall 0 2014-06-24 09:48 home/oracle/xiaoming/bbed/
-rw-r--r-- oracle/oinstall 18432 2014-04-25 09:18 home/oracle/xiaoming/bbed/bifile.bbd
-rw-r--r-- oracle/oinstall 402 2014-04-17 15:35 home/oracle/xiaoming/bbed/filelist.txt.bk
-rw-r--r-- oracle/oinstall 5251072 2014-04-14 15:07 home/oracle/xiaoming/bbed/users01.dbf
-rw-r--r-- oracle/oinstall 524296192 2014-04-17 15:45 home/oracle/xiaoming/bbed/undotbs02.dbf
-rw-r--r-- oracle/oinstall 207 2014-06-24 09:48 home/oracle/xiaoming/bbed/filelist.txt
-rw-r--r-- oracle/oinstall 67246 2014-06-24 10:24 home/oracle/xiaoming/bbed/log.bbd
-rw-r--r-- oracle/oinstall 47 2013-10-17 17:52 home/oracle/xiaoming/bbed/bbed.par
[oracle@goolen test]$ tar ztf test.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@goolen test]$ ls
test.tar.gz
+++解压bbed.par这个文件
[oracle@goolen test]$ tar zxfv test.tar.gz /home/oracle/xiaoming/bbed/bbed.par
tar: /home/oracle/xiaoming/bbed/bbed.par: Not found in archive
tar: Exiting with failure status due to previous errors
[oracle@goolen test]$ ls
test.tar.gz
[oracle@goolen test]$ tar zxfv test.tar.gz home/oracle/xiaoming/bbed/bbed.par
home/oracle/xiaoming/bbed/bbed.par
+++注意,tar命令后面接的是-t参数list出来的文件路径,前面不要再加一个“/”,否则就报错了
查看解压出来的文件,我们可以看到,连同文件所在的路径也一起创建了,也不是把文件解压到当前目录下,连同目录一起创建应该是为了避免同名文件被覆盖的危险吧。
[oracle@goolen test]$ ls
home test.tar.gz
[oracle@goolen test]$ ls home/oracle/xiaoming/bbed/bbed.par
home/oracle/xiaoming/bbed/bbed.par
更多 linux系统上怎么从tar包里解压单个文件 相关文章请关注PHP中文网!