Linux下的文件打包、压缩和解压缩

文章目录

  • 打包、压缩、解压缩整体介绍
  • 数据压缩
    • gzip
      • 示例
        • 压缩单个文件
        • 压缩多个文件
        • 保留原始文件并压缩
        • 递归压缩目录
        • 解压缩文件
        • 查看gz压缩文件的内容
    • bzip2
      • 示例
        • 压缩单个文件
        • 压缩多个文件
        • 压缩目录下的文件
        • 解压缩文件
        • 查看bz2压缩文件的内容
      • 命令行选项总结
    • zip
      • 示例
        • 压缩文件
        • 解压缩文件
        • 添加文件到已有的ZIP文件
        • 查看zip压缩文件的内容
    • xz
      • 示例
        • 压缩文件
        • 解压文件
        • 查看压缩文件夹的内容
  • 数据打包(归档)
    • 打包文件
    • 压缩文件
    • 解压文件
    • 查看tar文件内容
    • 其他用法

打包、压缩、解压缩整体介绍

打包:也称为归档,是将多个文件和目录合并成一个单独的文件。这个过程不会减少文件的大小,只是简单地将多个文件组合在一起,便于传输和存储。打包后的文件称为归档文件。在Linux中,最流行的打包工具是tar命令。

压缩:压缩是指通过特定算法减小文件的大小,以节省存储空间或加快传输速度。在Linux中,常见的压缩工具有gzip、bzip2、xz、zip和7z等。

解压缩:解压缩是压缩的逆过程,即将压缩文件还原为原始文件。

这里要区分一下打包和压缩:打包文件没有经过压缩,因此打包文件占用的存储空间是其中所有文件和目录的总和。

这里要注意打包和压缩的区别:

  • 文件大小:打包不会改变文件的实际大小,而压缩会减小文件的大小。
  • 目的不同:打包主要是为了方便文件的管理和传输,而压缩是为了减少文件占用的空间或提高传输效率。

在Linux中常见的文件压缩工具如下:

工具文件扩展名描述
bzip2.bz2采用Burrows-Wheeler块排序文本压缩算法和霍夫曼编码
compress.Z最初的Unix文件压缩工具,已经快要无人使用了
gzip.gzGNU压缩工具,用Lempel-Zivwelch编码(在Linux下,gzip是最流行的压缩工具)
xz.xz日渐流行的通用压缩工具
zip.zipWindows中PKZIP工具的Unix实现

本文首先会介绍Linux中常见的压缩工具的使用:gzip、bzip2、zip、xz。然后介绍打包工具tar命令的使用。

本文所讲的内容如下图的思维导图(其中,标黄的地方是我认为必须要掌握的)。
在这里插入图片描述

数据压缩

gzip

gzip软件包是GNU项目的产物,旨在编写一个能够替代原先Unix中compress工具的免费版本。这个软件包包括以下文件:

  • gzip:用于压缩文件。使用Lempel-Ziv-Welch(LZ77)算法进行压缩,通常用于压缩单个文件。压缩后的文件以.gz扩展名结尾。gzip压缩效率较高,但压缩率可能不如bzip2和xz。
  • gzcat:用于查看压缩过的文本文件的内容
  • gunzip:用于解压文件

示例

压缩单个文件

假设你有一个名为file1的文件,你想用gzip来压缩它,则可以使用以下命令:

zld@zld:~/Codes/shellCode$ gzip file1

执行上述命令后,file1文件将被压缩,并生成一个名为file1.gz的压缩文件。原始的file1文件将被删除(除非你使用了特定的选项来保留它)。

压缩多个文件

如果你有多个文件需要压缩,比如file1、file2、file3,你可以一次性压缩它们:

zld@zld:~/Codes/shellCode$ gzip file1 file2 file3 

zld@zld:~/Codes/shellCode$ ls -l *.gz
-rw-rw-r-- 1 zld zld 50 Oct 11 14:08 file1.gz
-rw-rw-r-- 1 zld zld 46 Oct 11 14:10 file2.gz
-rw-rw-r-- 1 zld zld 71 Oct 11 14:13 file3.gz

这将会生成file1.gz、file2.gz、file3.gz三个压缩文件。

保留原始文件并压缩

如果你希望在压缩后保留原始文件,可以使用-c选项将压缩后的内容输出到标准输出,然后使用重定向将其保存为一个新的文件。或者,更简单地,使用-k选项:

zld@zld:~/Codes/shellCode$ gzip -c file1 > file1.gz

或者

zld@zld:~/Codes/shellCode$ gzip -k file1 

注意:-k选项在大多数gzip实现中并不是标准的,它可能不会在所有系统上可用。更常见的方法是使用-c和重定向。

-c和-k选项在手册中的解释:

-c ‐‐stdout ‐‐to‐stdout
Write output on standard output; keep original files unchanged.  If there are several input files, the output con‐
sists of a sequence of independently compressed members. To obtain better compression, concatenate all input files
before compressing them.


