Linux 压缩命令(linux 压缩命令tar)
sinye56 2024-10-30 03:52 13 浏览 0 评论
linux 文件压缩命令
- .Z 使用 compress 压缩文件
- .zip 使用zip压缩文件
- .gz 使用gzip压缩文件
- .bz2 使用bzip2压缩文件
- .xz 使用xz压缩文件
- .tar 使用tar 工具打包归档,没有压缩文件
- .tar.gz 使用tar 归档 在 gz 压缩文件
- .tar.bz2 使用tar归档在bz2压缩文件
- .tar.xz 使用tar归档在xz压缩文件
其中,compress已经过时了,因为太老,个别版本的linux已经不支持了,linux下的压缩工具还是以gzip和bzip2以及后加入的xz作为主力,但是由于这些工具,最早不能压缩目录,只能针对单一文件进行压缩,所以在日常使用中,他们都是配合着tar这个打包工具,由tar把目录中的很多文件打包成一个文件,再经由对应的工具进行压缩,所以我们会看上面的那些tar.*的压缩包。好了我们先来学习下这些压缩工具如何使用
压缩文件的优点如下
- 文件更小,便于网络传输,效率更高;
- 避免杂乱,可以减少文件个数,多个文件一起压缩;
- 有些文件不能直接传输,比如安装程序,压缩后就可以传输了
压缩工具使用
compress
-d 解压压缩文件
-c 保留源文件,标准输出
-b 指定压缩率 9-16之间,值越大压缩效率越高
-f 强制解压覆盖源文件
-r 递归处理,将指定目录下的所有文件及子目录一并处理
-v 压缩统计信息
# 压缩文件
[root@ym test]# compress bigfile
[root@ym test]# ls bigfile.Z
bigfile.Z
# -d 加压缩
[root@ym test]# compress -d bigfile.Z
[root@ym test]# ls
1test.sh 2test.sh bigfile --delete file passwd test.sh
# -c 保留源文件
[root@ym test]# compress -c bigfile > bigfile.Z
[root@ym test]# ls bigfile*
bigfile bigfile.Z
# -f 强制压缩文件不管是否存在
[root@ym test]# ls bigfile*
bigfile bigfile.Z
[root@ym test]# compress -f bigfile
[root@ym test]# ls bigfile*
bigfile.Z
# -b 指定压缩率
[root@ym test]# compress -d bigfile.Z
# -r 递归压缩文件
[root@ym tmp]# compress -r ym
[root@ym tmp]# ls ym/test
1test.sh.Z 2test.sh.Z bigfile.Z --delete.Z file.Z passwd.Z
uncompress
-d 解压压缩文件
-c 保留源文件,标准输出
-b 指定压缩率 9-16之间,值越大压缩效率越高
-f 强制解压覆盖源文件
-r 递归处理,将指定目录下的所有文件及子目录一并处理
-v 压缩统计信息
gzip
[root@ym ~]# gzip -h
Usage: gzip [OPTION]... [FILE]...
-c, --stdout 保留源文件,把压缩后的文件输出到标准输出设备
-d, --decompress 解压缩文件
-f, --force 强制压缩文件,不管是什么类型的文件及是否存在
-h, --help 在线帮助
-k, --keep 保留源文件不删除文件
-l, --list 列出压缩文件的相关信息
-L, --license 显示版本与版权信息
-n, --no-name 压缩文件时,不保存原来的文件名称及时间戳记
-N, --name 压缩文件时,保存原来的文件名称及时间戳记
-q, --quiet 不显示警告信息
-r, --recursive 递归处理,将指定目录下的所有文件及子目录一并处理
-S, --suffix=SUF <压缩字尾字符串>或----suffix<压缩字尾字符串> 更改压缩字尾字符串
-t, --test 测试压缩文件是否正确无误
-v, --verbose 显示指令执行过程
-V, --version 显示版本信息
-1, --fast 此参数的效果和指定"-1"参数相同
-9, --best 此参数的效果和指定"-9"参数相同
[root@ym test]# tar -c passwd > passwd.gz # 将passwd内容通过>标准输出到 passwd.gz
[root@ym test]# ls
1test.sh 2test.sh file.gz passwd passwd.gz test.sh
[root@ym test]# gzip -d file.gz # -d 选项 将压缩文件file.gz解压成file文件
[root@ym test]# ls
1test.sh 2test.sh file passwd passwd.gz test.sh
[root@ym test]# gzip -k file # -k选项压缩文件同时保留源文件
[root@ym test]# ls
1test.sh 2test.sh file file.gz passwd passwd.gz test.sh
[root@ym test]# gzip -l file.gz # 查看压缩率信息
compressed uncompressed ratio uncompressed_name
1055 2660 61.2% file
#使用 dd 命令生成一个10M的文件
[root@ym test]# dd if=/dev/zero of=./bigfile bs=1M count=10
记录了10+0 的读入
记录了10+0 的写出
10485760 bytes (10 MB, 10 MiB) copied, 0.00384293 s, 2.7 GB/s
# 使用-9压缩率最高
[root@ym test]# gzip -9 bigfile
[root@ym test]# gzip -l bigfile.gz
compressed uncompressed ratio uncompressed_name
10216 10485760 99.9% bigfile
# 解压并使用压缩率最低重新压缩查看
[root@ym test]# gzip -d bigfile.gz
[root@ym test]# gzip -1 bigfile
[root@ym test]# gzip -l bigfile.gz
compressed uncompressed ratio uncompressed_name
45780 10485760 99.6% bigfile
[root@ym ~]# gzip -r test # 压缩目录下所有文件
[root@ym ~]# cd test
[root@ym test]# ls
1test.sh.gz 2test.sh.gz bigfile.gz file.gz passwd.gz test.sh.gz
zcat:不解压显示文件按内容
[root@ym test]# zcat 1test.sh.gz
#!/bin/sh
seq 1 5 > /tmp/test.log
#exec < /tmp/test.log
cat /tmp/test.log
while read line
do
echo $line
done
echo "ok"
bzip2
[root@ym ~]# bzip2 -h
-h --help 打印帮助信息
-d --decompress 强制解压文件
-z --compress 强制压缩文件
-k --keep 保留源文件不删除文件
-f --force 覆盖输出文件
-t --test 测试压缩文件
-c --stdout 输出到标准输出
-q --quiet 不显示警告信息
-v --verbose 显示指令执行过程
-L --license 显示版本
-V --version 显示版本信息
-s --small 使用最小内存
--fast -1 快速压缩,压缩比例最低
--best -9 压缩比例最高,压缩速度慢
[root@ym test]# bzip2 -z file # 压缩文件
[root@ym test]# ls
1test.sh 2test.sh bigfile file.bz2 passwd test.sh
[root@ym test]# bzip2 -d file.bz2 #加压文件
[root@ym test]# bzip2 -k file # 保留源文件并压缩
[root@ym test]# ls
1test.sh 2test.sh bigfile file file.bz2 passwd test.sh
# 使用 -f 压缩文件会覆盖目录下同名的文件
[root@ym test2]# ls
file file.bz2
[root@ym test2]# bzip2 -f file
[root@ym test2]# ls
file.bz2
# -c 使用管道符重定向到一个文件
[root@ym test2]# bzip2 -c file > file.bz2
[root@ym test2]# ls
file file.bz2
bzcat:不解压显示文件内容
[root@ym test2]# bzcat file.bz2
hello shell
hello python
xz
[root@ym ~]# xz --help
Usage: xz [OPTION]... [FILE]...
-z, --compress 压缩文件
-d, --decompress 解压文件
-t, --test 测试压缩文件
-l, --list 列出信息关于xz文件
-k, --keep 保留原文不删除
-f, --force 覆盖解压
-c, --stdout 将信息输出
-0 ... -9 指定压缩级别
-e, --extreme 使用大量CPU快速压缩
-T, --threads=NUM 使用多线程压缩
-q, --quiet 不显示警告信息
-v, --verbose 显示执行过程
-h, --help 查看帮助
-H, --long-help 更详细帮助信息
-V, --version 像是版本信息
[root@ym test2]# xz -z file # -z 压缩文件file位file.xz
[root@ym test2]# ls
file.bz2 file.xz
# -d 解压 file.xz 位 file
[root@ym test2]# xz -d file.xz
[root@ym test2]# ls
file file.bz2
# -f 覆盖源文件压缩
[root@ym test2]# xz -f file
[root@ym test2]# ls
file.bz2 file.xz
# -l 显示压缩文件内容
[root@ym test2]# xz -l file.xz
Strms Blocks Compressed Uncompressed Ratio Check Filename
1 1 84 B 26 B 3.231 CRC64 file.xz
# -e 会消耗大量cpu 压缩文件
[root@ym test2]# xz -e file
[root@ym test2]# ls
file.bz2.xz file.xz
# -T 指定使用的线程数解压
[root@ym test2]# xz -d file.xz
[root@ym test2]# xz -T 4 file
[root@ym test2]# ls
file.bz2.xz file.xz
# -C 压缩文件
[root@ym test2]# xz -d file.xz
[root@ym test2]# xz -c file > file.xz
[root@ym test2]# ls
file file.bz2.xz file.xz
[root@ym test2]# xz -l file.xz
Strms Blocks Compressed Uncompressed Ratio Check Filename
1 1 84 B 26 B 3.231 CRC64 file.xz
xzcat:不显示解压的前提下查看文件内容
[root@ym test2]# xzcat file.xz
hello shell
hello python
zip
-q:不显示执行过程
-v:显示执行过程
-r:递归处理,将指定目录下的所有文件和子目录一并处理
-d:从压缩文件中删除指定文件
[root@ym test2]# zip file.zip file
adding: file (deflated 12%)
[root@ym test2]# ls
file file.bz2.xz file.xz file.zip
# -q 不显示压缩过程
[root@ym test2]# zip -q file1.zip file
[root@ym test2]# ls
file file1.zip file.bz2.xz file.xz file.zip
# -r 递归压缩目录问价
zip -r test test.zip
zip error: Nothing to do! (try: zip -r test . -i test.zip)
[root@ym ~]# zip -r abc.zip test
adding: test/ (stored 0%)
adding: test/1test.sh (deflated 29%)
adding: test/2test.sh (deflated 31%)
adding: test/bigfile (deflated 100%)
adding: test/passwd (deflated 61%)
adding: test/test.sh (deflated 14%)
adding: test/file (deflated 61%)
adding: test/file.bz2 (stored 0%)
# -v 显示压缩过程
[root@ym test2]# zip -rv test.zip test
adding: test/ (in=0) (out=0) (stored 0%)
adding: test/1test.sh (in=122) (out=87) (deflated 29%)
adding: test/2test.sh (in=203) (out=141) (deflated 31%)
adding: test/bigfile . (in=10485760) (out=10190) (deflated 100%)
adding: test/file (in=2660) (out=1032) (deflated 61%)
adding: test/passwd (in=2660) (out=1032) (deflated 61%)
adding: test/test.sh (in=37) (out=32) (deflated 14%)
total bytes=10491442, compressed=12514 -> 100% savings
unzip
-c:将解压缩的结果显示到屏幕上,并对字符做适当的转换
-d:指定解压路径
-l:显示压缩文件内所包含的文件
-v:执行时显示详细的信息
-q:不显示解压过程
# -c显示解压内容到屏幕,并不解压文件按
[root@ym test2]# unzip -c test.zip
Archive: test.zip
extracting: test/
inflating: test/1test.sh
#!/bin/sh
seq 1 5 > /tmp/test.log
#exec < /tmp/test.log
cat /tmp/test.log
while read line
do
echo $line
done
echo "ok"
........省略
# -d 指定解压路径
[root@ym test2]# unzip test.zip -d /tmp/
Archive: test.zip
creating: /tmp/test/
inflating: /tmp/test/1test.sh
inflating: /tmp/test/2test.sh
inflating: /tmp/test/bigfile
inflating: /tmp/test/file
inflating: /tmp/test/passwd
inflating: /tmp/test/test.sh
# -l 显示解压信息,并不解压文件
[root@ym test2]# unzip -l test.zip
Archive: test.zip
Length Date Time Name
--------- ---------- ----- ----
0 07-08-2022 21:10 test/
122 07-04-2022 00:11 test/1test.sh
203 07-04-2022 00:17 test/2test.sh
10485760 07-08-2022 19:40 test/bigfile
2660 07-08-2022 19:30 test/file
2660 07-08-2022 18:55 test/passwd
37 07-08-2022 18:59 test/test.sh
--------- -------
10491442 7 files
[root@ym test2]# unzip -q test.zip
tar
[root@ym test2]# tar --help
tar [选项...] [FILE]...
-c: 创建归档文件
-f:使用归档文件
-z: 压缩成gzip格式的归档文件
-j:压缩成bzip2 格式归档文件
-J:压缩xz 格式归档文件
-C:指定解压文件的位置
-r: 添加文件到归档末尾
-u: --update 仅追加比归档中副本更新的文件
-t: 列出存档中文件的目录
-k: 保留源文件
-x: 解压归档文件
-P: 保留路径符号
-v: 解压详细信息
--delete:从存档中删除
# 创建tar 归档文件操作
# -c 创建归档文件 -f 使用归档文件
[root@ym test]# tar cf passwd.tar passwd
[root@ym test]# ls
1test.sh 2test.sh bigfile file passwd passwd.tar test.sh
# -rf 添加文件到归档文件中去
[root@ym test]# tar rf passwd.tar file
[root@ym test]# tar ft passwd.tar
passwd
file
# 创建tar.gz归档压缩文件
# cfz 创建归档压缩文件格式:.tar.gz
[root@ym test]# tar cfz passwd.tar.gz passwd
[root@ym test]# ls
1test.sh 2test.sh bigfile file passwd passwd.tar passwd.tar.gz test.sh
# cfj 创建归档压缩文件格式:.tar.bz
[root@ym test]# tar cfj passwd.tar.bz passwd
[root@ym test]# ls
1test.sh bigfile passwd passwd.tar.bz test.sh
2test.sh file passwd.tar passwd.tar.gz
# cfx 创建归档压缩文件:tar.xz
[root@ym test]# tar cfJ passwd.tar.xz passwd
[root@ym test]# ls passwd.tar.xz
passwd.tar.xz
# 解压归档文件并指定保存位置
[root@ym test]# tar xfz passwd.tar.gz -C /tmp/
[root@ym test]# tar xfj passwd.tar.bz -C /tmp/
[root@ym test]# tar xfJ passwd.tar.xz -C /tmp/
# --delete 从归档文件中删除指定文件
[root@ym test]# tar --delete file -f passwd.tar
[root@ym test]# tar tf passwd.tar
passwd
# -P 保留归档中的文件路径(大写)
[root@ym ~]# tar cfP test.tar /tmp/ym/test
[root@ym a]# tar tfP test.tar
/tmp/ym/test/
/tmp/ym/test/passwd.tar
/tmp/ym/test/1test.sh
/tmp/ym/test/2test.sh
/tmp/ym/test/bigfile
/tmp/ym/test/file
/tmp/ym/test/passwd
/tmp/ym/test/test.sh
/tmp/ym/test/passwd.tar.xz
/tmp/ym/test/passwd.tar.gz
/tmp/ym/test/passwd.tar.bz
/tmp/ym/test/passwd.gar.xz
/tmp/ym/test/--delete
相关推荐
- 程序员:JDK的安装与配置(完整版)_jdk的安装方法
-
对于Java程序员来说,jdk是必不陌生的一个词。但怎么安装配置jdk,对新手来说确实头疼的一件事情。我这里以jdk10为例,详细的说明讲解了jdk的安装和配置,如果有不明白的小伙伴可以评论区留言哦下...
- Linux中安装jdk并配置环境变量_linux jdk安装教程及环境变量配置
-
一、通过连接工具登录到Linux(我这里使用的Centos7.6版本)服务器连接工具有很多我就不一一介绍了今天使用比较常用的XShell工具登录成功如下:二、上传jdk安装包到Linux服务器jdk...
- 麒麟系统安装JAVA JDK教程_麒麟系统配置jdk
-
检查检查系统是否自带java在麒麟系统桌面空白处,右键“在终端打开”,打开shell对话框输入:java–version查看是否自带java及版本如图所示,系统自带OpenJDK,要先卸载自带JDK...
- 学习笔记-Linux JDK - 安装&配置
-
前提条件#检查是否存在JDKrpm-qa|grepjava#删除现存JDKyum-yremovejava*安装OracleJDK不分系统#进入安装文件目...
- Linux新手入门系列:Linux下jdk安装配置
-
本系列文章是把作者刚接触和学习Linux时候的实操记录分享出来,内容主要包括Linux入门的一些理论概念知识、Web程序、mysql数据库的简单安装部署,希望能够帮到一些初学者,少走一些弯路。注意:L...
- 测试员必备:Linux下安装JDK 1.8你必须知道的那些事
-
1.简介在Oracle收购Sun后,Java的一系列产品就被整合到Oracle官网中,打开官网乍眼一看也不知道去哪里下载,还得一个一个的摸索尝试,而且网上大多数都是一些Oracle收购Sun前,或者就...
- Linux 下安装JDK17_linux 安装jdk1.8 yum
-
一、安装环境操作系统:JDK版本:17二、安装步骤第一步:下载安装包下载Linux环境下的jdk1.8,请去官网(https://www.oracle.com/java/technologies/do...
- 在Ubuntu系统中安装JDK 17并配置环境变量教程
-
在Ubuntu系统上安装JDK17并配置环境变量是Java开发环境搭建的重要步骤。JDK17是Oracle提供的长期支持版本,广泛用于开发Java应用程序。以下是详细的步骤,帮助你在Ubuntu系...
- 如何在 Linux 上安装 Java_linux安装java的步骤
-
在桌面上拥抱Java应用程序,然后在所有桌面上运行它们。--SethKenlon(作者)无论你运行的是哪种操作系统,通常都有几种安装应用程序的方法。有时你可能会在应用程序商店中找到一个应用程序...
- Windows和Linux环境下的JDK安装教程
-
JavaDevelopmentKit(简称JDK),是Java开发的核心工具包,提供了Java应用程序的编译、运行和开发所需的各类工具和类库。它包括了JRE(JavaRuntimeEnviro...
- linux安装jdk_linux安装jdk软连接
-
JDK是啥就不用多介绍了哈,外行的人也不会进来看我的博文。依然记得读大学那会,第一次实验课就是在机房安装jdk,编写HelloWorld程序。时光飞逝啊,一下过了十多年了,挣了不少钱,买了跑车,娶了富...
- linux安装jdk,全局配置,不同用户不同jdk
-
jdk1.8安装包链接:https://pan.baidu.com/s/14qBrh6ZpLK04QS8ogCepwg提取码:09zs上传文件解压tar-zxvfjdk-8u152-linux-...
- 运维大神教你在linux下安装jdk8_linux安装jdk1.7
-
1.到官网下载适合自己机器的版本。楼主下载的是jdk-8u66-linux-i586.tar.gzhttp://www.oracle.com/technetwork/java/javase/downl...
- window和linux安装JDK1.8_linux 安装jdk1.8.tar
-
Windows安装JDK1.8的步骤:步骤1:下载JDK打开浏览器,找到JDK下载页面https://d.injdk.cn/download/oraclejdk/8在页面中找到并点击“下载...
- 最全的linux下安装JavaJDK的教程(图文详解)不会安装你来打我?
-
默认已经有了linux服务器,且有root账号首先检查一下是否已经安装过java的jdk任意位置输入命令:whichjava像我这个已经安装过了,就会提示在哪个位置,你的肯定是找不到。一般我们在...
你 发表评论:
欢迎- 一周热门
- 最近发表
- 标签列表
-
- oracle忘记用户名密码 (59)
- oracle11gr2安装教程 (55)
- mybatis调用oracle存储过程 (67)
- oracle spool的用法 (57)
- oracle asm 磁盘管理 (67)
- 前端 设计模式 (64)
- 前端面试vue (56)
- linux格式化 (55)
- linux图形界面 (62)
- linux文件压缩 (75)
- Linux设置权限 (53)
- linux服务器配置 (62)
- mysql安装linux (71)
- linux启动命令 (59)
- 查看linux磁盘 (72)
- linux用户组 (74)
- linux多线程 (70)
- linux设备驱动 (53)
- linux自启动 (59)
- linux网络命令 (55)
- linux传文件 (60)
- linux打包文件 (58)
- linux查看数据库 (61)
- linux获取ip (64)
- linux进程通信 (63)