文件打包压缩——tar

Posted wang618

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了文件打包压缩——tar相关的知识,希望对你有一定的参考价值。

 

 

 

 

 


 

 

 

 

 

 

tar——压缩数据/解压数据内容

 

 

   

命令语法: tar zcvf  生成压缩包路径/压缩包.tar.gz    压缩数据01,02,03....

巧记:

压缩名称为tar.gz,可以理解为tar命令,gz取压缩类型gizp的前2个辅音字母

参数说明:
       z --- 数据压缩方式 gzip (--gzip, --gunzip, --ungzip   filter the archive through gzip)
       c --- 创建一个压缩文件(create               create a new archive)
       v --- 显示压缩过程(verbose              verbosely list files processed)
       f --- 指定压缩包路径信息
       将链接文件进行压缩处理时:
       tar zcvhf  生成压缩包路径/压缩包.tar.gz  需要进行压缩链接文件
       h --- 指定压缩链接文件所指定源文件

 

 

注意压缩的参数建议加上-,因为很多命令的参数都加-

先创建压缩包,再把文件放到里面,解压缩时磁盘和CPU都会比较卡

 

 

 

注意不要压缩软链接,但是不知道文件是否为软链接,那么就可以加上参数h

[root@centos71 ~]# tar  --help | grep   "-h"
  -h, --dereference          follow symlinks; archive and dump the files they
      --hard-dereference     follow hard links; archive and dump the files they
  -?, --help                 give this help list

 

 

 

 

因为/只有一个,所以要去掉/

[root@centos71 ~]# tar   -zcvf   /tmp/etc.tar.gz  /etc  
[root@centos71 ~]# ll -h  /tmp/etc.tar.gz 
-rw-r--r-- 1 root root 9.8M Dec 13 21:19 /tmp/etc.tar.gz
[root@centos71 ~]# du  -s  /etc/
32096    /etc/
[root@centos71 ~]# du  -sh  /etc/
32M    /etc/

 

 

 

[root@centos71 ~]# tar  -xvf  /tmp/etc.tar.gz 
[root@centos71 ~]# du  -sh  etc/
32M    etc/

 

 

 

 

 

 

 

 

进行批量压缩数据

[root@centos71 ~]# find  -type f  -name   "m*.conf"    |  xargs   tar zcvf  /test/m.tar.gz
./etc/libreport/events.d/mailx_event.conf
./etc/libreport/plugins/mantisbt.conf
./etc/libreport/plugins/mantisbt_format.conf
./etc/libreport/plugins/mantisbt_format_analyzer_libreport.conf
./etc/libreport/plugins/mantisbt_formatdup.conf
./etc/libreport/plugins/mantisbt_formatdup_analyzer_libreport.conf
./etc/libreport/plugins/mailx.conf
./etc/ld.so.conf.d/mariadb-x86_64.conf
./etc/man_db.conf
./etc/mke2fs.conf
[root@centos71 ~]# ls /test
m.tar.gz

 

 

 

[root@centos71 ~]# tar  -tf  /test/m.tar.gz 
./etc/libreport/events.d/mailx_event.conf
./etc/libreport/plugins/mantisbt.conf
./etc/libreport/plugins/mantisbt_format.conf
./etc/libreport/plugins/mantisbt_format_analyzer_libreport.conf
./etc/libreport/plugins/mantisbt_formatdup.conf
./etc/libreport/plugins/mantisbt_formatdup_analyzer_libreport.conf
./etc/libreport/plugins/mailx.conf
./etc/ld.so.conf.d/mariadb-x86_64.conf
./etc/man_db.conf
./etc/mke2fs.conf

 

 

 

 

 

 

 

[root@centos71 ~]# ls  /test
m.tar.gz
[root@centos71 ~]# find  -type f  -name   "m*.conf"     -exec tar zcvf /test/m.conf.tar.gz {} ;
./etc/libreport/events.d/mailx_event.conf
./etc/libreport/plugins/mantisbt.conf
./etc/libreport/plugins/mantisbt_format.conf
./etc/libreport/plugins/mantisbt_format_analyzer_libreport.conf
./etc/libreport/plugins/mantisbt_formatdup.conf
./etc/libreport/plugins/mantisbt_formatdup_analyzer_libreport.conf
./etc/libreport/plugins/mailx.conf
./etc/ld.so.conf.d/mariadb-x86_64.conf
./etc/man_db.conf
./etc/mke2fs.conf
[root@centos71 ~]# ls  /test
m.conf.tar.gz  m.tar.gz

 

 

 

 

[root@centos71 ~]# tar  -tf  /test/m.conf.tar.gz 
./etc/mke2fs.conf

 

 

 

 

 

[root@centos71 ~]# find  -type f  -name   "m*.conf"     -exec tar zcvf /test/m.conf.tar.gz {} +;
./etc/libreport/events.d/mailx_event.conf
./etc/libreport/plugins/mantisbt.conf
./etc/libreport/plugins/mantisbt_format.conf
./etc/libreport/plugins/mantisbt_format_analyzer_libreport.conf
./etc/libreport/plugins/mantisbt_formatdup.conf
./etc/libreport/plugins/mantisbt_formatdup_analyzer_libreport.conf
./etc/libreport/plugins/mailx.conf
./etc/ld.so.conf.d/mariadb-x86_64.conf
./etc/man_db.conf
./etc/mke2fs.conf
[root@centos71 ~]# tar  -tf  /test/m.conf.tar.gz 
./etc/libreport/events.d/mailx_event.conf
./etc/libreport/plugins/mantisbt.conf
./etc/libreport/plugins/mantisbt_format.conf
./etc/libreport/plugins/mantisbt_format_analyzer_libreport.conf
./etc/libreport/plugins/mantisbt_formatdup.conf
./etc/libreport/plugins/mantisbt_formatdup_analyzer_libreport.conf
./etc/libreport/plugins/mailx.conf
./etc/ld.so.conf.d/mariadb-x86_64.conf
./etc/man_db.conf
./etc/mke2fs.conf
[root@centos71 ~]# ls  /test
m.conf.tar.gz  m.tar.gz
[root@centos71 ~]# 

以上是关于文件打包压缩——tar的主要内容,如果未能解决你的问题,请参考以下文章

linux 系统tar文件压缩打包命令

打包压缩文件命令

tar命令 打包命令

6.5 zip压缩工具 6.6 tar打包 6.7 打包并压缩

linux命令行打包压缩及解压缩

Linux 使用 tar 命令打包压缩及解压缩文件夹