-k ‐‐keep
Keep (don’t delete) input files during compression or decompression.
递归压缩目录

如果你想递归地压缩一个目录中的所有文件,可以使用-r选项。但是,gzip默认不会压缩目录本身,而是会压缩目录中的每个文件(但是不支持压缩子目录)。

# testDir目录中有两个文件:cluster.c、test1.txt,以及一个子目录redis-test
zld@zld:~/Codes/shellCode$ tree testDir/
testDir/
├── cluster.c
├── redis-test
│   ├── test.c.gz
│   └── test.gz
└── test1.txt

2 directories, 4 files


# 使用-r选项递归地压缩目录
zld@zld:~/Codes/shellCode$ gzip -r testDir/

# 默认没有压缩目录本身,而是压缩目录中的每个文件(但不包括子目录)
zld@zld:~/Codes/shellCode$ tree testDir/
testDir/
├── cluster.c.gz
├── redis-test
│   ├── test.c.gz
│   └── test.gz
└── test1.txt.gz

2 directories, 4 files
解压缩文件

要想解压一个gzip压缩的文件,可以使用-d选项(表示decompress):

zld@zld:~/Codes/shellCode/testDir$ gzip -d test1.txt.gz

或者,更常见的是,直接使用gunzip命令,它是gzip的解压缩版本:

zld@zld:~/Codes/shellCode/testDir$ gunzip cluster.c.gz 

