Linux中tar归档命令、zip压缩、gzip压缩、bzip2压缩

tar:归档,把多个文件整和在一起,常常用在备份上面,本身不具备压缩功能,但是可以指定其他压缩功能去压缩。

目录

一、 tar命令的基本使用

1、常用命令选项

二、zip压缩命令的使用

1、常用命令选项

三、gzip压缩命令的使用

1、常用命令选项

四、bzip2压缩命令的使用

1、常用命令选项

2、bzcat命令

五、各种压缩命令的区别


一、 tar命令的基本使用

1、常用命令选项

tar -ccreate 创建一个新的归档文件create a new archive
tar -tlist 查看当前归档文件里面有哪些原始文件 list the contents of an archive
tar -xextract 解压当前归档文件--get extract files from an archive
tar -f指定归档文件--use archive file or
tar -jbzip2 使用bzip去压缩归档文件 filter the archive through bzip2
tar -J使用xz去压缩归档文件 filter the archive through xz
tar -vverbose 在运行过程中显示详细信息 verbosely list files processed
tar -zgzip 使用gzip去压缩filter the archive through gzip
tar -tf查看指定归档文件里面的内容
tar -cf是打包归档文件 
tar -xf是解压归档文件
du -sh查看文件大小 ls -alh 也可以查看文件大小信息
tar -cvzf常用的命令查看使用gzip压缩过程的详细信息

exclude=PATTERN 排除 某些文件不打包

exclude files, given as a PATTERN

tar -tf [文件名]:查看指定文件里面的内容

tar -tf cron.tar查看指定文件cron.tar里面的内容

tar -cf [打包后新文件名][需要打包的文件]:打包多个文件到归档文件

(打包新文件名在前面,后面的是进行打包的文件)

 tar -cf cron.tar cron cron-20220307 cron-20220313将后面三个文件进行归档到cron.tar中

tar -xf [文件名]:解压指定文件

cp cron.tar /tmp  备份到/tmp临时文件中
在tmp中解包文件:  tar -xf cron.tar

进行压缩:

[root@localhost log]# tar cvjf vm.tar.bz2 vmware-vmsvc.log
vmware-vmsvc.log

 -C:指定解压文件存放的目录:

root@C tmp]# tar -xf tartest.tar.gz -C /root

tar 路径问题:tar自动打包的文件路径层级关系,如果文件指定的路径是绝对路径,默认打包时路径会删除“/”。

tar命令的注意事项:​​​​​​​一定注意绝对路径和相对路径问题,推荐使用相对路径:. ..

打包和解包压缩包时,想保留绝对路径的话,都需要接一个-p选项

tar在压缩的时候:使用z或者j压缩的文件,解压缩的时候不需要加上z或者j,tar直接可以识别。

例如:1、将audit.log audit.log.bak1 audit.log.bak2 都归档打包成audit2.log.tar.gz,并且使用gzip方式进行压缩。 

[root@C log]# tar -zcvf audit2.log.tar.gz audit.log audit.log.bak1 audit.log.bak2

 2、找出/etc 下所有包含system的文件,并且将它打包成system.tar.gz,放到/tmp目录下。

[root@C tmp]# find /etc -name "*system*" -type f -exec tar -zcf /tmp/system2.tar.gz {} \;
[root@C tmp]# find /etc -name "*system*" -type f |xargs tar -zcf /tmp/system2.tar.gz 

其中的args是把前一条传入的命令作为后一条的参数使用。

在选项f之后的文件档名(system2.tar.gz {} \、system2.tar.gz )是自己取的,我们习惯上都用 .tar 来作为辨识。 如果加z选项(-zcf),则以.tar.gz或.tgz来代表gzip压缩过的tar包;如果加选项:j(-jcf),则以.tar.bz2来作为tar包名。

例如:

[root@C log]# tar -zcvf audit.log.tar.gz audit.log
audit.log
[root@C log]# du -sh audit.log.tar.gz
240K  audit.log.tar.gz
[root@C log]# tar -jcvf audit.log.tar.bz2 audit.log
audit.log
[root@C log]# du -sh audit.log.tar.gz
240K  audit.log.tar.gz
[root@C log]# du -sh audit.log.tar.bz2
156K  audit.log.tar.bz2

二、zip压缩命令的使用

格式:zip 【选项】【压缩文件路径】【源文件路径】

首先需要安装zip:yum install zip -y

源文件的路径可以有多个,也可以用通配符来表示(zip init.zip initramfs*)

[root@localhost audit]# du -sh *
4.6M    audit.log
[root@localhost audit]# zip audit.zip audit.log
  adding: audit.log (deflated 94%)
[root@localhost audit]# du -sh *
4.6M    audit.log
280K    audit.zip

1、常用命令选项

使用zip命令压缩目录,需要使用接【-r】选项

zip -r 对目录进行递归压缩

[root@localhost audit]# zip -r audit.zip audit.log
updating: audit.log (deflated 94%)
[royst@localhost audit]# ls
audit.log  audit.zip

=======unzip进行解压缩=====

先安装unzip:yum install unzip -y

unzip 【选项】【压缩文件路径】

