Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2420406
  • 博文数量: 384
  • 博客积分: 10193
  • 博客等级: 上将
  • 技术积分: 3785
  • 用 户 组: 普通用户
  • 注册时间: 2005-06-09 18:02
文章分类

全部博文(384)

文章存档

2011年(10)

2010年(29)

2009年(39)

2008年(36)

2007年(43)

2006年(198)

2005年(29)

分类: LINUX

2009-04-29 21:57:17

tar是linux/unix下常用的归档命令, 今天遇到了一个小问题, 如何在tar的时候不包括某一目录, 一时竟没有想起来, 记录如下:

tar可以使用--exclude=来不包括某一目录

liang@blueice2:~$ tar cvf tartest.tar tartest
tartest/                                     
tartest/introduction-dbd-oracle_files/       
tartest/introduction-dbd-oracle_files/dbd-oracle.png
tartest/introduction-dbd-oracle.html                
tartest/dklar/                                      
tartest/dklar/afs.tgz                               
tartest/dklar/debian.tgz                            
tartest/dklar/kerberos.tgz                          
tartest/dklar/ldap.tgz                              
tartest/dklar/afs.html                              
tartest/dklar/afs.xml                               
tartest/dklar/kerberos.html                         
tartest/dklar/kerberos.xml                          
tartest/dklar/ldap.html                             
tartest/dklar/ldap.xml                              
tartest/dklar/debian-basic-adm.sgml                 
tartest/dklar/debian-concepts.sgml                  
tartest/dklar/debian-first.sgml                     
tartest/dklar/debian-glossary.sgml                  
tartest/dklar/debian-hw.sgml                        
tartest/dklar/debian-overview.sgml                  
tartest/dklar/debian-prereq.sgml                    
tartest/dklar/debian-tmp.sgml                       
tartest/dklar/debian-xwin.sgml                      
tartest/dklar/debian.sgml                           
tartest/dklar/common.ent                            
tartest/dklar/DKLAR.css                             
tartest/dklar/Makefile                              
tartest/dklar/debian.html                           
tartest/dklar/yuzuki01.jpg                          
tartest/dklar/yuzuki.gif                            
tartest/dklar/yuzuki03.jpg                          
liang@blueice2:~$ rm tartest.tar                    
liang@blueice2:~$ tar cvf tartest.tar --exclude=tartest/dklar tartest
tartest/                                                             
tartest/introduction-dbd-oracle_files/                               
tartest/introduction-dbd-oracle_files/dbd-oracle.png                 
tartest/introduction-dbd-oracle.html                                 
liang@blueice2:~$ tar -tvf tartest.tar
drwxr-xr-x liang/liang       0 2009-04-29 21:31 tartest/
drwxr-xr-x liang/liang       0 2009-04-29 21:30 tartest/introduction-dbd-oracle_files/
-rw-r--r-- liang/liang    1410 2009-04-07 12:05 tartest/introduction-dbd-oracle_files/dbd-oracle.png
-rw-r--r-- liang/liang   62476 2009-04-07 12:05 tartest/introduction-dbd-oracle.html

如果有多个目录或多个文件需要不包含在tar包中, 则可以多次指定 --exclude, 如

tar cvf tartest.tar --exclude=tartest/introduction-dbd-oracle.html --exclude=tartest/introduction-dbd-oracle_files tartest                                                                                                                                          

--exclude也可以在解包时使用.

liang@blueice2:~$ tar xvf tartest.tar --exclude=tartest/dklar
tartest/
tartest/introduction-dbd-oracle_files/
tartest/introduction-dbd-oracle_files/dbd-oracle.png
tartest/introduction-dbd-oracle.html


如果从tar包中解包一个文件, 可以使用

 tar xvf tartest.tar tartest/introduction-dbd-oracle.html