注意,上面的两条命令会删除原始的gz文件。如果想保留gz文件,可以像之前一样使用-c和重定向 或者-k`选项:

# 用gzip命令同时使用-d 和-k选项,来解压.gz文件,并保留原始.gz文件
zld@zld:~/Codes/shellCode/testDir$ gzip -dk cluster.c.gz 
zld@zld:~/Codes/shellCode/testDir$ ll
total 312
drwxrwxr-x 2 zld zld   4096 Oct 11 17:16 ./
drwxrwxr-x 3 zld zld   4096 Oct 11 17:00 ../
-rw-rw-r-- 1 zld zld 241844 Oct 11 14:54 cluster.c
-rw-rw-r-- 1 zld zld  61378 Oct 11 14:54 cluster.c.gz
-rw-rw-r-- 1 zld zld     34 Oct 11 17:06 test1.txt.gz

# 用gunzip命令并且使用-k选项,来解压.gz文件,并保留原始.gz文件
zld@zld:~/Codes/shellCode/testDir$ gunzip -k test1.txt.gz 
zld@zld:~/Codes/shellCode/testDir$ ll
total 316
drwxrwxr-x 2 zld zld   4096 Oct 11 17:16 ./
drwxrwxr-x 3 zld zld   4096 Oct 11 17:00 ../
-rw-rw-r-- 1 zld zld 241844 Oct 11 14:54 cluster.c
-rw-rw-r-- 1 zld zld  61378 Oct 11 14:54 cluster.c.gz
-rw-rw-r-- 1 zld zld     13 Oct 11 17:06 test1.txt
-rw-rw-r-- 1 zld zld     34 Oct 11 17:06 test1.txt.gz

# 使用-c 和重定向保留原始文件
zld@zld:~/Codes/shellCode/testDir$ gzip -cd test1.txt.gz > test1.txt
zld@zld:~/Codes/shellCode/testDir$ ll
total 316
drwxrwxr-x 2 zld zld   4096 Oct 11 17:19 ./
drwxrwxr-x 3 zld zld   4096 Oct 11 17:00 ../
-rw-rw-r-- 1 zld zld 241844 Oct 11 14:54 cluster.c
-rw-rw-r-- 1 zld zld  61378 Oct 11 14:54 cluster.c.gz
-rw-rw-r-- 1 zld zld     13 Oct 11 17:19 test1.txt
-rw-rw-r-- 1 zld zld     34 Oct 11 17:06 test1.txt.gz

zld@zld:~/Codes/shellCode/testDir$ gunzip -c test1.txt.gz > test1.txt
zld@zld:~/Codes/shellCode/testDir$ ll
total 316
drwxrwxr-x 2 zld zld   4096 Oct 11 17:21 ./
drwxrwxr-x 3 zld zld   4096 Oct 11 17:00 ../
-rw-rw-r-- 1 zld zld 241844 Oct 11 14:54 cluster.c
-rw-rw-r-- 1 zld zld  61378 Oct 11 14:54 cluster.c.gz
-rw-rw-r-- 1 zld zld     13 Oct 11 17:21 test1.txt
-rw-rw-r-- 1 zld zld     34 Oct 11 17:06 test1.txt.gz
查看gz压缩文件的内容

可以通过zcat命令(在某些系统上命令可能是gzcat)查看gz压缩文件的内容:

zld@zld:~/Codes/shellCode/testDir$ zcat test1.txt.gz 
111111111111

zcat命令相当于gunzip -c

zld@zld:~/Codes/shellCode/testDir$ gunzip -c test1.txt.gz 
111111111111

bzip2

bzip2:基于Burrows-Wheeler变化的文件压缩工具,通常提供比gzip更高的压缩率,但压缩和解压缩的速度较慢。压缩后的文件以.bz2扩展名结尾。

示例

压缩单个文件

假设你有一个名为file1的文件,你想用bzip2来压缩它,则可以使用以下命令:

zld@zld:~/Codes/shellCode$ bzip2 file1
zld@zld:~/Codes/shellCode$ ls -l *.bz2
-rw-rw-r-- 1 zld zld 58 Oct 11 16:59 file1.bz2

执行上述命令后,file1文件将被压缩,并生成一个名为file1.bz2的压缩文件。原始的file1文件将被删除。

和gzip一样,如果你希望保留原始文件文件,可以使用-k选项或者-c选项加重定向:

zld@zld:~/Codes/shellCode$ bzip2 -k file2

zld@zld:~/Codes/shellCode$ bzip2 -c file2 > file2.bz2
压缩多个文件

如果你有多个文件需要压缩,比如file1、file2、file3,你可以一次性压缩它们:

zld@zld:~/Codes/shellCode$ bzip2 file1  file2  file3 

zld@zld:~/Codes/shellCode$ ls -l *.bz2
-rw-rw-r-- 1 zld zld 58 Oct 11 16:59 file1.bz2
-rw-rw-r-- 1 zld zld 50 Oct 11 14:10 file2.bz2
-rw-rw-r-- 1 zld zld 79 Oct 11 14:13 file3.bz2

这将会生成file1.bz2、file2.bz2、file3.bz2三个压缩文件。

压缩目录下的文件

如果你想要压缩一个目录下的所有文件,需要注意bzip2本身不支持直接压缩目录。但是你可以通过通配符来压缩目录下的所有文件。例如压缩testDir目录下的所有文件(不包括子目录):

# testDir目录下有两个文件(cluster.c、 test1.txt)和一个子目录(redis-test):
zld@zld:~/Codes/shellCode$ ls -R testDir/
testDir/:
cluster.c  redis-test  test1.txt

testDir/redis-test:
test  test.c

# 通过通配符来压缩目录下的所有文件,但是不包括子目录
zld@zld:~/Codes/shellCode$ bzip2 testDir/*
bzip2: Input file testDir/redis-test is a directory.

# 压缩后:生成了两个压缩文件:cluster.c.bz2、test1.txt.bz2,但是子目录redis-test没有压缩
zld@zld:~/Codes/shellCode$ ls -Rl testDir/
testDir/:
total 56
-rw-rw-r-- 1 zld zld 47624 Oct 11 14:54 cluster.c.bz2
drwxr-xr-x 2 zld zld  4096 Oct 11 18:34 redis-test
-rw-rw-r-- 1 zld zld    41 Oct 11 17:21 test1.txt.bz2

testDir/redis-test:
total 20
-rwxr-xr-x 1 zld zld 15952 Oct 11 18:34 test
-rw-r--r-- 1 zld zld  2421 Oct 11 18:34 test.c
解压缩文件

要想解压一个bz2压缩的文件,可以使用-d选项(表示decompress):

zld@zld:~/Codes/shellCode$ bzip2 -d file1.bz2 

或者,直接使用bunzip2命令,它是bzip2的解压缩版本:

zld@zld:~/Codes/shellCode$ bunzip2 file2.bz2 

注意,上面的两条命令会删除原始的bz2文件。如果想保留bz2文件,可以像之前一样使用-c和重定向 或者-k`选项(参考前述内容)。

查看bz2压缩文件的内容

可以通过bzcat命令查看bz2压缩文件的内容:

zld@zld:~/Codes/shellCode$ bzcat file3.bz2 
Apr
Aug
Dec
Feb
Jan
Jul
Jun
Mar
May
Nov
Oct
Sep

bzcat命令相当于bzip2 ‐dc

zld@zld:~/Codes/shellCode$ bzip2 -dc file3.bz2 
Apr
Aug
Dec
Feb
Jan
Jul
Jun
Mar
May
Nov
Oct
Sep

命令行选项总结

现将上述常用的选项在这总结一下:

  • -c或--stdout:将压缩与解压缩的结果送到标准输出。
  • -d或--decompress:执行解压缩。
  • -k或--keep:在压缩或解压缩后保留原始文件。

zip

zip:支持多种压缩算法的压缩工具,包括DEFLATE。zip通常用于创建ZIP格式的压缩文件,该格式在Windows上非常流行,因此常用于跨平台项目。压缩后的文件以.zip扩展名结尾。

示例

压缩文件

zip最基础的压缩格式如下:

zip zipfile.zip file1 file2 ...filen

