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]# tar zxf apache-ant-1.8.1-bin.tar.gz -C /opt/apache
[root@sunrise18 setup]# tar zxf apache-tomcat-6.0.29.tar.gz -C /opt/apache
[root@sunrise18 setup]# ls /opt/apache/
apache-ant-1.8.1  apache-tomcat-6.0.29
[root@sunrise18 setup]#

 

問題思考

相關資料

【1】21Andy.com   linux解壓tar命令   
【2】鳥哥的私房菜 
tar   
【3】Bolt的專欄 
linux tar命令使用詳解   
【4】baile8410   
linux tar.gz 安裝方法

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章