概念
tar
tar 是一种打包的工具。将多个(或者一个,如果你够无聊的话)文件/文件夹打包成为一个文件(使用f
选项指定输出文件, 否则输出到标准输出)。
- 打包一个文件夹
user@linux-xmq3:~/tmp> ll
total 0
drwxr-xr-x 8 lguo users 129 Jan 28 16:06 original
# 将original文件夹打包成为packet文件
lguo@linux-xmq3:~/tmp> tar -cf packet original/
lguo@linux-xmq3:~/tmp> ll
total 520
drwxr-xr-x 8 lguo users 129 Jan 28 16:06 original
# 由于packet包里面多了些包描述信息,所以大小大了很多
-rw-r--r-- 1 lguo users 532480 Feb 4 16:02 packet
user@linux-xmq3:~/tmp>
- 打包一个文件并输出到标准输出
lguo@linux-xmq3:~/tmp> echo Hello >test
lguo@linux-xmq3:~/tmp> tar -c test
./PaxHeaders.25766/test0000644000000000000000000000013212654603237011673 xustar0030 mtime=1454573215.720304654
30 atime=1454573215.720304654
30 ctime=1454573215.720304654
test0000644000175000001440000000000612654603237012151 0ustar00lguousers00000000000000Hello
#可以看到 , 包含了文件内容,用户名,文件名等信息, 应该是遵守某种协议
- 打开一个包, 释放文件。
lguo@linux-xmq3:~/tmp>tar -xf packet
gzip
gzip 是一个压缩工具
- 压缩 gzip 将XX文件压缩成为XX.gz
lguo@linux-xmq3:~/tmp> ls -all
total 1048
drwxr-xr-x 3 lguo users 77 Feb 4 16:23 .
drwxr-xr-x 49 lguo users 4096 Feb 4 16:16 ..
drwxr-xr-x 8 lguo users 129 Jan 28 16:06 original
-rw-r--r-- 1 lguo users 532480 Feb 4 16:02 packet
-rw-r--r-- 1 lguo users 532480 Feb 4 16:05 packet.tar
-rw-r--r-- 1 lguo users 0 Feb 4 16:09 tar
-rw-r--r-- 1 lguo users 6 Feb 4 16:06 test
lguo@linux-xmq3:~/tmp> gzip packet.tar
lguo@linux-xmq3:~/tmp> ls -all
total 864
drwxr-xr-x 3 lguo users 80 Feb 4 16:24 .
drwxr-xr-x 49 lguo users 4096 Feb 4 16:16 ..
drwxr-xr-x 8 lguo users 129 Jan 28 16:06 original
-rw-r--r-- 1 lguo users 532480 Feb 4 16:02 packet
-rw-r--r-- 1 lguo users 343509 Feb 4 16:05 packet.tar.gz
-rw-r--r-- 1 lguo users 0 Feb 4 16:09 tar
-rw-r--r-- 1 lguo users 6 Feb 4 16:06 test
- 解压
lguo@linux-xmq3:~/tmp> ls -all
total 864
drwxr-xr-x 3 lguo users 80 Feb 4 16:24 .
drwxr-xr-x 49 lguo users 4096 Feb 4 16:16 ..
drwxr-xr-x 8 lguo users 129 Jan 28 16:06 original
-rw-r--r-- 1 lguo users 532480 Feb 4 16:02 packet
-rw-r--r-- 1 lguo users 343509 Feb 4 16:05 packet.tar.gz
-rw-r--r-- 1 lguo users 0 Feb 4 16:09 tar
-rw-r--r-- 1 lguo users 6 Feb 4 16:06 test
lguo@linux-xmq3:~/tmp> gzip -d packet.tar.gz
lguo@linux-xmq3:~/tmp> ls -all
total 1048
drwxr-xr-x 3 lguo users 77 Feb 4 16:26 .
drwxr-xr-x 49 lguo users 4096 Feb 4 16:16 ..
drwxr-xr-x 8 lguo users 129 Jan 28 16:06 original
-rw-r--r-- 1 lguo users 532480 Feb 4 16:02 packet
-rw-r--r-- 1 lguo users 532480 Feb 4 16:05 packet.tar
-rw-r--r-- 1 lguo users 0 Feb 4 16:09 tar
-rw-r--r-- 1 lguo users 6 Feb 4 16:06 test
bzip2
- 压缩解压
lguo@linux-xmq3:~/tmp> ls -all
total 1048
drwxr-xr-x 3 lguo users 77 Feb 4 16:26 .
drwxr-xr-x 49 lguo users 4096 Feb 4 16:16 ..
drwxr-xr-x 8 lguo users 129 Jan 28 16:06 original
-rw-r--r-- 1 lguo users 532480 Feb 4 16:02 packet
-rw-r--r-- 1 lguo users 532480 Feb 4 16:05 packet.tar
-rw-r--r-- 1 lguo users 0 Feb 4 16:09 tar
-rw-r--r-- 1 lguo users 6 Feb 4 16:06 test
lguo@linux-xmq3:~/tmp> bzip2 packet.tar
lguo@linux-xmq3:~/tmp> ls -all
total 868
drwxr-xr-x 3 lguo users 81 Feb 4 16:28 .
drwxr-xr-x 49 lguo users 4096 Feb 4 16:16 ..
drwxr-xr-x 8 lguo users 129 Jan 28 16:06 original
-rw-r--r-- 1 lguo users 532480 Feb 4 16:02 packet
-rw-r--r-- 1 lguo users 347079 Feb 4 16:05 packet.tar.bz2
-rw-r--r-- 1 lguo users 0 Feb 4 16:09 tar
-rw-r--r-- 1 lguo users 6 Feb 4 16:06 test
lguo@linux-xmq3:~/tmp> bzip2 -d packet.tar.bz2
lguo@linux-xmq3:~/tmp> ls -all
total 1048
drwxr-xr-x 3 lguo users 77 Feb 4 16:28 .
drwxr-xr-x 49 lguo users 4096 Feb 4 16:16 ..
drwxr-xr-x 8 lguo users 129 Jan 28 16:06 original
-rw-r--r-- 1 lguo users 532480 Feb 4 16:02 packet
-rw-r--r-- 1 lguo users 532480 Feb 4 16:05 packet.tar
-rw-r--r-- 1 lguo users 0 Feb 4 16:09 tar
-rw-r--r-- 1 lguo users 6 Feb 4 16:06 test
tar 命令详解
信息来自 tar –help
基本工作模式
- -A, –catenate, –concatenate append tar files to an archive
- 追加一个tar文件到档案(tar文件)中
- -c, –create create a new archive
- 创建一个新的档案
- -d, –diff, –compare find differences between archive and file system
- 查看档案和文件系统中的不同
- –delete delete from the archive (not on mag tapes!)
- 从档案中删除
- -r, –append append files to the end of an archive
- 追加一个文件到档案中
- -t, –list list the contents of an archive
- 列出档案的内容
- –test-label test the archive volume label and exit
- 检测档案是否是合法的tar文件
- -u, –update only append files newer than copy in archive
- 追加有更新的文件到档案中
- -x, –extract, –get extract files from an archive
- 解析出具体文件
选择压缩工具
- -a, –auto-compress use archive suffix to determine the compression program
- -I, –use-compress-program=PROG
filter through PROG (must accept -d) - -j, –bzip2 filter the archive through bzip2
- -J, –xz filter the archive through xz
- –lzip filter the archive through lzip
- –lzma filter the archive through lzma
- –lzop
- –no-auto-compress do not use archive suffix to determine the compression program
- -z, –gzip, –gunzip, –ungzip filter the archive through gzip
- -Z, –compress, –uncompress filter the archive through compress
常用
- *.tar 用 tar –xvf 解压
- *.gz 用 gzip -d或者gunzip 解压
- *.tar.gz用 tar –xzf 解压
- *.bz2 用 bzip2 -d或者用bunzip2 解压
- *.tar.bz2用tar –xjf 解压
- *.Z 用 uncompress 解压
- *.tar.Z 用tar –xZf 解压
- *.rar 用 unrar e解压
- *.zip 用 unzip 解压