{{ cat }}'s docs

9.2.0: 打包&压缩-扩展研究


1. 压缩命令对于相应后缀名要求严格吗?

1) gzip对后缀名严格要求".gz"

gzip t1

mv t1.gz t1.exe

# 未知的后缀名,忽略
gzip -d t1.exe
gzip: t1.exe: unknown suffix -- ignored

2) bzip可处理不规范后缀名,命名规则为"filename.out"

cat t2
line 1
line 2

bzip2 t2

mv t2.bz2 t2.exe
# 无法推测压缩文件的源文件,用filename.out替代

bzip2 -d t2.exe
bzip2: Can\'t guess original name for t2.exe -- using t2.exe.out

cat t2.exe.out
line 1
line 2

3) xz对后缀名严格要求".xz"

xz t3
mv t3.xz t3.exe
# 未知的后缀名,跳过
xz -d t3.exe
xz: t3.exe: Filename has an unknown suffix, skipping

4) zip完美支持不规范后缀名

zip t2.exe t2
  adding: t2 (deflated 7%)
# 完美支持各种后缀名,并提供选项询问如何处理解压的文件
unzip t2.exe
Archive:  t2.exe
replace t2? [y]es, [n]o, [A]ll, [N]one, [r]ename: y
  inflating: t2

2. 压缩命令对于链接文件和inode的影响

1) 压缩命令不会保留源文件的inode

ls -i pass
428213 pass

gzip pass
# 创建几个普通文件,用来占用空出来的inode
touch t1
touch t2
gzip -d pass.gz
# inode"428213"、"428214"被占用,只能分配"428215"
ls -i pass
428215 pass

2) 压缩会造成软连接失效,但解压后软连接会恢复

ln -s pass pass.sl
gzip pass
ll pass*
-rw-r--r--  1 root root  686 Dec  2 20:44 pass.gz
lrwxrwxrwx  1 root root    4 Dec  2 22:35 pass.sl -> pass
# 重新解压后软连接恢复
gzip -d pass.gz
ll pass*
-rw-r--r-- 1 root root 4605 Dec  2 20:44 pass
lrwxrwxrwx 1 root root    4 Dec  2 22:35 pass.sl -> pass

3) 当文件有硬链接时,会阻止压缩进程

ln pass pass.hl
ll -i pass*
428220 -rw-r--r-- 2 root root 4605 Dec  2 20:44 pass
428220 -rw-r--r-- 2 root root 4605 Dec  2 20:44 pass.hl
428182 lrwxrwxrwx 1 root root    4 Dec  2 22:35 pass.sl -> pass
# 提示文件拥有其他的link,未执行压缩,删除硬链接后,可以压缩
gzip pass
gzip: pass has 1 other link  -- unchanged

3. 遇到过的问题

错误信息:

# 加-P参数是为了取得绝对路径
# tar zcvf -P /tmp/rc.tar.gz /etc/rc*
tar: Removing leading '/' from member names
tar: /tmp/rc.tar.gz: Cannot stat: No such file or directory
/etc/rc0.d
......
/etc/rc.local
tar: Exiting with failure status due to previous errors

原因: f 参数后面必须直接跟包文件名称

GET新技能-tar的排错方法

# tar zcvf -P /tmp/rc.tar.gz /etc/rc* >/dev/null
tar: Removing leading '/' from member names
tar: /tmp/rc.tar.gz: Cannot stat: No such file or directory
tar: Exiting with failure status due to previous errors
直接把普通结果输出给/dev/null,然后最后就是具体错误的报出

Contents