上面这个命令的意思是将文件file1、file2、一直到filen一次性将它们压缩到一个zipfile.zip的文件中。zip可以同时压缩多个文件到一个文件中,这个是和前面的gzip、bzip2不同的地方。

如果想要压缩一个目录及其包含的所有文件和子目录,可以使用-r选项来递归地压缩目录。

zld@zld:~/Codes/shellCode$ ll
total 32
drwxrwxr-x  3 zld zld 4096 Oct 11 19:18 ./
drwxrwxr-x 11 zld zld 4096 Sep 28 03:45 ../
-rw-rw-r--  1 zld zld   24 Oct 11 16:59 file1
-rw-rw-r--  1 zld zld   23 Oct 11 14:10 file2
-rw-rw-r--  1 zld zld   48 Oct 11 14:13 file3
-rw-rw-r--  1 zld zld   13 Oct 10 14:51 test1.txt
drwxrwxr-x  3 zld zld 4096 Oct 11 19:12 testDir/
-rw-rw-r--  1 zld zld  680 Oct 10 14:59 test_file

# 将文件file1、file2、file3同时压缩到一个文件中,压缩后的文件名为archive.zip
zld@zld:~/Codes/shellCode$ zip archive.zip file1 file2 file3 
  adding: file1 (stored 0%)
  adding: file2 (deflated 4%)
  adding: file3 (deflated 2%)
zld@zld:~/Codes/shellCode$ ll
total 36
drwxrwxr-x  3 zld zld 4096 Oct 11 19:23 ./
drwxrwxr-x 11 zld zld 4096 Sep 28 03:45 ../
-rw-rw-r--  1 zld zld  529 Oct 11 19:23 archive.zip
-rw-rw-r--  1 zld zld   24 Oct 11 16:59 file1
-rw-rw-r--  1 zld zld   23 Oct 11 14:10 file2
-rw-rw-r--  1 zld zld   48 Oct 11 14:13 file3
-rw-rw-r--  1 zld zld   13 Oct 10 14:51 test1.txt
drwxrwxr-x  3 zld zld 4096 Oct 11 19:12 testDir/
-rw-rw-r--  1 zld zld  680 Oct 10 14:59 test_file

# 如果想要压缩一个目录及其包含的所有文件和子目录,则需要加上-r选项
zld@zld:~/Codes/shellCode$ zip -r archive2.zip testDir/
  adding: testDir/ (stored 0%)
  adding: testDir/cluster.c (deflated 75%)
  adding: testDir/test1.txt (deflated 54%)
  adding: testDir/redis-test/ (stored 0%)
  adding: testDir/redis-test/test.c.gz (stored 0%)
  adding: testDir/redis-test/test.gz (stored 0%)
解压缩文件

在Linux下,解压缩ZIP文件通常使用unzip命令。

基本语法如下:

unzip 文件名.zip

这会将ZIP文件中的所有内容解压到当前目录。

如果你想要将文件解压到指定目录,可以使用-d选项。

# 解压到当前目录
zld@zld:~/Codes/shellCode$ unzip archive.zip 

# 解压到指定目录
zld@zld:~/Codes/shellCode$ unzip archive.zip -d ./tmp1011
Archive:  archive.zip
 extracting: ./tmp1011/file1         
  inflating: ./tmp1011/file2         
  inflating: ./tmp1011/file3     
添加文件到已有的ZIP文件

如果你已有一个ZIP文件,并且想要向其中添加更多的文件,可以使用-u选项。

# 向archive.zip中添加test1.txt
zld@zld:~/Codes/shellCode$ zip -u archive.zip test1.txt 
  adding: test1.txt (deflated 54%)
查看zip压缩文件的内容

因为zip压缩文件可以同时将多个文件压缩到一个文件中,所以一般来说,无法查看zip压缩文件的具体内容。

但是可以看到压缩文件中包含的文件和文件夹:

  • 方法一:使用unzip命令:可以使用unzip命令,加上-l选项,表示只列出压缩包里的文件名,而不解压它们。这个命令会显示ZIP文件中的所有文件和文件夹,以及每个文件的大小和最后修改的日期。

    zld@zld:~/Codes/shellCode$ unzip -l archive.zip 
    Archive:  archive.zip
      Length      Date    Time    Name
    ---------  ---------- -----   ----
           24  2024-10-11 16:59   file1
           23  2024-10-11 14:10   file2
           48  2024-10-11 14:13   file3
    ---------                     -------
           95                     3 files
    
  • 方法二:使用zipinfo命令:zipinfo是另一个用于处理ZIP文件的命令行工具,它提供了有关ZIP文件的详细信息,包括文件名、大小、日期、压缩比率、压缩方法等。

    zld@zld:~/Codes/shellCode$ zipinfo archive.zip 
    Archive:  archive.zip
    Zip file size: 529 bytes, number of entries: 3
    -rw-rw-r--  3.0 unx       24 tx stor 24-Oct-11 16:59 file1
    -rw-rw-r--  3.0 unx       23 tx defN 24-Oct-11 14:10 file2
    -rw-rw-r--  3.0 unx       48 tx defN 24-Oct-11 14:13 file3
    3 files, 95 bytes uncompressed, 93 bytes compressed:  2.1%
    
  • 使用zcat命令:zcat可以将ZIP文件的内容输出到标准输出,但是,zcat命令用于常看包含单个文本文件的ZIP文件,如果ZIP文件中包含多个文件或目录,则可能无法正确显示。

    # 单个文件显示没有问题
    zld@zld:~/Codes/shellCode$ zcat archive3.zip
    111111111111
    
    # 如果压缩文件包含多个文件或目录,则无法正确显示
    zld@zld:~/Codes/shellCode$ zcat archive2.zip
    gzip: archive2.zip has more than one entry--rest ignored
    zld@zld:~/Codes/shellCode$ 
    

