Linux常用打包压缩命令

压缩命令应用场景
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
  • 9
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 常用Linux压缩命令包括: 1. tar 命令:用于打包和解压 tar 格式的文件,常见的参数包括 -x(解压)、-c(打包)、-v(显示详细信息)、-f(指定文件名)等。 2. gzip 命令:用于压缩和解压 gzip 格式的文件,常见的参数包括 -d(解压)、-c(输出到标准输出)、-v(显示详细信息)、-f(强制覆盖)等。 3. bzip2 命令:用于压缩和解压 bzip2 格式的文件,常见的参数包括 -d(解压)、-c(输出到标准输出)、-v(显示详细信息)、-f(强制覆盖)等。 4. unzip 命令:用于解压 zip 格式的文件,常见的参数包括 -d(指定解压目录)、-v(显示详细信息)、-o(覆盖已有文件)等。 5. rar 命令:用于解压 rar 格式的文件,常见的参数包括 x(解压)、e(解压到当前目录)、v(显示详细信息)等。 6. 7z 命令:用于解压 7z 格式的文件,常见的参数包括 x(解压)、e(解压到当前目录)、v(显示详细信息)等。 ### 回答2: 在Linux系统中,压缩和解压缩是很常见的操作。有时候,在下载一些文件或软件包后,我们需要将它们解压缩出来才能进行安装或使用。本文将给大家介绍一些常用Linux压缩命令。 1. tar命令 tar命令Linux系统下一个很常用压缩和解压缩工具。可以将多个文件打包为一个文件,并可对其进行压缩和解压缩。通常用于将核心程序的源代码打包备份,方便传输和保存。例如:解压缩命令 tar -zxvf filename.tar.gz 解压缩的文件名称为filename.tar.gz 2. gzip命令 gzip命令可以用来对文件进行压缩和解压缩压缩后的文件通常采用“.gz”作为扩展名。例如:解压缩命令 gzip -d filename.tar.gz, 解压缩后的文件命名为filename.tar 3. bzip2命令 bzip2是一款高压缩比的压缩软件,常用压缩大文件。解压缩后的文件通常采用“.bz2”作为扩展名。例如:解压缩命令 bzip2 –d filename.tar.bz2, 解压缩后的文件命名为filename.tar 4. zip命令 zip是一款常用的文件压缩和解压缩软件,支持多种压缩方法和压缩格式。解压缩后的文件通常采用“.zip”作为扩展名。例如:解压缩命令 zip -r filename.zip dirname,解压缩后的文件名称为filename.zip 总之,Linux系统下有很多压缩和解压缩工具,采用不同的命令和参数可以完成不同的压缩和解压缩操作,上述介绍的是常用的一些命令。在使用它们时,需要根据实际情况进行选择和操作。 ### 回答3: Linux是一个广受欢迎的操作系统平台,在日常使用中,我们需要处理各种类型的文件,其中压缩文件是最常见的。解压缩是指将压缩包中的文件还原成原始状态,Linux系统提供了各种类型的解压缩命令,以下是常见的几种解压缩命令及其使用方法。 1. tar命令:tar是Linux系统中常用压缩命令,它可以将多个文件或目录打包成一个文件,并可以选择不同的压缩格式。解压tar包的命令为: tar -zxvf filename.tar.gz # 解压gzip格式的tar包 tar -jxvf filename.tar.bz2 # 解压bzip2格式的tar包 tar -xvf filename.tar # 解压普通的tar包 2. unzip命令:unzip是Linux系统中用于解压zip格式压缩文件的命令,使用方法如下: unzip filename.zip # 解压zip格式的文件 3. gzip命令:gzip是Linux系统中用于压缩和解压缩gzip格式文件的命令。解压gzip文件的命令为: gzip -d filename.gz # 解压缩gzip格式的文件 4. bzip2命令:bzip2是Linux系统中用于压缩和解压缩bzip2格式文件的命令。解压bzip2文件的命令如下: bzip2 -d filename.bz2 # 解压缩bzip2格式的文件 总结:以上是常见的Linux系统中的解压缩命令,掌握这些命令可以更方便地处理压缩文件。需要注意的是,在使用这些命令时,要根据具体的压缩文件格式选择相应的命令,并指定正确的参数。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值