Home | Mirror | Search

第 24 章 package / compress and decompress

目錄

1. tar — The GNU version of the tar archiving utility
1.1. tar examples
1.2. gunzip
1.3. b2zip
1.4. compress
1.5. -t, --list
1.6. tar: Removing leading `/’ from member names
1.7. -C, --directory=DIR
1.8. --exclude
1.9. -T
1.10. 日期過濾
1.11. 保留權限
1.12. -r, --append
1.13. 遠程傳輸
1.14. 分卷壓縮
2. cpio - copy files to and from archives
3. gzip
4. zip, zipcloak, zipnote, zipsplit - package and compress (archive) files
5. RAR
6. 7-Zip

1. tar — The GNU version of the tar archiving utility

1.1. tar examples

tar

tar -cvf foo.tar foo/
       tar contents of folder foo in foo.tar

tar -xvf foo.tar
       extract foo.tar
			

1.2. gunzip

tar -zcvf foo.tar foo/
       tar contents of folder foo in foo.tar.gz
			
tar -xvzf foo.tar.gz
       extract gzipped foo.tar.gz
			

1.3. b2zip

b2zip
tar -jcvf foo.tar.bz2 foo/
       tar contents of folder foo in foo.tar.bz2

tar -jxvf foo.tar.bz2
       extract b2zip foo.tar.bz2
			

1.4. compress

compress/uncompress
tar -Zcvf foo.tar.Z foo/
       tar contents of folder foo in foo.tar.Z

tar -Zxvf foo.tar.Z
       extract compress foo.tar.Z
      		

1.5. -t, --list

-t, --list list the contents of an archive

列出tar包中的檔案

tar tvf neo.tar.gz
			

1.6. tar: Removing leading `/’ from member names

-P, --absolute-names don't strip leading `/'s from file names

$ tar -czvPf neo.tar.gz /home/neo/

$ tar -xzvPf neo.tar.gz
			

1.7. -C, --directory=DIR

-C, --directory=DIR change to directory DIR

解壓到目標目錄

tar -xzvf neo.tar.gz -C /tmp
			

1.8. --exclude

排除neo目錄

tar --exclude /home/neo -zcvf myfile.tar.gz /home/* /etc

tar zcvf rpmbuild/SOURCES/netkiller-1.0.tar.gz ~/workspace/public_html/* --exclude .git --exclude .svn
			

1.9. -T

find . -name "*.jpg" -print >list
tar -T list -czvf picture.tar.gz

find /etc/ | tar czvf xxx1.tar.gz -T -
			

1.10. 日期過濾

打包 2010/08/01 之後的檔案和目錄

tar -N '2010/08/01' -zcvf home.tar.gz /home
			

1.11. 保留權限

tar -zxvpf /tmp/etc.tar.gz /etc
			

1.12. -r, --append

追加最近7天更改過的檔案

find / -type f -mtime -7 | xargs tar -rf weekly_incremental.tar
			

1.13. 遠程傳輸

tar -jcpvf - file | ssh remote "tar -jxpvf -"
tar -jcpvf - file.php | ssh root@172.16.3.1 "tar -jxpvf -"
			

1.14. 分卷壓縮

分卷壓縮一個目錄:如doc

在doc目錄的上次目錄

#tar cvf doc | split -b 2m (已2M大小分卷壓縮)
#cat x* > doc.tar (合成分卷壓縮包)			
			

或者

#tar czvf doc.tar.gz doc/
#tar czvfp - doc.tar.gz | split -b 5m
#cat x* > doc.tar.gz

查看壓縮包裡面的內容:

#tar -tf doc.tar
#tar -tzvf doc.tar.gz
			
comments powered by Disqus