xz

xz是一个在Linux系统中广泛使用的数据压缩工具,它采用了LZMA(Lempel-Ziv-Markov chain-Algorithm)或LZMA2算法,提供了极高的压缩比。

基本使用语法为:

xz [option...]  [file...]

示例

压缩文件

要压缩文件test1.txt,可以使用以下命令:

zld@zld:~/Codes/shellCode$ xz test1.txt

这将会生成一个名为test1.txt.xz的压缩文件,并且原文件test1.txt会被删除。如果想保留原文件,需要使用-k选项:

zld@zld:~/Codes/shellCode$ xz -k file1 

注意:xz压缩工具本身不支持直接将多个文件压缩成一个单独的xz压缩文件,一般是使用tar命令与xz结合。首先,使用tar命令打包多个文件,然后再压缩归档文件。

解压文件

如果需要解压文件,可以使用以下命令:

xz -d [选项] 压缩文件名

或者

unxz [选项] 压缩文件名

例如,

zld@zld:~/Codes/shellCode$ xz -d test1.txt.xz

zld@zld:~/Codes/shellCode$ unxz file1.xz 

在上述命令解压后,原始压缩文件.xz会被删除,同样的,如果想保留原始文件,需要使用-k选项。

zld@zld:~/Codes/shellCode$ unxz -k file1.xz 

zld@zld:~/Codes/shellCode$ xz -dk file2.xz 
查看压缩文件夹的内容

要查看一个xz压缩文件的内容而不进行解压,可以使用xzcat命令:

zld@zld:~/Codes/shellCode$ xzcat file1.xz 
one
two
three
four
five

当然也可以用以下命令:

zld@zld:~/Codes/shellCode$ xz -cd file1.xz 
one
two
three
four
five

数据打包(归档)

打包(也称为归档)是将多个文件和目录合并成一个单独的文件,便于传输和存储。Linux中最常用的打包工具是tar。tar(全称是tape archive)是一个强大的文件打包和解包工具,它可以将多个文件和目录打包成一个单独的文件。tar本身并不直接压缩文件,但可以与压缩工具如gzipbzip2xz等结合使用,以创建压缩的tar归档文件。

tar命令的格式如下:

tar function [options] object1 object2...

其中function定义了tar命令要执行的操作:

操作              长选项                          描述
-A              --concatenate              将一个tar文档文件追加到另一个tar归档文件末尾
-c              --create                   创建新的tar归档文件
-d              --diff                     检查归档文件和文件系统的不同之处
                --delete                   从tar归档文件中删除文件
-r              --append                   将文件追加到tar归档文件末尾
-t              --list                     列出tar归档文件的内容
-u              --update                 将比tar归档文件中已有的同名文件更新的文件追加到该归档文件
-x              --extract                  从tar归档文件中提取文件

每种操作都使用option(选项)来定义针对tar归档文件的具体行为,下面是常用的一些选项:

选项                            描述
-C dir                         切换到指定目录
-f file                        将结果输出到文件(或设备)
-j                             将输出传给bzip2命令进行压缩
-J                             将输出传给xz命令进行压缩
-p                             保留文件的所有权限
-v                             在处理文件时显示文件名
-z                             将输出传给gzip命令进行压缩
-Z                             将输出传给compress命令进行压缩

上面的这些选项通常合并使用,下面是一些示例。

打包文件

zld@zld:~/Codes/shellCode$ tar -cvf test.tar testDir/ tmp1011/
testDir/
testDir/cluster.c
testDir/test1.txt
testDir/redis-test/
testDir/redis-test/test.c.gz
testDir/redis-test/test.gz
tmp1011/
tmp1011/file1
tmp1011/file3
tmp1011/file2

上面的命令在当前目录下创建了一个名为test.tar的归档文件,包含目录testDir、tmp1011的内容。从上面的选项可知,-c表示创建新的归档文件,-v表示显示详细处理信息(这个是可选的),-f指定归档文件的名称。

