初学者 Linux xz 命令教程(7 个示例)初学者 Linux xz 命令教程(7 个示例)初学者 Linux xz 命令教程(7 个示例)初学者 Linux xz 命令教程(7 个示例)
  • 文章
  • 正则表达式
    • 工具
  • 登录
找到的结果: {phrase} (显示: {results_count} 共: {results_count_total})
显示: {results_count} 共: {results_count_total}

加载更多搜索结果...

搜索范围
模糊匹配
搜索标题
搜索内容
发表 admin at 2025年2月28日
类别
  • 未分类
标签

初学者 Linux xz 命令教程(7 个示例)

在此页

  1. Linux xz 命令
  2. Q1。如何使用 xz 命令?
  3. Q2。如何让 xz 也保留原始文件?
  4. Q3。如何压缩多个文件?
  5. Q4。如何解压 .xz 文件?
  6. Q5。如何让xz打印压缩文件的信息?
  7. Q6。如何指定不同的压缩/解压格式?
  8. Q7。如何让xz显示进度条?
  9. 结论

在 Linux 中有几种压缩和解压缩文件的方法。虽然我们已经讨论了一些(这里),但还有很多我们还没有讨论。所以在这里,在本教程中,我们将讨论另一个这样的命令行实用程序。

它被称为 xz。我们将讨论此命令行工具的基础知识。但在我们开始之前,值得一提的是,这里的所有示例都已经在 Ubuntu 18.04 LTS 机器上进行了测试。

Linux xz命令

如开头所述,Linux 中的 xz 命令可让您压缩和解压缩文件。以下是它的语法:

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

这是手册页必须说明的内容:

     xz is a general-purpose data compression tool with command line syntax similar to gzip(1) 
and bzip2(1).  The native file format is the .xz format, but the legacy .lzma format used
by LZMA Utils and raw compressed streams with no container format headers are also supported.

     xz compresses or decompresses each file according to the selected operation mode.  If no files
are given or file  is  -,  xz  reads from standard input and writes the processed data to
standard output.  xz will refuse (display an error and skip the file) to write compressed data
to standard output if it is a terminal.  Similarly, xz will refuse to read compressed data
from standard  input if it is a terminal.

以下是一些 Q&A 风格的示例,应该可以让您更好地了解 xz 的工作原理。

Q1。如何使用 xz 命令?

基本用法相当简单。只需将文件名(即要压缩的文件名)作为输入参数传递给 xz。这是一个例子:

xz file.txt

在我的系统上,上述命令产生了以下文件作为输出。

file.txt.xz

所以可以看到xz命令压缩后的file.txt。值得一提的是,该操作将原始文件(在本例中为 file.txt)替换为压缩版本。

Q2。如何让 xz 也保留原始文件?

正如我在上一节中提到的,xz 命令将原始文件替换为其压缩版本。但是,如果需要,您也可以强制 xz 保留原始文件。这可以使用 -k 命令行选项来完成。

例如:

xz -k file.txt

所以这一次,您会在当前工作目录中找到 file.txt 和 file.txt.xz。

Q3.如何压缩多个文件?

这很简单。只需将名称作为输入参数传递给 xz。

例如:

xz file1.txt file2.txt

此命令将一次性压缩这两个文件。

Q4.如何解压 .xz 文件?

要解压缩 .xz 文件,请使用 -d 命令行选项。例如:

xz -d file.txt.xz

此命令将在当前工作目录中生成 file.txt。

Q5.如何让xz打印压缩文件的信息?

这可以使用 -l 命令行选项来完成。例如:

xz -l file.txt.xz

在我的案例中,此命令产生了以下信息:

Strms  Blocks   Compressed Uncompressed  Ratio  Check   Filename
    1       1         96 B         37 B  2.595  CRC64   file.txt.xz

Q6.如何指定不同的压缩/解压格式?

这可以使用 -F 命令行选项来完成。不过,要使用此选项,您需要了解以下内容:

-F format, --format=format
       Specify the file format to compress or decompress:

       auto   This is the default.  When compressing, auto is equivalent to xz.  When decompressing, the format of the  input  file
              is automatically detected.  Note that raw streams (created with --format=raw) cannot be auto-detected.

       xz     Compress to the .xz file format, or accept only .xz files when decompressing.

       lzma, alone
              Compress  to the legacy .lzma file format, or accept only .lzma files when decompressing.  The alternative name alone
              is provided for backwards compatibility with LZMA Utils.

       raw    Compress or uncompress a raw stream (no headers).  This is meant for advanced users only.  To decode raw streams, you
              need use --format=raw and explicitly specify the filter chain, which normally would have been stored in the container
              headers.

所以你看,你可以使用以下任何一种格式:auto、xz、lzma 和 raw。

Q7.如何让xz显示进度条?

这可以使用 -v 命令行选项来实现。这是一个例子:

结论

虽然我们在本教程中讨论了一些 xz 命令行选项,但还有更多。一旦您理解并实践了这些,请前往工具手册页了解更多信息。

©2015-2025 艾丽卡 support@alaica.com