tar打包工具的使用

zip压缩文件

zip压缩,压缩文件后原文件不会删除

[root@localhost mulu]# zip wen.txt.zip wen.txt     zip  压缩命名   要压缩的文件
  adding: wen.txt (deflated 73%)
[root@localhost mulu]# ll -h
总用量 3.7M
-rw-r--r-- 1 root root 2.9M 6月 21 15:54 wen.txt
-rw-r--r-- 1 root root 783K 6月 24 12:54 wen.txt.zip

zip 压缩目录

[root@localhost tmp]# zip -r mulu.zip 1.txt mulu/        -r指定压缩目录的选项
  adding: 1.txt (stored 0%)
  adding: mulu/ (stored 0%)
  adding: mulu/wen.txt (deflated 73%)
  adding: mulu/wen.txt.zip (stored 0%)
[root@localhost tmp]# ll -h         压缩目录和文件后
总用量 1.6M
-rw-r--r-- 1 root root 0 6月 24 12:57 1.txt
drwxr-xr-x 2 root root 40 6月 24 12:54 mulu
-rw-r--r-- 1 root root 1.6M 6月 24 12:57 mulu.zip

zip解压

[root@localhost tmp]# unzip mulu.zip 
Archive: mulu.zip     使用unzip解压时,如果原文件和目录存在的话,会提示是否覆盖
replace 1.txt? [y]es, [n]o, [A]ll, [N]one, [r]ename: 

zip不支持查看压缩的文件内的内容,但是可以查看一个压缩包里有哪些文件,使用unzip -l查看


[root@localhost tmp]# unzip -l mulu.zip 
Archive: mulu.zip
  Length Date Time Name
--------- ---------- ----- ----
        0 06-24-2018 12:57 1.txt
        0 06-24-2018 12:54 mulu/
  3017434 06-21-2018 15:54 mulu/wen.txt
   800798 06-24-2018 12:54 mulu/wen.txt.zip
--------- -------
  3818232 4 files

tar打包工具

tar -cvf 打包名称 要打包的内容
tar -xcf 包名
tar -tf 包名 查看打包文件内部的文件数量
tar -cvf 包名 --exclude 1.txt 要打包的目录/ 打包一个目录排除掉指定的文件不进行打包

[root@localhost tmp]# tar -cvf mulu.tar mulu  
mulu/
mulu/wen.txt
mulu/wen.txt.zip
[root@localhost tmp]# ll -h
总用量 5.2M
-rw-r--r-- 1 root root 0 6月 24 12:57 1.txt
drwxr-xr-x 2 root root 40 6月 24 13:00 mulu
-rw-r--r-- 1 root root 3.7M 6月 24 13:11 mulu.tar
-rw-r--r-- 1 root root 1.6M 6月 24 12:57 mulu.zip
[root@localhost tmp]# tar -xvf mulu.tar 
mulu/
mulu/wen.txt
mulu/wen.txt.zip
[root@localhost tmp]# tar -tf mulu.tar    查看打包文件内的文件信息 
mulu/
mulu/wen.txt
mulu/wen.txt.zip
[root@localhost tmp]# tar -cvf mu.tar --exclude 1.txt mulu /打包一个目录排除掉指定文件
mulu.tar
mulu.zip

打包并压缩

tar -zcf 压缩包名称 要压缩的目录/文件 以zip格式压缩
tar -z位置是指定压缩格式的,z表示为gzip,j表示为bzip2,-c压缩,-x解压

tar是在Linux中使用得非常广泛的文档打包格式。它的好处就是它只消耗非常少的CPU以及时间去打包文件,但它仅仅只是一个打包工具,并不负责压缩。下面是如何打包一个目录:

[root@localhost ]# tar -cvf archive_name.tar directory_to_compress

下面是如何解包的命令:

[root@localhost ]# tar -xvf archive_name.tar.gz

上面这个解包命令将会将文档解开在当前目录下面。当然,你也可以用这个命令来更改解包的路径:

[root@localhost ]# tar -xvf archive_name.tar -C /tmp/extract_here/

tar.gz 它在压缩时不会占用太多CPU的,而且可以得到一个非常理想的压缩率。可以使用下面的命令去压缩一个目录:

[root@localhost ]# tar -zcvf  archive_name.tar.gz  directory_to_compress

tar.gz解压缩:

扫描二维码关注公众号,回复: 1753761 查看本文章
[root@localhost ]# tar -zxvf archive_name.tar.gz

上面这个解包命令将会将文档解压在当前目录下面。当然,也可以用这个命令来更改解包的路径:

[root@localhost ]# tar -zxvf archive_name.tar.gz -C /tmp/extract_here/

tar.bz2

这种压缩格式是所有方式中压缩率最好的。当然,这也就意味着,它比前面的方式要占用更多的CPU与时间。下面的命令就是如何使用tar.bz2进行压缩。

[root@localhost ]# tar -jcvf archive_name.tar.bz2 directory_to_compress

上面这个解包命令将会将文档解开在当前目录下面。也可以用这个命令来更改解包的路径:

[root@localhost ]# tar -jxvf archive_name.tar.bz2 -C /tmp/extract_here/
下面对tar命令中一些常用重要的参数进行总结:

-c或–create 建立新的备份文件。 
-C<目的目录>或–directory=<目的目录> 切换到指定的目录。 
-f<备份文件>或–file=<备份文件> 指定备份文件。 
-j或–bzip2 以bz2的算法来压缩或者解压文件。 
-k或–keep-old-files 解开备份文件时,不覆盖已有的文件。 
-m或–modification-time 还原文件时,不变更文件的更改时间。 
-N<日期格式>或–newer=<日期时间> 只将较指定日期更新的文件保存到备份文件里。 
-r或–append 新增文件到已存在的备份文件的结尾部分。 
-t或–list 列出备份文件的内容。 
-u或–update 仅置换较备份文件内的文件更新的文件。 
-v或–verbose 显示指令执行过程。 
-w或–interactive 遭遇问题时先询问用户。 
-W或–verify 写入备份文件后,确认文件正确无误。 
-x或–extract或–get 从备份文件中还原文件。 
-z或–gzip或–ungzip 通过gzip指令处理备份文件。 
-Z或–compress或–uncompress 通过compress指令处理备份文件。

猜你喜欢

转载自blog.51cto.com/8844414/2132434