如果想要将归档文件输出到指定目录,也可以写上指定的目录加归档文件名(但是指定的目录必须存在,tar命令不会帮你创建目录),上面因为没有指定目录,因此是默认在当前目录下:

zld@zld:~/Codes/shellCode$ tar -cvf test1/test.tar testDir/ tmp1011/
testDir/
testDir/cluster.c
testDir/test1.txt
testDir/redis-test/
testDir/redis-test/test.c.gz
testDir/redis-test/test.gz

上面这个命令就是将归档文件输出到当前目录的子目录test1中。

压缩文件

tar本身并不直接压缩文件,但可以与压缩工具如gzipbzip2xz等结合使用,以创建压缩的tar归档文件。

  • gzip压缩:打包后,以gzip压缩:
zld@zld:~/Codes/shellCode$ tar -zcvf tmp1011.tar.gz tmp1011/
tmp1011/
tmp1011/file1
tmp1011/file3
tmp1011/file2

其中,-z表示使用gzip压缩。

  • bizp2压缩:打包后,以bzip2压缩:

    zld@zld:~/Codes/shellCode$ tar -jcvf tmp1011.tar.bz2 tmp1011/
    tmp1011/
    tmp1011/file1
    tmp1011/file3
    tmp1011/file2
    

这里,-j表示使用bzip2压缩。

  • xz压缩:打包后,以xz压缩:

    zld@zld:~/Codes/shellCode$ tar -Jcvf tmp1011.tar.xz tmp1011/
    tmp1011/
    tmp1011/file1
    tmp1011/file3
    tmp1011/file2
    

这里,-J表示使用xz压缩。

解压文件

  • 解压.tar文件到指定目录

如果我们想要从一个归档文件(后缀是.tar)提取文件(即解压),则可以使用-x选项:

zld@zld:~/Codes/shellCode$ tar -xvf test.tar -C test1/
testDir/
testDir/cluster.c
testDir/test1.txt
testDir/redis-test/
testDir/redis-test/test.c.gz
testDir/redis-test/test.gz
tmp1011/
tmp1011/file1
tmp1011/file3
tmp1011/file2

上面的这条命令,就是将归档文件test.tar提取文件出来,并将提取出来的文件放到当前目录的子目录test1中。其中,-C表示切换到指定目录。

上述命令中,如果不使用-C选项,则会解压到当前目录下:

zld@zld:~/Codes/shellCode/test0$ tar -xvf test.tar 
testDir/
testDir/cluster.c
testDir/test1.txt
testDir/redis-test/
testDir/redis-test/test.c.gz
testDir/redis-test/test.gz
tmp1011/
tmp1011/file1
tmp1011/file3
tmp1011/file2

下面解压gzip等文件也是一样,不加-C选项,解压到当前目录下。

  • 解压gzip压缩文件

    # 在当前目录下创建一个子目录test2
    zld@zld:~/Codes/shellCode$ mkdir test2
    
    # 解压gzip文件tmp1011.tar.gz到指定目录test2,其中,-z表示文件是通过gzip压缩的,-C指定了目录
    zld@zld:~/Codes/shellCode$ tar -xzvf tmp1011.tar.gz -C test2/
    tmp1011/
    tmp1011/file1
    tmp1011/file3
    tmp1011/file2
    
    # 查看解压后的文件
    zld@zld:~/Codes/shellCode/test2$ ll
    total 12
    drwxrwxr-x 3 zld zld 4096 Oct 11 21:33 ./
    drwxrwxr-x 6 zld zld 4096 Oct 11 21:33 ../
    drwxrwxr-x 2 zld zld 4096 Oct 11 19:34 tmp1011/
    

    在上面的命令tar -xzvf tmp1011.tar.gz -C test2/中,-x表示解压,,-z表示文件是通过gzip压缩的,-v表示显示解压过程中的文件(可选),-f指定了归档文件的名称(这里例子中是指tmp1011.tar.gz),-C指定了目标目录,-C选项一般单独放在最后,以避免路径解析问题,同时还可以提高阅读性。

    注意:在下载开源软件时经常会看到文件名以.tgz结尾,这是经gzip压缩过的tar文件,同样可以使用tar -xzvf filename.tgz来解压。

  • 解压bizp2(后缀为.bz2)压缩文件

    # 在当前目录下创建一个子目录test3
    zld@zld:~/Codes/shellCode$ mkdir test3
    
    # 解压bzip2文件tmp1011.tar.bz2到指定目录test3,其中,-j表示文件是通过bzip2压缩的,-C指定了目录
    zld@zld:~/Codes/shellCode$ tar -xjvf tmp1011.tar.bz2 -C test3/
    tmp1011/
    tmp1011/file1
    tmp1011/file3
    tmp1011/file2
    zld@zld:~/Codes/shellCode$ ls -Rl test3/
    test3/:
    total 4
    drwxrwxr-x 2 zld zld 4096 Oct 11 19:34 tmp1011
    
    # 查看解压后的文件
    test3/tmp1011:
    total 12
    -rw-rw-r-- 1 zld zld 24 Oct 11 16:59 file1
    -rw-rw-r-- 1 zld zld 23 Oct 11 14:10 file2
    -rw-rw-r-- 1 zld zld 48 Oct 11 14:13 file3
    
  • 解压xz(后缀为.xz)压缩文件

