首页 > 系统相关 >Linux常用打包压缩命令

Linux常用打包压缩命令

时间:2024-07-28 18:27:46浏览次数:10  
标签:zip tar 压缩 localhost Linux test txt root 打包

压缩命令应用场景
tar大部分使用tar即可。创建,查看,解压,解压到指定目录
gzip一般配合其他命令使用
zip+unzip一般用于解压zip格式的压缩包

1. tar 打包压缩命令

1.1 命令详解

【功能说明】

在Linux系统里,tar是将多个文件打包在一起,并且可以实现解压打包的文件的命令。是系统管理员最常用的命令之一,tar命令不但可以实现对多个文件进行打包,还可以对多个文件打包后进行压缩。

打包是指将一大堆文件或目录变成一个总的文件,压缩则是将一个大的文件通过一些压缩算法变成一个小文件。

【语法格式】

tar [option] [file]
tar [选项]   [文件或目录]

【选项说明】

参数选项解释说明
z通过gzip压缩或解压
c创建新的tar包
v显示详细的tar命令执行过程
f指定压缩文件的名字
t不解压查看tar包的内容
p保持文件的原有属性
P(大写)以绝对路径打包,危险参数
j通过bzip2命令压缩或解压
x解开tar包
C指定解压的目录路径
–exclude=PATTERN打包时排除不需要处理的文件或目录
-X file从指定文件读取不需要处理的文件或目录列表
-N date仅打包比指定日期新的文件,可用于增量打包备份
-h打包软链接文件指向的真实源文件
–hard-dereference打包硬链接文件

1.2 使用范例

tar命令主要的用途是:

  • 创建压缩包
  • 查看压缩包
  • 解压压缩包
  • 解压压缩包到指定目录
tar命令选项与说明
创建 zcf/zcvftar zcf 压缩包 被压缩的文件/目录
查看 tf/ztftar tf /tmp/etc.tar.gz即使不指定z选项,tar命令也会自动判断压缩包类型,自动调用gzip
解压 xf/xvftar xf /tmp/etc.tar.gz
# 案例1.打包当前的1.txt文件为test.tar.gz
[root@localhost ~]# echo aaa > 1.txt
[root@localhost ~]# ll
total 4
-rw-r--r-- 1 root root 4 Nov 10 12:19 1.txt
[root@localhost ~]# cat 1.txt 
aaa

[root@localhost ~]# tar zcvf test.tar.gz 1.txt
1.txt
[root@localhost ~]# ll
total 8
-rw-r--r-- 1 root root   4 Nov 10 12:19 1.txt
-rw-r--r-- 1 root root 118 Nov 10 12:20 test.tar.gz

