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

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

一、 tar命令的基本使用

1、常用命令选项

tar -c create 创建一个新的归档文件create a new archive
tar -t list 查看当前归档文件里面有哪些原始文件 list the contents of an archive
tar -x extract 解压当前归档文件--get extract files from an archive
tar -f 指定归档文件--use archive file or
tar -j bzip2 使用bzip去压缩归档文件 filter the archive through bzip2
tar -J 使用xz去压缩归档文件 filter the archive through xz
tar -v verbose 在运行过程中显示详细信息 verbosely list files processed
tar -z gzip 使用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 可以对目录进行压缩,也可以对文件进行压缩 。

相关文章
|
1月前
|
Ubuntu 搜索推荐 Linux
详解Ubuntu的strings与grep命令:Linux开发的实用工具。
这就是Ubuntu中的strings和grep命令,透明且强大。我希望你喜欢这个神奇的世界,并能在你的Linux开发旅程上,通过它们找到你的方向。记住,你的电脑是你的舞台,在上面你可以做任何你想做的事,只要你敢于尝试。
122 32
|
1月前
|
监控 Linux
Linux命令大全:echo与tail实现输出重定向。
这样,我们实现了使用echo和tail命令进行输出重定向的目的。在实际应用中,输出重定向技巧可节省时间,提高工作效率。希望本文内容对您了解和掌握Linux系统中echo与tail命令以及输出重定向的操作有所帮助。
102 27
|
29天前
|
消息中间件 Linux Kafka
linux命令使用消费kafka的生产者、消费者
linux命令使用消费kafka的生产者、消费者
115 16
|
1月前
|
Ubuntu Linux
"unzip"命令解析:Linux下如何处理压缩文件。
总的来说,`unzip`命令是Linux系统下一款实用而方便的ZIP格式文件处理工具。本文通过简明扼要的方式,详细介绍了在各类Linux发行版上安装 `unzip`的方法,以及如何使用 `unzip`命令进行解压、查看和测试ZIP文件。希望本文章能为用户带来实际帮助,提高日常操作的效率。
232 12
|
1月前
|
消息中间件 NoSQL Linux
Redis的基本介绍和安装方式(包括Linux和Windows版本),以及常用命令的演示
Redis(Remote Dictionary Server)是一个高性能的开源键值存储数据库。它支持字符串、列表、散列、集合等多种数据类型,具有持久化、发布/订阅等高级功能。由于其出色的性能和广泛的使用场景,Redis在应用程序中常作为高速缓存、消息队列等用途。
452 16
|
19天前
|
Linux
linux文件重命名命令
本指南介绍Linux文件重命名方法,包括单文件操作的`mv`命令和批量处理的`rename`命令。`mv`可简单更改文件名并保留扩展名,如`mv old_file.txt new_name.txt`;`rename`支持正则表达式,适用于复杂批量操作,如`rename 's/2023/2024/' *.log`。提供实用技巧如大小写转换、数字序列处理等,并提醒覆盖风险与版本差异,建议使用`-n`参数预览效果。
|
19天前
|
安全 Linux 网络安全
Linux wget 常用命令详解
wget 是一款强大的命令行下载工具,支持 HTTP/HTTPS/FTP 协议。本文详细介绍其基础用法、高效下载参数、高级应用场景及参数速查。内容涵盖断点续传(-c)、后台下载(-b)、限速下载(--limit-rate)、递归下载(-r)、整站镜像(-mk)等实用功能,同时提供文件管理、网络优化与安全下载配置方法,助您高效完成各类下载任务。
|
1月前
|
缓存 Linux 数据安全/隐私保护
Linux环境下如何通过手动调用drop_caches命令释放内存
总的来说,记录住“drop_caches” 命令并理解其含义,可以让你在日常使用Linux的过程中更加娴熟和自如。
194 23
|
3月前
|
Linux
linux命令详细说明以及案例
本文介绍了常用的 Linux 命令及其详细说明和示例,包括:`ls`(列出目录内容)、`cd`(更改目录)、`rm` 和 `mv`(删除与移动文件)、`grep`(搜索文本)、`cat`(显示文件内容)以及 `chmod`(更改文件权限)。每个命令均配有功能描述、选项说明及实际案例,帮助用户更好地掌握 Linux 命令的使用方法。
200 56
|
2月前
|
Linux 定位技术
Linux系统中的cd命令:目录切换技巧
踏过千山,越过万水,人生就是一场不断前行的旅程,总充满了未知与挑战。然而,“cd”命令如同你的旅伴,会带你穿梭在如棋盘一般的文件系统中,探索每一处未知。希望你能从“cd”命令中找到乐趣,像是掌控了一种络新妙的魔法,去向未知进发,开始你的探索之旅。
156 24