Home | 简体中文 | 繁体中文 | 杂文 | Github | 知乎专栏 | Facebook | Linkedin | Youtube | 打赏(Donations) | About
知乎专栏

第 4 章 包管理 / 压缩 / 解压

package / compress and decompress

目录

4.1. tar — The GNU version of the tar archiving utility
4.1.1. tar examples
4.1.2. gunzip
4.1.3. b2zip
4.1.4. compress
4.1.5. .xz 文件
4.1.6. -t, --list
4.1.7. tar: Removing leading `/’ from member names
4.1.8. -C, --directory=DIR
4.1.9. --exclude
4.1.10. -T
4.1.11. 日期过滤
4.1.12. 保留权限
4.1.13. -r, --append
4.1.14. 远程传输
4.1.15. 分卷压缩
4.2. cpio - copy files to and from archives
4.3. gzip
4.4. zip, zipcloak, zipnote, zipsplit - package and compress (archive) files
4.5. bzip2, bunzip2 - a block-sorting file compressor
4.6. RAR
4.7. 7-Zip
4.7.1. 压缩
4.7.2. 浏览压缩包
4.7.3. 解压
4.7.4. Creates self extracting archive.
4.8. RAR
4.9. xz, unxz, xzcat, lzma, unlzma, lzcat - Compress or decompress .xz and .lzma files

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

4.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
			

4.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
			

4.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
			

4.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
      		

4.1.5. .xz 文件

			
tar -Jxf file.pkg.tar.xz
			
			

4.1.6. -t, --list

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

列出tar包中的文件

tar tvf neo.tar.gz
			
# mkdir -p /www/test.com/www.test.com/
# echo helloworld > /www/test.com/www.test.com/test.txt
# tar zcvf www.test.com.tar.gz /www/test.com/www.test.com/

# tar ztvf www.test.com.tar.gz
drwxr-xr-x root/root         0 2013-08-08 15:24 www/test.com/www.test.com/
-rw-r--r-- root/root        11 2013-08-08 15:24 www/test.com/www.test.com/test.txt

# tar zxvf www.test.com.tar.gz
www/test.com/www.test.com/
www/test.com/www.test.com/test.txt

# find www
www
www/test.com
www/test.com/www.test.com
www/test.com/www.test.com/test.txt
			

4.1.7. 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
			
tar zcvfP www.test.com.tar.gz /www/test.com/www.test.com/
tar zxvfP www.test.com.tar.gz
			

4.1.8. -C, --directory=DIR

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

解压到目标目录

tar -xzvf neo.tar.gz -C /tmp
			
# tar zxvf www.test.com.tar.gz -C /tmp
www/test.com/www.test.com/
www/test.com/www.test.com/test.txt

# find /tmp/www/
/tmp/www/
/tmp/www/test.com
/tmp/www/test.com/www.test.com
/tmp/www/test.com/www.test.com/test.txt



# rm -rf /www/test.com/*

# tar zxvf www.test.com.tar.gz -C /
www/test.com/www.test.com/
www/test.com/www.test.com/test.txt

# find /www/test.com/
/www/test.com/
/www/test.com/www.test.com
/www/test.com/www.test.com/test.txt
			

4.1.9. --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
			

4.1.10. -T

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

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

4.1.11. 日期过滤

打包 2010/08/01 之后的文件和目录

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

4.1.12. 保留权限

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

4.1.13. -r, --append

追加最近7天更改过的文件

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

4.1.14. 远程传输

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

4.1.15. 分卷压缩

分卷压缩一个目录:如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