-d :指定解压文件路径

[root@localhost audit]# cp audit.zip /tmp  先复制压缩包到/tmp中
[root@localhost tmp]# unzip audit.zip -d /lianxi/tar (指定文件解压到/lianxi/tar)

三、gzip压缩命令的使用

1、压缩命令gzip命令:直接接文件的名称,会删除原文件,只保留压缩文件。

[root@zabbix-agent-nginx tar]# ls
audit  boot_pw_log.tar.xz  host_passwd.tar.xz  hosts.zip
boot   boot.tar.gz         hosts               passwd
[root@zabbix-agent-nginx tar]# gzip hosts
[root@zabbix-agent-nginx tar]# ls
audit  boot_pw_log.tar.xz  host_passwd.tar.xz  hosts.zip
boot   boot.tar.gz         hosts.gz            passwd
原文件hosts被删除,只保留了hosts.gz压缩文件。

 2、用途:制作压缩文件、解开压缩文件

格式:gzip 【-9】文件名

1、常用命令选项

gzip -9:表示高压缩比,取值1-9,默认6.

[root@localhost tar]# gzip -9 audit.log
[root@localhost tar]# du -sh *

gzip -d:用于解压缩文件,同gunzip命令,gzip -d .gz格式的压缩文件。

[root@localhost tar]# gzip -d audit.log.gz
[root@localhost tar]# ls
audit  audit.log  boot  boot_pw_log.tar.xz  boot.tar.gz  host_passwd.tar.gz  hosts.gz  hosts.zip  passwd

gzip -c:将输出重定向到标准输出,既保留原文件又保留压缩后的文件。

[root@C log]# gzip -c vmware-network.5.log > vmware.log.gz 对vmware-network.5.log进行模拟压缩然后输出到vmware.log.gz 文件中。
[root@C log]# gzip -c vmware-network.6.log >> vmware.log.gz

 重定向:

更改原本输出路径

原本事将haha输出到屏幕,通过>重定向到文件aa

> 覆盖重定向,会覆盖原来的文件内容

>>追加重定向,只会追加在文件末尾

root@C log]# echo haha
haha
[root@C log]# echo haha > aa
[root@C log]# ls
aa 
[root@C log]# cat aa
haha
[root@C log]# echo haha2>> aa
[root@C log]# cat aa
haha
haha2
[root@C log]# echo haha3>> aa
[root@C log]# cat aa
haha
haha2
haha3
[root@C log]# echo haha4 > aa
[root@C log]# cat aa
haha4

1、只能对文件进行压缩,不能对目录进行压缩。

[root@localhost tar]# gzip boot(错误,不能对目录进行压缩)
gzip: boot is a directory -- ignored

2、gunzip 是一个使用广泛的解压缩命令,它用于解压被 gzip 压缩过的文件(扩展名为 .gz)。

gunzip audit.log.gz

四、bzip2压缩命令的使用

先安装bzip2:yum install bzip2 -y

进行压缩:

[root@localhost log]# tar cvjf vm.tar.bz2 vmware-vmsvc.log

vmware-vmsvc.log

-C指定解压缩文件的存放的目录:

root@C tmp]# tar -xf tartest.tar.gz -C /root

用途:制作压缩文件、解开压缩文件

格式:bzip2 【文件名】

[root@localhost tar]# bzip2 audit.log

1、常用命令选项

格式:bzip2 【-9】【文件名】

bzip2 -9:表示高压缩比,取值1-9,默认为6

bzip2 -d:用于解压缩文件,同bunzip2(解压缩)命令

bzip2 -d:解压.bz2格式的压缩文件

[root@localhost tar]# bzip2 -d audit.log.bz2
[root@localhost tar]# bunzip2 audit.log.bz2

bzip2 -c:将输出重定向到标准输出,保留源文件

[root@localhost tar]# bzip2 -c audit.log > audit1.log.bz2

bzip2 -k:保留原文件压缩。压缩比相对于gzip更高

[root@localhost tar]# bzip2 -k audit.log
[root@C log]# du -sh audit.log.bz2
156K  audit.log.bz2
[root@C log]# du -sh audit.logbak2.gz
240K  audit.logbak2.gz
[root@C log]# zip audit.logbak.zip audit.logbak
  adding: audit.logbak (deflated 95%)
[root@C log]# du -sh audit.logbak.zip
240K  audit.logbak.zip
[root@C log]# less audit.log.bz2
[root@C log]# bzcat audit.log.bz2

 其中less 是一个Linux命令行实用程序,用于显示文件或命令输出的内容,它一次只显示一个页面。

2、bzcat命令

bzact :用于查看bz2格式的压缩文件。

用途:查看压缩文件的内容

格式:bzcat 【压缩文件名】

[root@localhost tar]# bzcat audit.log.bz2

 xz压缩 xzcat 查看压缩 使用跟bzip2差不多。查看以xz格式的压缩文件。

五、各种压缩命令的区别

xz、 bizp2、 gzip 都是对文件进行压缩。

zip、 tar 可以对目录进行压缩,也可以对文件进行压缩 。

  • 5
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值