文件压缩和打包

这里主要看ziptar

zip适合于需要压缩并打包的场景,特别是在Windows环境下;而tar通常用于Linux/Unix系统中,适合打包大型目录,并且与不同的压缩工具搭配使用。一下是常见的用法:

zip test.zip test #zip 压缩文件名 原文件夹 (只包含文件夹test)
zip test.zip test -r #zip 压缩文件名 原文件夹 包含test文件夹中所有文件
unzip test.zip -d ziptest

tar -cf test.tar test # 压缩文件
tar -xf test.tar -C tardir # 解压缩到指定目录(不指定默认当前目录,会覆盖文件)

以下是一个实验,按照要求使用命令完成。

A. 将位于当前用户(shiyanlou)家目录下的03.tar.gz压缩文件中所有的html文件(包括所在目录)解压并解包到当前用户(shiyanlou)家目录下的Desktop目录中。
mkdir temp
tar -xzf ~/03.tar.gz -C ~/temp
find temp -name '*html'
cp temp/edit Project -r

B. 解压(注意:不解包)03.tar.gz文件到Desktop目录中。
gzip -d -c ~/03.tar.gz > ~/Desktop/03.tar

C. 不解包文件,将刚刚解压到Desktop目录中的03.tar归档文件中的blogs目录以及其下的所有文件都删除。
tar --delete -f ~/Desktop/03.tar blogs

D. 将系统中的/etc/passwd和/etc/group文件打包压缩放在Desktop目录下,并命名为users.tar.gz。
tar -czf ~/Desktop/users.tar.gz /etc/passwd /etc/group

E. 在位于当前用户(shiyanlou)家目录下的Project目录中查找过去24小时产生的,大小大于5k,后缀名为.bak的文件,打包(注意:不压缩)放在前用户(shiyanlou)家目录下Project目录下,并命名为bak.tar
find ~/Project -type f -name "*.bak" -size +5k -mtime -1 -exec tar -cf ~/Project/bak.tar {} +

F. 在Project目录中查找所有空目录并删除。
find ~/Project -type d -empty -delete

G. 在Project目录中查找所有符号链接文件,并为这些文件添加.ln的后缀名。
find ~/Project -type l | while read -r link; do
mv "$link" "${link}.ln"
done

H. 查看/etc/passwd文件,找到第3行第一个字段(字段是用:号隔开的),并在Project目录下新建一个用该字段命名的文件。
cat /etc/passwd
touch ~/Project/bin

I. 在当前用户(shiyanlou)家目录下为系统中的/etc/passwd文件建立名为passwd_hl的硬链接(位于家目录中);为/etc目录在当前用户(shiyanlou)家目录下的Desktop目录中创建一个名为etc_sl的符号链接(位于Desktop目录中,指向/etc目录)。
ln /etc/passwd ~/passwd_hl
ln -s /etc ~/Desktop/etc_sl