《linux就该这么学》笔记(6)
人们越来越倾向于传输压缩格式的文件, 压缩文件内存更小,传输的更快。付出的代价就是多了压缩和解压两个步骤。 在linux系统中tar命令就可以完成该任务。

tar [参数] [文件]

在linux系统中,常见的压缩文件格式比较多, 其中主要的是.tar或.tar.gz 或.tar.bz2。 这些格式大部分都是由tar命令生成的。
tar的使用完全依赖其参数,下面列出参数及其作用。

参数官方说明作用
-Aappend tar files to an archive将tar文件追加到文件
-ccreate a new archive创建压缩文件
-dfind differences between archive and file system查找压缩和文件系统之间的区别
–deletedelete from the archive (not on mag tapes!)从存档中删除(不是在磁带上!)
-rappend files to the end of an archive将文件追加到压缩文件的末尾
-tlist the contents of an archive列出压缩文件的内容
–test-labeltest the archive volume label and exit测试压缩文件标签并退出
-uonly append files newer than copy in archive在压缩文件中只追加比复制更新的文件
-xextract files from an archive解开压缩文件
-tlist the contents of an archive查看压缩包里有哪些文件
-zfilter the archive through gzip用Grip压缩或解压
-jfilter the archive through bzip2用bzip2压缩或解压
-Jfilter the archive through xz用xz压缩或解压
-ghandle new GNU-format incremental backup处理新的gnu格式增量备份
-Ghandle old GNU-format incremental backup处理旧的gnu格式增量备份
–ignore-failed-readdo not exit with nonzero on unreadable files在不可读的文件上不以非零退出
–leveldump level for created listed-incremental archive已创建的列表增量归档的转储级别
-narchive is seekable已创建的列表增量归档的转储级别
–no-check-devicedo not check device numbers when creating incremental archives创建增量归档时不检查设备号
-vverbosely list files processed显示解压或压缩的过程
-fuse archive file or device ARCHIVE目标文件名
-pextract information about file permissions (default for superuser)保留原始的权限与属性
-Pdon’t strip leading `/'s from file names使用绝对路径来压缩
-Cchange to directory DIR指定解压到的目录

-c 和-x不能同时使用,一个压缩一个解压。 在执行某些压缩或解压文件时,可能要花费较长时间,如果屏幕一直没有动静,您可能以为电脑死机了,所以可以用-v参数向用户不断显示压缩或解压的过程。 -C参数用于指定要解压到哪个目录。 -f参数特别重要,它必须放到参数的最后一位。
下面分别压缩和解压 test.txt文件:

[root@linuxpronbe home]# touch test.txt  
[root@linuxpronbe home]# ls
redhat  test.txt
[root@linuxpronbe home]# tar -czvf test.tar.gz  test.txt  ### 压缩
test.txt
[root@linuxpronbe home]# ls
redhat  test.tar.gz  test.txt
[root@linuxpronbe home]# rm -f test.txt  
[root@linuxpronbe home]# tar -xzvf test.tar.gz -C /home   ### 解压
test.txt
[root@linuxpronbe home]# ls
redhat  test.tar.gz  test.txt
Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