# 在当前目录下创建一个子目录test4
zld@zld:~/Codes/shellCode$ mkdir test4

# 解压xz文件tmp1011.tar.xz到指定目录test4,其中,-J表示文件是通过xz压缩的,-C指定了目录
zld@zld:~/Codes/shellCode$ tar -xJvf tmp1011.tar.xz -C test4/
tmp1011/
tmp1011/file1
tmp1011/file3
tmp1011/file2

# 查看解压后的文件
zld@zld:~/Codes/shellCode$ ls -Rl test4/
test4/:
total 4
drwxrwxr-x 2 zld zld 4096 Oct 11 19:34 tmp1011

test4/tmp1011:
total 12
-rw-rw-r-- 1 zld zld 24 Oct 11 16:59 file1
-rw-rw-r-- 1 zld zld 23 Oct 11 14:10 file2
-rw-rw-r-- 1 zld zld 48 Oct 11 14:13 file3

查看tar文件内容

-t选项表示列出tar归档文件的内容。

  • 查看tar文件的内容

    zld@zld:~/Codes/shellCode$ tar -tvf test.tar 
    drwxrwxr-x zld/zld           0 2024-10-11 19:12 testDir/
    -rw-rw-r-- zld/zld      241844 2024-10-11 14:54 testDir/cluster.c
    -rw-rw-r-- zld/zld          13 2024-10-11 17:21 testDir/test1.txt
    drwxr-xr-x zld/zld           0 2024-10-11 18:55 testDir/redis-test/
    -rw-r--r-- zld/zld         450 2024-10-11 18:34 testDir/redis-test/test.c.gz
    -rwxr-xr-x zld/zld        2284 2024-10-11 18:34 testDir/redis-test/test.gz
    drwxrwxr-x zld/zld           0 2024-10-11 19:34 tmp1011/
    -rw-rw-r-- zld/zld          24 2024-10-11 16:59 tmp1011/file1
    -rw-rw-r-- zld/zld          48 2024-10-11 14:13 tmp1011/file3
    -rw-rw-r-- zld/zld          23 2024-10-11 14:10 tmp1011/file2
    
  • 查看gzip压缩的tar文件内容

    zld@zld:~/Codes/shellCode$ tar -ztvf tmp1011.tar.gz 
    drwxrwxr-x zld/zld           0 2024-10-11 19:34 tmp1011/
    -rw-rw-r-- zld/zld          24 2024-10-11 16:59 tmp1011/file1
    -rw-rw-r-- zld/zld          48 2024-10-11 14:13 tmp1011/file3
    -rw-rw-r-- zld/zld          23 2024-10-11 14:10 tmp1011/file2
    

​ 这里,-t表示累出归档文件中包含的内容,-v表示显示详细处理信息(可选),-f指定归档文件的名称,-z表示文件是经过gzip2压缩的。 下面的例子同理,后面不再描述。

  • 查看bzip2压缩的tar文件内容

    zld@zld:~/Codes/shellCode$ tar -jtvf tmp1011.tar.bz2 
    drwxrwxr-x zld/zld           0 2024-10-11 19:34 tmp1011/
    -rw-rw-r-- zld/zld          24 2024-10-11 16:59 tmp1011/file1
    -rw-rw-r-- zld/zld          48 2024-10-11 14:13 tmp1011/file3
    -rw-rw-r-- zld/zld          23 2024-10-11 14:10 tmp1011/file2
    

    这里,-j表示文件是经过bzip2压缩的。

  • 查看xz压缩的tar文件内容

    zld@zld:~/Codes/shellCode$ tar -Jtvf tmp1011.tar.xz 
    drwxrwxr-x zld/zld           0 2024-10-11 19:34 tmp1011/
    -rw-rw-r-- zld/zld          24 2024-10-11 16:59 tmp1011/file1
    -rw-rw-r-- zld/zld          48 2024-10-11 14:13 tmp1011/file3
    -rw-rw-r-- zld/zld          23 2024-10-11 14:10 tmp1011/file2
    

其他用法

  • 向已有的归档文件中添加文件:使用-r选项可以将文件追加到归档文件中。