# 案例2.同时打包多个文件
#打包/etc/hosts 1.txt /etc/passwd
[root@localhost ~]# tar zcvf test.tar.gz /etc/hosts /etc/passwd 1.txt
tar: Removing leading `/' from member names
/etc/hosts
/etc/passwd
1.txt

# 注意: 打包建议到相对路径
[root@localhost ~]# tar zcvf dir.tar.gz etc test
etc/
etc/hosts
etc/passwd
test/
[root@localhost ~]# ll
total 12
-rw-r--r-- 1 root root   4 Nov 10 12:19 1.txt
-rw-r--r-- 1 root root 695 Nov 10 12:26 dir.tar.gz
drwxr-xr-x 2 root root  33 Nov 10 12:24 etc
drwxr-xr-x 2 root root   6 Nov 10 12:26 test

# 案例3.解压test.tar.gz 到当前目录
[root@localhost ~]# mv test.tar.gz /opt/
[root@localhost ~]# cd /opt/
[root@localhost opt]# tar xf test.tar.gz 
[root@localhost opt]# ll
total 8
-rw-r--r-- 1 root root   4 Nov 10 12:19 1.txt
drwxr-xr-x 2 root root  33 Nov 10 12:28 etc
-rw-r--r-- 1 root root 675 Nov 10 12:22 test.tar.gz
[root@localhost opt]# ll etc/
total 8
-rw-r--r-- 1 root root  158 Nov  3 10:25 hosts
-rw-r--r-- 1 root root 1017 Oct 10  2008 passwd

# 案例4.指定解压到某个位置-C
[root@localhost ~]# tar xf 1.tar.gz -C /opt/
# 案例5.压缩包在/root下 想在/opt下解压
[root@localhost ~]# ll
total 4
-rw-r--r-- 1 root root 695 Nov 10 12:26 dir.tar.gz
[root@localhost ~]# cd /opt/
[root@localhost opt]# rm -rf *
[root@localhost opt]# ll
total 0

[root@localhost ~]# ll
total 4
-rw-r--r-- 1 root root 695 Nov 10 12:26 dir.tar.gz
[root@localhost ~]# cd /opt/
[root@localhost opt]# rm -rf *
[root@localhost opt]# ll
total 0
[root@localhost opt]# ll
total 0
[root@localhost opt]# tar xf /root/dir.tar.gz 
[root@localhost opt]# ll
total 0
drwxr-xr-x 2 root root 33 Nov 10 12:24 etc
drwxr-xr-x 2 root root  6 Nov 10 12:26 test

2. gzip 压缩或解压文件

2.1 命令详情

【功能说明】

gzip命令用于将一个大的文件通过压缩算法 (Lempel-Ziv coding (LZ77))变成一个小的文件。gzip命令不能直接压缩目录,因此目录需要先用tar打包成一个文件,然后tar再调用gzip进行压缩。

【语法格式】

gzip [option] [file]
gzip   [选项] [文件]

【选项说明】

参数选项解释说明
-d解开压缩文件
-v显示指令执行的过程
-l列出压缩文件的内容信息
-c将内容输出到标准输出,不改变原始文件
-r对目录下的所有文件递归进行压缩操作
-n (n这里代指数字,范围1~9)指定压缩率,默认为6,值越大压缩率越高
-t测试,检查压缩文件是否完整

2.2 使用范例

[root@localhost test]# ls
1.txt  2.txt  3.html  4.html
[root@localhost test]# gzip *.txt
[root@localhost test]# ls
1.txt.gz  2.txt.gz  3.html  4.html
# ".gz"后缀是gzip命令自动添加的。
# gzip命令的缺点是压缩后源文件不见了,它的特性是压缩、解压都会自动删除源文件。

# 不解压显示每个压缩文件的信息。使用-l参数
# 压缩率为0是因为,源文件为空文件。
[root@localhost test]# gzip -l *.txt.gz 
         compressed        uncompressed  ratio uncompressed_name
                 26                   0   0.0% 1.txt
                 26                   0   0.0% 2.txt
                122               16429  99.4% 5.txt
                174               16429  99.1% (totals)

# 实际解压文件
[root@localhost test]# gzip -dv *.txt.gz 
1.txt.gz:         0.0% -- replaced with 1.txt
2.txt.gz:         0.0% -- replaced with 2.txt
5.txt.gz:        99.4% -- replaced with 5.txt
# -d 解压缩;-v 显示过程
[root@localhost test]# ls
1.txt  2.txt  3.html  4.html  5.txt
# 查看解压后的结果,gz文件不存在了,只剩下txt文件。

3. zip 打包和压缩文件

3.1 命令详解

【功能说明】

zip压缩格式是 Windows 与 Linux等多平台通用的压缩格式。和gzip命令相比,zip命令压缩文件不仅不会删除源文件,而且还可以压缩目录。

【语法格式】

zip [option] [file]
zip   [选项] [文件或目录]

【选项说明】

参数选项解释说明
-r递归压缩,将指定目录下的所有文件和子目录一并压缩
-x压缩文件时排除某个文件
-q不显示压缩信息

3.2 使用范例

# 压缩文件1.txt和2.txt
[root@localhost test]# ls
1.txt  2.txt  3.html  4.html  5.txt
[root@localhost test]# zip 1-2.zip 1.txt 2.txt 
  adding: 1.txt (stored 0%)
  adding: 2.txt (stored 0%)
[root@localhost test]# ls
1-2.zip  1.txt  2.txt  3.html  4.html  5.txt
# 源文件依然存在

# 压缩目录
[root@localhost ~]# zip -r test.zip test/
  adding: test/ (stored 0%)
  adding: test/2.txt (stored 0%)
  adding: test/1.txt (stored 0%)
  adding: test/3.html (stored 0%)
  adding: test/1-2.zip (stored 0%)
  adding: test/5.txt (deflated 99%)
  adding: test/4.html (stored 0%)
[root@localhost ~]# ls
test  test.zip

# 排除压缩,指定某些文件不进行压缩
[root@localhost ~]# zip -r test.zip test/ -x test/5.txt 
  adding: test/ (stored 0%)
  adding: test/2.txt (stored 0%)
  adding: test/1.txt (stored 0%)
  adding: test/3.html (stored 0%)
  adding: test/1-2.zip (stored 0%)
  adding: test/4.html (stored 0%)

4. unzip 解压zip文件

4.1 命令详解

【功能说明】

unzip命令可以解压zip命令或其他压缩软件压缩的zip格式的文件。

【语法格式】

unzip [option] [fie]
unzip [选项] [压缩文件]

【选项说明】

参数选项解释说明
-l不解压显示压缩包内容
-d指定解压目录
-o解压时不提示是否覆盖文件
-v解压时显示详细信息

4.2 使用范例

# 查看压缩文件信息
[root@localhost ~]# ls
test  test.zip
[root@localhost ~]# unzip -l test.zip 
Archive:  test.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
        0  01-04-2024 13:35   test/
        0  12-29-2023 12:55   test/2.txt
        0  12-29-2023 12:55   test/1.txt
        0  01-04-2024 11:17   test/3.html
      298  01-04-2024 13:35   test/1-2.zip
        0  01-04-2024 11:17   test/4.html
---------                     -------
      298                     6 files

# 直接进行源路径解压,会提示是否覆盖源文件
[root@localhost ~]# unzip test.zip 
Archive:  test.zip
replace test/2.txt? [y]es, [n]o, [A]ll, [N]one, [r]ename: y
 extracting: test/2.txt              
......           
replace test/4.html? [y]es, [n]o, [A]ll, [N]one, [r]ename: y
 extracting: test/4.html             
[root@localhost ~]# ls
test  test.zip

# 解压时不进行提示是否覆盖
[root@localhost ~]# unzip -o test.zip 
Archive:  test.zip
 extracting: test/2.txt              
 extracting: test/1.txt              
 extracting: test/3.html             
 extracting: test/1-2.zip            
 extracting: test/4.html 

# 指定解压目录解压文件
[root@localhost ~]# unzip -d test/ test.zip 
Archive:  test.zip
   creating: test/test/
 extracting: test/test/2.txt         
 extracting: test/test/1.txt         
 extracting: test/test/3.html        
 extracting: test/test/1-2.zip       
 extracting: test/test/4.html
[root@localhost ~]# tree
.
├── test
│   ├── 1-2.zip
│   ├── 1.txt
│   ├── 2.txt
│   ├── 3.html
│   ├── 4.html
│   ├── 5.txt
│   └── test
│       ├── 1-2.zip
│       ├── 1.txt
│       ├── 2.txt
│       ├── 3.html
│       └── 4.html
└── test.zip

标签:zip,tar,压缩,localhost,Linux,test,txt,root,打包
From: https://blog.csdn.net/weixin_44415987/article/details/140739755

相关文章

  • Linux网络:传输层协议TCP(三)滑动窗口及流量控制
    目录一、关于滑动窗口在TCP中的应用1.1什么是滑动窗口,为什么要有滑动窗口1.2滑动窗口的实现1.3滑动窗口针对丢包重传的处理机制二、流量控制一、关于滑动窗口在TCP中的应用1.1什么是滑动窗口,为什么要有滑动窗口在上一篇博文中博主阐述了确认应答ACK策略,对每一个对......
  • java使用google开源工具Thumbnailator实现图片压缩
    Thumbnailator,一款google使用的开源的图片压缩工具类。github地址:https://github.com/coobird/thumbnailator优点:1、压缩程度可控制,想压缩成多小就多小。2、压缩之后图片尽可能的不失真。3、压缩速度要快。4、代码简单,依赖较少。5、可以实现对图片到编辑,如如旋转,裁切,加......
  • Linux内核-异常输出调用栈CallTrace与Ftrace工具集
    1dump_stack函数打印内核调用堆栈。举个例子:我们定义四个函数aaa、bbb、ccc、ddd,然后bbb中调用aaa,ccc中调用bbb,ddd函数谁都不调用。在入口函数中,我们调用ccc与ddd函数,看看堆栈打印效果如何:#include<linux/module.h>#include<linux/kernel.h>#include<linux/init.h>#incl......
  • Blender 4.2 LTS (macOS, Linux, Windows) - 开源 3D 创意软件 (渲染 建模 雕刻)
    Blender4.2LTS(macOS,Linux,Windows)-开源3D创意软件(渲染建模雕刻)创造的自由Blender获得GNUGPL许可,由其贡献者拥有。因此,Blender永远是免费和开源软件。使命以免费/开源软件的形式将世界上最好的3DCG技术交到艺术家手中。想象每个人都应该自由地创......
  • Linux——CPU占不上去的解决办法
    一、将调节器升至performance:1.1查看当前的调节器:cat/sys/devices/system/cpu/cpu0/cpufreq/scaling_governor如果不是performance,则进入root账户1.2进入root账户先进入管理员账户输入命令:suroot如果没有root账号,则参考博客:Linux系统下的root用户初始密码设......
  • Linux——手动清理内存缓存
    前言:使用free-m命令可以查看内存缓存。一、方法1.1先进管理员账户,然后进root账户1.2运行下面的命令:syncecho1>/proc/sys/vm/drop_caches#清空目录项缓存echo0>/proc/sys/vm/drop_caches#还原默认配置,这一步如果出错,则不用管sync二、小贴士......
  • linux服务器使用docker部署ES相关记录
    ES/可视化工具Kibana/ik分词器最好使用相同版本部署,实在找不到资源可基于ES版本,其余可向下兼容找最高版本docker创建网络因为我们还需要部署kibana容器,因此需要让es和kibana容器互联。这里先创建一个网络:dockernetworkcreatees-netES配置文件夹数据卷挂载:需先复制config......
  • 【Linux应用编程】Day10_进程 一文详细剖析进程,从基本概念到创建再到进程操作直至消亡
    进程详细剖析进程,包括以下内容:⚫程序与进程基本概念;⚫程序的开始与结束;⚫进程的环境变量与虚拟地址空间;⚫进程ID;⚫fork()创建子进程;⚫进程的消亡与诞生;⚫僵尸进程与孤儿进程;⚫父进程监视子进程;⚫进程关系与进程的六种状态;⚫守护进程;⚫进程间通信概......
  • Linux用户和权限
    文章目录Linux用户和权限root用户和普通用户root用户(超级管理员)用户管理su和exit命令sudo命令为普通用户配置sudo认证用户组管理查看用户所属组创建以用户的时候指定组添加已经存在用户到指定组查看权限限制认知权限信息rwx修改权限控制chmod示例权限的数字序号chown......
  • 【linux】【设备树】具有 GPIO 控制器和连接器的硬件配置的备树(Device Tree)代码讲解
    具有GPIO控制器和连接器的硬件配置的备树(DeviceTree)代码讲解背景-学习Linux设备树代码soc{soc_gpio1:gpio-controller1{#gpio-cells=<2>;};soc_gpio2:gpio-controller2{#gpio-cells=<2>;};};connector:connect......