在Linux中, 不论是创建还是解压tar包, 默认情况下绝对路径中的根"/"将会被去除, 如需保留(这在AIX, HPUX中是默认行为), 则可使用P参数
liang@blueice2:~$ tar cvPf tartest.tar /home/liang/tartest                                                     
/home/liang/tartest/                                                                                           
/home/liang/tartest/introduction-dbd-oracle_files/                                                             
/home/liang/tartest/introduction-dbd-oracle_files/dbd-oracle.png                                               
/home/liang/tartest/introduction-dbd-oracle.html                                                               
/home/liang/tartest/dklar/                                                                                     
/home/liang/tartest/dklar/afs.tgz                                                                              
/home/liang/tartest/dklar/debian.tgz                                                                           
/home/liang/tartest/dklar/kerberos.tgz                                                                         
/home/liang/tartest/dklar/ldap.tgz                                                                             
/home/liang/tartest/dklar/afs.html                                                                             
/home/liang/tartest/dklar/afs.xml                                                                              
/home/liang/tartest/dklar/kerberos.html                                                                        
/home/liang/tartest/dklar/kerberos.xml                                                                         
/home/liang/tartest/dklar/ldap.html                                                                            
/home/liang/tartest/dklar/ldap.xml                                                                             
/home/liang/tartest/dklar/debian-basic-adm.sgml                                                                
/home/liang/tartest/dklar/debian-concepts.sgml                                                                 
/home/liang/tartest/dklar/debian-first.sgml                                                                    
/home/liang/tartest/dklar/debian-glossary.sgml                                                                 
/home/liang/tartest/dklar/debian-hw.sgml                                                                       
/home/liang/tartest/dklar/debian-overview.sgml                                                                 
/home/liang/tartest/dklar/debian-prereq.sgml                                                                   
/home/liang/tartest/dklar/debian-tmp.sgml                                                                      
/home/liang/tartest/dklar/debian-xwin.sgml                                                                     
/home/liang/tartest/dklar/debian.sgml                                                                          
/home/liang/tartest/dklar/common.ent                                                                           
/home/liang/tartest/dklar/DKLAR.css                                                                            
/home/liang/tartest/dklar/Makefile                                                                             
/home/liang/tartest/dklar/debian.html                                                                          
/home/liang/tartest/dklar/yuzuki01.jpg                                                                         
/home/liang/tartest/dklar/yuzuki.gif                                                                           
/home/liang/tartest/dklar/yuzuki03.jpg
liang@blueice2:~$ rm -rf tartest                                                                                              
liang@blueice2:~$ tar xvf tartest.tar                                                                                          
tar: 从成员名中删除开头的“/”                                                                                                   
/home/liang/tartest/                                                                                                           
/home/liang/tartest/introduction-dbd-oracle_files/
/home/liang/tartest/introduction-dbd-oracle_files/dbd-oracle.png
/home/liang/tartest/introduction-dbd-oracle.html
/home/liang/tartest/dklar/
/home/liang/tartest/dklar/afs.tgz
/home/liang/tartest/dklar/debian.tgz
/home/liang/tartest/dklar/kerberos.tgz
/home/liang/tartest/dklar/ldap.tgz
/home/liang/tartest/dklar/afs.html
/home/liang/tartest/dklar/afs.xml
/home/liang/tartest/dklar/kerberos.html
/home/liang/tartest/dklar/kerberos.xml
/home/liang/tartest/dklar/ldap.html
/home/liang/tartest/dklar/ldap.xml
/home/liang/tartest/dklar/debian-basic-adm.sgml
/home/liang/tartest/dklar/debian-concepts.sgml
/home/liang/tartest/dklar/debian-first.sgml
/home/liang/tartest/dklar/debian-glossary.sgml
/home/liang/tartest/dklar/debian-hw.sgml
/home/liang/tartest/dklar/debian-overview.sgml
/home/liang/tartest/dklar/debian-prereq.sgml
/home/liang/tartest/dklar/debian-tmp.sgml
/home/liang/tartest/dklar/debian-xwin.sgml
/home/liang/tartest/dklar/debian.sgml
/home/liang/tartest/dklar/common.ent
/home/liang/tartest/dklar/DKLAR.css
/home/liang/tartest/dklar/Makefile
/home/liang/tartest/dklar/debian.html
/home/liang/tartest/dklar/yuzuki01.jpg
/home/liang/tartest/dklar/yuzuki.gif
/home/liang/tartest/dklar/yuzuki03.jpg
liang@blueice2:~$ ls home/liang/tartest/
dklar  introduction-dbd-oracle_files  introduction-dbd-oracle.html
liang@blueice2:~$


对于aix等操作系统, 如需要取消绝对路径中的根"/", 可以使用pax命令:

pax -s!\/!.\/! -rvf 

如需要进入某个目录再解压缩,则可指定C参数:

tar cvf tartest.tar -C home/liang/tartest/  dklar



阅读(2038) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~