# 查看test.tar归档文件中的内容,从下面可知,不包含test1.txt和test_file文件 
zld@zld:~/Codes/shellCode$ tar -tvf test.tar 
drwxrwxr-x zld/zld           0 2024-10-11 19:12 testDir/
-rw-rw-r-- zld/zld      241844 2024-10-11 14:54 testDir/cluster.c
-rw-rw-r-- zld/zld          13 2024-10-11 17:21 testDir/test1.txt
drwxr-xr-x zld/zld           0 2024-10-11 18:55 testDir/redis-test/
-rw-r--r-- zld/zld         450 2024-10-11 18:34 testDir/redis-test/test.c.gz
-rwxr-xr-x zld/zld        2284 2024-10-11 18:34 testDir/redis-test/test.gz
drwxrwxr-x zld/zld           0 2024-10-11 19:34 tmp1011/
-rw-rw-r-- zld/zld          24 2024-10-11 16:59 tmp1011/file1
-rw-rw-r-- zld/zld          48 2024-10-11 14:13 tmp1011/file3
-rw-rw-r-- zld/zld          23 2024-10-11 14:10 tmp1011/file2

# 向test.tar文件中添加test1.txt和test_file文件,这里-r选项表示将文件追加到归档文件中。
zld@zld:~/Codes/shellCode$ tar -rvf test.tar test1.txt test_file 
test1.txt
test_file

# 再次查看test.tar归档文件中的内容,已经包含test1.txt和test_file文件
zld@zld:~/Codes/shellCode$ tar -tvf test.tar 
drwxrwxr-x zld/zld           0 2024-10-11 19:12 testDir/
-rw-rw-r-- zld/zld      241844 2024-10-11 14:54 testDir/cluster.c
-rw-rw-r-- zld/zld          13 2024-10-11 17:21 testDir/test1.txt
drwxr-xr-x zld/zld           0 2024-10-11 18:55 testDir/redis-test/
-rw-r--r-- zld/zld         450 2024-10-11 18:34 testDir/redis-test/test.c.gz
-rwxr-xr-x zld/zld        2284 2024-10-11 18:34 testDir/redis-test/test.gz
drwxrwxr-x zld/zld           0 2024-10-11 19:34 tmp1011/
-rw-rw-r-- zld/zld          24 2024-10-11 16:59 tmp1011/file1
-rw-rw-r-- zld/zld          48 2024-10-11 14:13 tmp1011/file3
-rw-rw-r-- zld/zld          23 2024-10-11 14:10 tmp1011/file2
-rw-rw-r-- zld/zld          13 2024-10-10 14:51 test1.txt
-rw-rw-r-- zld/zld         680 2024-10-10 14:59 test_file

这里需要注意的是,只能向未经过压缩的归档文件中添加文件,不能向经过压缩的归档文件中添加文件,比如下面的用法就会报错:

# 试图向经过压缩的文件tmp1011.tar.gz添加文件,报错
zld@zld:~/Codes/shellCode$ tar -rzvf tmp1011.tar.gz test1.txt test_file 
tar: Cannot update compressed archives
Try 'tar --help' or 'tar --usage' for more information.
  • 从归档文件中提取指定文件:使用-x选项并指定要提取的文件名,可以从归档文件中提取指定的文件。例如,从archive.tar.gz文件中提取file1,可以使用命令tar -xzvf archive.tar.gz file1
Linux中常用的文件打包解压缩工具有tar、gzip、bzip2和zip等。 1. 使用tar打包文件 将多个文件或目录打包成一个文件,使用tar命令。例如,将目录/home/user/docs及其子目录打包成一个文件docs.tar: ``` tar -cvf docs.tar /home/user/docs ``` 选项说明: - c:创建一个新的tar包; - v:显示打包过程中处理的文件名; - f:指定打包后的文件名。 2. 使用gzip压缩文件 将tar打包后的文件进行压缩,使用gzip命令。例如,将docs.tar压缩成docs.tar.gz: ``` gzip docs.tar ``` 此时,会生成docs.tar.gz文件,原始的docs.tar文件会被删除。 3. 使用bzip2压缩文件 与gzip类似,使用bzip2命令可以将tar打包后的文件进行压缩。例如,将docs.tar压缩成docs.tar.bz2: ``` bzip2 docs.tar ``` 此时,会生成docs.tar.bz2文件,原始的docs.tar文件会被删除。 4. 使用zip压缩文件 除了tar、gzip和bzip2,Linux中也支持zip格式的文件压缩。使用zip命令可以将文件或目录打包成一个zip文件。例如,将目录/home/user/docs及其子目录打包成一个文件docs.zip: ``` zip -r docs.zip /home/user/docs ``` 选项说明: - r:递归处理子目录和文件。 5. 解压缩文件 解压缩文件使用的命令与压缩文件的命令相对应。例如,解压缩docs.tar.gz文件: ``` tar -xvf docs.tar.gz ``` 选项说明: - x:解包操作; - v:显示解包过程中处理的文件名; - f:指定要解包的文件解压缩其他格式的文件时,使用相应的命令即可。例如,解压缩docs.zip文件: ``` unzip docs.zip ``` 以上就是Linux文件打包解压缩的基本操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值