日期:2014-05-16  浏览次数:20643 次

我使用过的Linux命令之tar - 解压缩工具

我使用过的Linux命令之tar - 解压缩工具

本文链接:http://codingstandards.iteye.com/blog/833203 ? (转载请注明出处)

用途说明

tar命令是常用的备份、压缩、解压命令(The GNU version of the tar archiving utility)。我们经常把文件或目录打包压缩到tar.gz文件中,许多开源软件的发布形式就是tar.gz或者tar.bz2包,因此在Linux下工作,必须掌握tar这个工具的使用。

?

常用参数

参数: -v

显示处理过程,可以加在下面任何一种格式中。

?

格式:tar cf file.tar files

将一个或多个文件、目录打包到tar包。

?

格式:tar rf file.tar file

将文件file添加到tar包中。

?

格式:tar uf file.tar file

将文件file更新tar包中相应的文件。

?

格式:tar tf file.tar

测试tar文件的完整性。

?

格式:tar xf file.tar

解压tar包。

?

格式:tar zcf file.tar.gz files

格式:tar zcf file.tgz files

将一个或多个文件、目录压缩到file.tar.gz中。

?

格式:tar zxf file.tar.gz

格式:tar zxf file.tgz

解压.tar.gz或.tgz文件。

?

格式:tar jcf file.tar.bz2 files

将一个或多个文件、目录压缩到file.tar.bz2中。

?

格式:tar jxf file.tar.bz2

解压.tar.bz2文件。

?

格式:tar Zcf file.tar.Z files

将一个或多个文件、目录压缩到file.tar.Z中。需要安装了ncompress软件包才行。

?

格式:tar Zxf file.tar.Z

解压.tar.Z文件。

?

参数:-C dir

解压时指定输出目录。

?

使用示例

示例一 备份目录到tar.gz压缩包中

[root@web work11]# tar zcf surf.20101203a.tar.gz surf
[root@web work11]# tar tvf surf.20101203a.tar.gz
drwxr-xr-x root/root???????? 0 2010-07-01 09:58:33 surf/
drwxr-xr-x root/root???????? 0 2010-07-01 10:06:41 surf/run/
-rw-r--r-- root/root??????? 27 2010-11-17 15:15:52 surf/run/surf.run
-rw-r--r-- root/root????? 1827 2010-08-01 20:17:56 surf/run/surf.cfg
drwxr-xr-x root/root???????? 0 2010-07-01 09:58:33 surf/run/CVS/
-rw-r--r-- root/root??????? 33 2010-07-01 09:58:33 surf/run/CVS/Root
-rw-r--r-- root/root???????? 2 2010-07-01 09:58:33 surf/run/CVS/Entries
-rw-r--r-- root/root???????? 9 2010-07-01 09:58:33 surf/run/CVS/Repository
drwxr-xr-x root/root???????? 0 2010-11-28 21:37:34 surf/src/
-rw-r--r-- root/root?????? 900 2006-07-21 11:44:42 surf/src/s_run.c
-rw-r--r-- root/root????? 8342 2006-09-28 17:04:51 surf/src/s_tcpgum.c
-rw-r--r-- root/root???? 33170 2008-09-09 10:13:15 surf/src/s_http.c

省略输出

[root@web work11]#

?

示例二 演示rlwrap的安装步骤

[root@web setup]# ls rlwrap-0.30.tar.gz -l
-rw-r--r-- 1 root root 184037 07-07 16:40 rlwrap-0.30.tar.gz
[root@web setup]# tar zxf rlwrap-0.30.tar.gz
[root@web setup]# cd rlwrap-0.30/
[root@web rlwrap-0.30]# ls
aclocal.m4? BUGS?????? completions? configure???? COPYING?????? doc????? Makefile.am? NEWS??? src?? TODO
AUTHORS???? ChangeLog? config.h.in? configure.ac? distribution? INSTALL? Makefile.in? README? test? tools
[root@web rlwrap-0.30]# ./configure --prefix=/usr

[root@web rlwrap-0.30]# make && make install

[root@web rlwrap-0.30]#

?

示例三 将ant和tomcat解压到/opt/apache/

[root@sunrise18 setup]# ls
apache-ant-1.8.1-bin.tar.gz? apache-tomcat-6.0.29.tar.gz? jdk-6u23-linux-i586-rpm.bin
[root@sunrise18 setup]# tar -C /opt/apache zxf apache-ant-1.8.1-bin.tar.gz
tar: You must specify one of the `-Acdtrux' options
Try `tar --help' for more information.
[root@sunrise18 setup]# tar zxf apache-ant-1.8.1-bin.tar.gz -C /opt/apache
tar: /opt/apache: Cannot chdir: No such file or directory
tar: Error is not recoverable: exiting now
[root@sunrise18 setup]# mkdir /opt/apache
[root@sunrise18 setup]#