Linux RAR 命令详解

压缩和解压 RAR 文件

RAR 是一种用于创建和提取压缩文件 (.rar 格式) 的流行工具。当我们从网络下载压缩文件时, 通常需要使用 RAR 工具来解压它们。

本文将介绍如何在 Linux 系统上使用官方提供的二进制文件安装 rarunrar 命令行工具, 以便打开、提取和解压缩 RAR 压缩文件。

安装和配置

下载和安装

要安装 RAR 工具, 请前往 RARLAB 网站下载最新的 unrar/rar 文件, 然后按照以下步骤操作

# 下载 RAR 工具的 Linux 版本并解压
wget https://www.rarlab.com/rar/rarlinux-x64-610.tar.gz
tar -xf rarlinux-x64-610.tar.gz

# 进入解压后的目录
cd rar

使用 root 用户安装

# 将 rar 和 unrar 二进制文件复制到系统路径
cp -v rar unrar /usr/local/bin/

普通用户安装

# 创建本地 bin 目录
mkdir -p $HOME/.local/bin

# 将本地 bin 目录添加到 PATH 环境变量
export PATH="$HOME/.local/bin:$PATH"

# 创建 rar 和 unrar 的符号链接
ln -s $HOME/rar/rar $HOME/.local/bin/rar
ln -s $HOME/rar/unrar $HOME/.local/bin/unrar

注册 RAR

要注册 RAR 工具, 你需要将许可证文件 rarreg.key 复制到用户目录 /home/user 或以下任意目录之一

  • /etc
  • /usr/lib
  • /usr/local/lib
  • /usr/local/etc

你也可以选择将 rarreg.key 文件重命名为 .rarreg.key.rarregkey, 这些名称同样有效。

网上搜集到的可用注册码

rarreg.key 文件放置在上述位置后, RAR 工具将在使用时自动读取注册信息, 从而解锁完整功能

RAR 6.10   Copyright (c) 1993-2022 Alexander Roshal   24 Jan 2022
Registered to Federal Agency for Education


RAR registration data
Federal Agency for Education
1000000 PC usage license
UID=b621cca9a84bc5deffbf
6412612250ffbf533df6db2dfe8ccc3aae5362c06d54762105357d
5e3b1489e751c76bf6e0640001014be50a52303fed29664b074145
7e567d04159ad8defc3fb6edf32831fd1966f72c21c0c53c02fbbb
2f91cfca671d9c482b11b8ac3281cb21378e85606494da349941fa
e9ee328f12dc73e90b6356b921fbfb8522d6562a6a4b97e8ef6c9f
fb866be1e3826b5aa126a4d2bfe9336ad63003fc0e71c307fc2c60
64416495d4c55a0cc82d402110498da970812063934815d81470829275

第二个

RAR 6.10   Copyright (c) 1993-2022 Alexander Roshal   24 Jan 2022
Registered to WinRAR


RAR registration data
WinRAR
Unlimited Company License
UID=4b914fb772c8376bf571
6412212250f5711ad072cf351cfa39e2851192daf8a362681bbb1d
cd48da1d14d995f0bbf960fce6cb5ffde62890079861be57638717
7131ced835ed65cc743d9777f2ea71a8e32c7e593cf66794343565
b41bcf56929486b8bcdac33d50ecf773996052598f1f556defffbd
982fbe71e93df6b6346c37a3890f3c7edc65d7f5455470d13d1190
6e6fb824bcf25f155547b5fc41901ad58c0992f570be1cf5608ba9
aef69d48c864bcd72d15163897773d314187f6a9af350808719796

第三个

RAR 6.10   Copyright (c) 1993-2022 Alexander Roshal   24 Jan 2022
Registered to State Grid Corporation Of China


RAR registration data
State Grid Corporation Of China
50000 PC usage license
UID=5827a0bd1c43525d0a5d
64122122500a5d3d56f784f3a440ac3fb632d34e08bbaa37fc7712
6acaeb8eb044810272e86042cb7c79b1da0eaf88c79f8a7c6dd77b
dba335e27a109997ac90fb0e10e4129e79f46c42b4ee1832fa5113
7443fcc1124840d4dd36f3af84a5c915a760b18c6394f938168227
fbf29edbc4b34ef85ee53fbfca71814a82afadf073876b4b033451
b6292a7cc7975b3ff3cc73404abbf7c126787344169eeae4609f62
c9ffbc159bf2640ad5d9b88f8fa9d9cbf2b7e5b022a21938465244

常用操作

文件夹原始结构, 以下是一个压缩包的文件夹结构示例

$ tree
├── acesheep
|   ├── abc.txt
|   ├── index.html
|   ├── index.php
|   └── xyz.txt
├── default
|   ├── index.html
|   └── index.php
├── include
|   └── abc.txt
└── php
    └── xyz.txt

4 directories, 8 files

解压 RAR 文件

解压并忽略目录结构

使用 unrar e 命令可以提取文件 (忽略目录结构)

  • e 选项会忽略压缩包中的路径信息, 将所有内容解压到目录
  • 如果解压时遇到同名文件, 程序会提示是否覆盖

将所有内容解压到当前文件夹

$ unrar e acesheep.rar

UNRAR 6.10 freeware      Copyright (c) 1993-2022 Alexander Roshal


Extracting from acesheep.rar

Extracting  xyz.txt                                                   OK
Extracting  index.html                                                OK
Extracting  index.php                                                 OK
Extracting  abc.txt                                                   OK

Would you like to replace the existing file index.html
     0 bytes, modified on 2022-02-25 10:33
with a new one
     0 bytes, modified on 2022-02-25 10:34

[Y]es, [N]o, [A]ll, n[E]ver, [R]ename, [Q]uit a

Extracting  index.html                                                OK
Extracting  index.php                                                 OK
Extracting  abc.txt                                                   OK
Extracting  xyz.txt                                                   OK
All OK

将所有内容解压到指定路径

$ unrar e acesheep.rar ace/

UNRAR 6.10 freeware      Copyright (c) 1993-2022 Alexander Roshal


Extracting from acesheep.rar

Extracting  ace/xyz.txt                                               OK
Extracting  ace/index.html                                            OK
Extracting  ace/index.php                                             OK
Extracting  ace/abc.txt                                               OK

Would you like to replace the existing file ace/index.html
     0 bytes, modified on 2022-02-25 10:33
with a new one
     0 bytes, modified on 2022-02-25 10:34

[Y]es, [N]o, [A]ll, n[E]ver, [R]ename, [Q]uit a

Extracting  ace/index.html                                            OK
Extracting  ace/index.php                                             OK
Extracting  ace/abc.txt                                               OK
Extracting  ace/xyz.txt                                               OK
All OK

解压并保留目录结构

使用 unrar x 命令可以保留压缩包中的原始目录结构进行解压。一般情况下使用 x 解压

程序会根据压缩包中的目录结构创建相应的文件夹并提取文件, 请看下面的命令输出

$ unrar x acesheep.rar

UNRAR 6.10 freeware      Copyright (c) 1993-2022 Alexander Roshal


Extracting from acesheep.rar

Creating    acesheep                                                  OK
Extracting  acesheep/xyz.txt                                          OK
Extracting  acesheep/index.html                                       OK
Extracting  acesheep/index.php                                        OK
Extracting  acesheep/abc.txt                                          OK
Creating    default                                                   OK
Extracting  default/index.html                                        OK
Extracting  default/index.php                                         OK
Creating    include                                                   OK
Extracting  include/abc.txt                                           OK
Creating    php                                                       OK
Extracting  php/xyz.txt                                               OK
All OK

查看压缩包内容

使用 unrar l 列出存档文件中的内容。它将显示文件列表及其 大小日期时间权限

$ unrar l acesheep.rar

UNRAR 6.10 freeware      Copyright (c) 1993-2022 Alexander Roshal

Archive: acesheep.rar
Details: RAR 5

 Attributes      Size     Date    Time   Name
----------- ---------  ---------- -----  ----
 -rw-r--r--         0  2022-02-25 10:33  acesheep/xyz.txt
 -rw-r--r--         0  2022-02-25 10:33  acesheep/index.html
 -rw-r--r--         0  2022-02-25 10:33  acesheep/index.php
 -rw-r--r--         0  2022-02-25 10:33  acesheep/abc.txt
 -rw-r--r--         0  2022-02-25 10:34  default/index.html
 -rw-r--r--         0  2022-02-25 10:34  default/index.php
 -rw-r--r--         0  2022-02-25 10:37  include/abc.txt
 -rw-r--r--         0  2022-02-25 10:37  php/xyz.txt
----------- ---------  ---------- -----  ----
                    0                    8

测试压缩包完整性

使用 unrar t 检查存档文件是否损坏, 检查完整性。该命令将对压缩包中的每个文件执行完整性检查, 并显示文件的状态

$ unrar t acesheep.rar

UNRAR 6.10 freeware      Copyright (c) 1993-2022 Alexander Roshal


Testing archive acesheep.rar

Testing     acesheep/xyz.txt                                          OK
Testing     acesheep/index.html                                       OK
Testing     acesheep/index.php                                        OK
Testing     acesheep/abc.txt                                          OK
Testing     default/index.html                                        OK
Testing     default/index.php                                         OK
Testing     include/abc.txt                                           OK
Testing     php/xyz.txt                                               OK
All OK

创建 RAR 压缩文件

使用 rar a 创建新的 RAR 文件。unrar 命令仅支持提取、列出或测试压缩文件, 无法用于创建 RAR 压缩文件。要创建 RAR 文件, 需要使用 rar 命令。

以下命令会将文件夹 acesheep 压缩为 acesheep.rar

$ rar a acesheep.rar acesheep

RAR 6.10   Copyright (c) 1993-2022 Alexander Roshal   24 Jan 2022
Registered to WinRAR

Updating archive acesheep.rar

Updating  acesheep/xyz.txt                                            OK
Updating  acesheep/index.html                                         OK
Updating  acesheep/index.php                                          OK
Updating  acesheep/abc.txt                                            OK
Adding    acesheep                                                    OK
Done

在压缩过程中, rar 命令会显示每个文件的压缩状态。如果压缩成功, 会显示 OK。压缩完成后, acesheep.rar 文件将包含整个 acesheep 文件夹及其内容。

删除压缩包中的文件

使用 rar d 从存档中删除特定文件。

例如, 以下命令会从 acesheep.rar 压缩包中删除文件 acesheep/abc.txt

rar d acesheep.rar acesheep/abc.txt

更新压缩包

使用 rar u 更新现有存档文件。如果需要向现有的 RAR 压缩文件中更新添加文件

例如, 以下命令会将文件 acesheep.sql 添加到现有的 acesheep.rar 压缩包中

$ rar u acesheep.rar acesheep.sql

RAR 6.10   Copyright (c) 1993-2022 Alexander Roshal   24 Jan 2022
Registered to WinRAR

Updating archive acesheep.rar

Adding    acesheep.sql                                                OK
Done

现在, 可以使用 rar l 命令列出压缩包的内容, 以确认文件 acesheep.sql 是否已成功添加到压缩文件中

$ rar l acesheep.rar

RAR 6.10   Copyright (c) 1993-2022 Alexander Roshal   24 Jan 2022
Registered to WinRAR

Archive: acesheep.rar
Details: RAR 5

 Attributes      Size     Date    Time   Name
----------- ---------  ---------- -----  ----
 -rw-r--r--         0  2022-02-25 10:33  acesheep/xyz.txt
 -rw-r--r--         0  2022-02-25 10:33  acesheep/index.html
 -rw-r--r--         0  2022-02-25 10:33  acesheep/index.php
 -rw-r--r--         0  2022-02-25 10:33  acesheep/abc.txt
 drwxr-xr-x         0  2022-02-25 12:27  acesheep
 -rw-r--r--         0  2022-02-25 13:36  acesheep.sql
----------- ---------  ---------- -----  ----
                    0                    6

从输出中可以看到, acesheep.sql 文件已经成功添加到压缩包中

修复压缩包

使用 rar r 修复损坏的存档文件。当 RAR 压缩包损坏时, 修复操作会在同一目录下创建一个修复后的文件, 通常命名为 rebuilt.<原文件名>.rar 例如 rebuilt.acesheep.rar 文件

$ rar r acesheep.rar

RAR 6.10   Copyright (c) 1993-2022 Alexander Roshal   24 Jan 2022
Registered to WinRAR

Data recovery record not found
Reconstructing acesheep.rar
Building rebuilt.acesheep.rar    8%
Found  acesheep/xyz.txt  26%
Found  acesheep/index.html  45%
Found  acesheep/index.php  64%
Found  acesheep/abc.txt  82%
Found  acesheep 100%
Done

添加恢复记录

为了提高压缩包的容错性, 可以在创建压缩包时添加恢复记录。使用 -rr 参数即可, 默认情况下恢复记录为 3%, 等价于 -rr3p。建议选择 3% 至 10% 的恢复记录比例, 以平衡文件大小和恢复能力。

rar a -rr5p acesheep.rar acesheep

上述命令会在创建 acesheep.rar 压缩包时, 添加 5% 的恢复记录, 从而提高修复成功率

高级功能

创建带密码的压缩文件

使用 rar a -p 创建带密码的压缩文件。可以对压缩文件进行密码保护。在命令中直接指定密码, 例如 rar a -p51522zzwlwlbb, 其中密码为 51522zzwlwlbb

$ rar a -p acesheep.rar acesheep.sql

Enter password (will not be echoed):

Reenter password:


RAR 6.10   Copyright (c) 1993-2022 Alexander Roshal   24 Jan 2022
Registered to WinRAR

Updating archive acesheep.rar

Updating  acesheep.sql                                                OK
Done

验证密码保护

通过提取压缩文件来验证是否设置了密码。如果文件受密码保护, 提取时会提示输入密码

$ rar x acesheep.rar

RAR 6.10   Copyright (c) 1993-2022 Alexander Roshal   24 Jan 2022
Registered to WinRAR


Extracting from acesheep.rar

Creating    acesheep                                                  OK
Extracting  acesheep/xyz.txt                                          OK
Extracting  acesheep/index.html                                       OK
Extracting  acesheep/index.php                                        OK
Extracting  acesheep/abc.txt                                          OK
Enter password (will not be echoed) for acesheep.sql:

Extracting  acesheep.sql                                              OK
All OK

注意事项

  • 如果密码直接包含在命令中 (如 -p51522zzwlwlbb), 会有安全隐患, 因为命令可能被系统日志或其他工具记录
  • 推荐使用交互式方式输入密码, 以提高安全性

锁定压缩文件

使用 rar k 锁定存档以防止修改。RAR 提供了一个选项, 可以锁定压缩文件以防止其被更新、添加或修改。通过锁定, 你可以保护重要的存档文件, 避免意外更改。

锁定后的存档文件无法通过 WinRAR 或其他工具进行修改。

$ rar k acesheep.rar

RAR 6.10   Copyright (c) 1993-2022 Alexander Roshal   24 Jan 2022
Registered to WinRAR


Processing archive acesheep.rar
Locking archive
Done

注意事项

  • 锁定后, 存档文件处于只读状态, 任何更新或修改操作都会被拒绝
  • 锁定操作不可逆, 确保在锁定前已完成所有必要的更新或修改

分卷压缩

使用 -v 参数可以创建分卷压缩文件, 支持多种单位 (如 kmg 等)。分卷压缩适用于将大文件分割成多个小文件, 便于存储或传输。

分卷压缩参数说明

-v<n>[k|b|f|m|M|g|G] - 设置分卷大小

  • 默认单位: <n> 乘以 1000 字节为单位计算 (非 1024 字节)

  • 单位符号说明

    符号
    k KiB = 1024 bytes
    b bytes = 1 bytes
    f 软盘大小 (360、720、1200、1440 或 2880 KiB)。如果省略大小, 将使用自动检测。
    m MiB = 1024 KiB
    M MB = 1000 KiB
    g GiB = 1024 MiB
    G GB = 1000 MiB
  • 支持小数: 可以使用小数点指定分卷大小, 例如 -v1.5g 表示 1.5 GiB

  • 多级分卷: 可以通过多个 -v 参数为不同分卷设置不同大小

为不同分卷设置大小

rar a -v100k -v200k -v300k acesheep.rar
  • 第一个分卷大小为 100 KiB
  • 第二个分卷大小为 200 KiB
  • 后续所有的分卷大小为 300 KiB

验证分卷大小

# 使用 -v10G 创建 10 GB 分卷
$ ls -all mysql-5.7.31-winx64.part01.rar
-rw-r--r-- 1 user user 10000000000 Feb 22 01:51 mysql-5.7.31-winx64.part01.rar

# 使用 -v10g 创建 10 GiB 分卷
$ ls -all mysql-5.7.31-winx64.part01.rar
-rw-r--r-- 1 user user 10737418240 Feb 22 14:09 mysql-5.7.31-winx64.part01.rar

指定的单位会影响分卷的实际大小, 请根据需求选择合适的单位

实际运用示例

在实际操作中, 可以结合多个参数来优化压缩效率和分卷大小。以下命令展示了一个典型的应用场景

目录结构

[root@server temp]# tree -L 1
temp
├── mysql-5.7.31-winx64.rar
└── mysql-5.7.31-winx64/

压缩命令

进入需要压缩文件的上层目录 temp 文件夹后运行以下命令

rar a -m5 -ma5 -md128MB -mt2 -v10g -r -k mysql-5.7.31-winx64.rar mysql-5.7.31-winx64/

参数说明

参数 作用
-m5 使用最高压缩级别 5
-ma5 使用 RAR5 存档格式
-md128MB 设置字典大小为 128 MB
-mt2 使用 2 个线程进行压缩
-v10g 分卷压缩, 每个分卷大小为 10 GiB
-r 递归处理子目录
-k 锁定压缩文件, 禁止后续修改

注意事项

  • 压缩线程数: 根据 CPU 核心数量调整 -mt 参数以提高压缩效率
  • 字典大小: -md 参数设置的字典大小会影响压缩率和内存使用, 推荐值为 128 MB 或更高

不在同一层目录时压缩

当我们不在需要压缩的文件上层目录时, 可以使用绝对路径来压缩目标文件夹。但如果不加注意, 这种方式会将整个绝对路径同时存储到压缩包中, 可能导致路径层级过深的问题。通过调整参数, 可以避免这种情况。

示例目录结构

假设目录结构如下, 我们当前位于 WorkFolder, 使用绝对路径压缩 FolderToArchive 文件夹

[root@server ~]# tree temp
temp
├── FolderToArchive
│   ├── File1
│   ├── File2
│   ├── File3
│   ├── FolderA
│   │   ├── FileX
│   │   ├── FileY
│   │   └── FileZ
│   └── FolderB
│       ├── FileA
│       ├── FileB
│       └── FileC
└── WorkFolder

4 directories, 9 files

错误示范

如果直接使用以下命令压缩。虽然我们没有指定, 但是参数中有 -ep2 默认参数

[root@server WorkFolder]# rar a -m5 -ma5 -md128MB -r -k acesheep.rar /temp/FolderToArchive/

RAR 6.10   Copyright (c) 1993-2022 Alexander Roshal   24 Jan 2022
Registered to WinRAR

Creating archive acesheep.rar

Adding    /temp/FolderToArchive/File2                            OK
Adding    /temp/FolderToArchive/File1                            OK
Adding    /temp/FolderToArchive/FolderA/FileX                    OK
Adding    /temp/FolderToArchive/FolderA/FileY                    OK
Adding    /temp/FolderToArchive/FolderA/FileZ                    OK
Adding    /temp/FolderToArchive/File3                            OK
Adding    /temp/FolderToArchive/FolderB/FileB                    OK
Adding    /temp/FolderToArchive/FolderB/FileA                    OK
Adding    /temp/FolderToArchive/FolderB/FileC                    OK
Adding    /temp/FolderToArchive/FolderA                          OK
Adding    /temp/FolderToArchive/FolderB                          OK
Locking archive
Done

检查压缩包的文件列表时, 可以看到压缩时的绝对路径中的所有文件夹都被完整保存了进去。这种方式会使文件夹层数变得非常多, 因此极其不推荐使用。

路径为: /temp/FolderToArchive/, 压缩包内路径为 temp/FolderToArchive/

[root@server WorkFolder]# rar l acesheep.rar

RAR 6.10   Copyright (c) 1993-2022 Alexander Roshal   24 Jan 2022
Registered to WinRAR

Archive: acesheep.rar
Details: RAR 5, lock

 Attributes      Size     Date    Time   Name
----------- ---------  ---------- -----  ----
 -rw-r--r--         0  2022-03-28 22:44  temp/FolderToArchive/File2
 -rw-r--r--         0  2022-03-28 22:44  temp/FolderToArchive/File1
 -rw-r--r--         0  2022-03-28 22:44  temp/FolderToArchive/FolderA/FileX
 -rw-r--r--         0  2022-03-28 22:44  temp/FolderToArchive/FolderA/FileY
 -rw-r--r--         0  2022-03-28 22:44  temp/FolderToArchive/FolderA/FileZ
 -rw-r--r--         0  2022-03-28 22:44  temp/FolderToArchive/File3
 -rw-r--r--         0  2022-03-28 22:44  temp/FolderToArchive/FolderB/FileB
 -rw-r--r--         0  2022-03-28 22:44  temp/FolderToArchive/FolderB/FileA
 -rw-r--r--         0  2022-03-28 22:44  temp/FolderToArchive/FolderB/FileC
 drwxr-xr-x         0  2022-03-28 22:44  temp/FolderToArchive/FolderA
 drwxr-xr-x         0  2022-03-28 22:44  temp/FolderToArchive/FolderB
----------- ---------  ---------- -----  ----
                    0                    11

命令稍作修改, 添加 -ep1 参数。-ep1 参数会移除绝对路径, 仅保留目标文件夹的内容

[root@server WorkFolder]# rar a -m5 -ma5 -md128MB -r -k -ep1 acesheep.rar /temp/FolderToArchive/

RAR 6.10   Copyright (c) 1993-2022 Alexander Roshal   24 Jan 2022
Registered to WinRAR

Creating archive acesheep.rar

Adding    /temp/FolderToArchive/File2                            OK
Adding    /temp/FolderToArchive/File1                            OK
Adding    /temp/FolderToArchive/FolderA/FileX                    OK
Adding    /temp/FolderToArchive/FolderA/FileY                    OK
Adding    /temp/FolderToArchive/FolderA/FileZ                    OK
Adding    /temp/FolderToArchive/File3                            OK
Adding    /temp/FolderToArchive/FolderB/FileB                    OK
Adding    /temp/FolderToArchive/FolderB/FileA                    OK
Adding    /temp/FolderToArchive/FolderB/FileC                    OK
Adding    /temp/FolderToArchive/FolderA                          OK
Adding    /temp/FolderToArchive/FolderB                          OK
Locking archive
Done

此时压缩包中只包含了绝对路径中的子目录和文件, 但却排除了 FolderToArchive 目标文件夹本身。如果想要将 FolderToArchive 一并包含在压缩包中, 该怎么办呢?

[root@server WorkFolder]# rar l acesheep.rar

RAR 6.10   Copyright (c) 1993-2022 Alexander Roshal   24 Jan 2022
Registered to WinRAR

Archive: acesheep.rar
Details: RAR 5, lock

 Attributes      Size     Date    Time   Name
----------- ---------  ---------- -----  ----
 -rw-r--r--         0  2022-03-28 22:44  File2
 -rw-r--r--         0  2022-03-28 22:44  File1
 -rw-r--r--         0  2022-03-28 22:44  FolderA/FileX
 -rw-r--r--         0  2022-03-28 22:44  FolderA/FileY
 -rw-r--r--         0  2022-03-28 22:44  FolderA/FileZ
 -rw-r--r--         0  2022-03-28 22:44  File3
 -rw-r--r--         0  2022-03-28 22:44  FolderB/FileB
 -rw-r--r--         0  2022-03-28 22:44  FolderB/FileA
 -rw-r--r--         0  2022-03-28 22:44  FolderB/FileC
 drwxr-xr-x         0  2022-03-28 22:44  FolderA
 drwxr-xr-x         0  2022-03-28 22:44  FolderB
----------- ---------  ---------- -----  ----
                    0                    11

去掉路径末尾的斜杠 /

要将目标文件夹 FolderToArchive 一并包含在压缩包中, 需要删除绝对路径末尾的 /

需要注意的是, 使用 TAB 键自动补全目录时, 系统会在文件夹末尾自动添加一个斜杠。

[root@server WorkFolder]# rar a -m5 -ma5 -md128MB -r -k -ep1 acesheep.rar /temp/FolderToArchive

RAR 6.10   Copyright (c) 1993-2022 Alexander Roshal   24 Jan 2022
Registered to WinRAR

Creating archive acesheep.rar

Adding    /temp/FolderToArchive/File2                            OK
Adding    /temp/FolderToArchive/File1                            OK
Adding    /temp/FolderToArchive/FolderA/FileX                    OK
Adding    /temp/FolderToArchive/FolderA/FileY                    OK
Adding    /temp/FolderToArchive/FolderA/FileZ                    OK
Adding    /temp/FolderToArchive/File3                            OK
Adding    /temp/FolderToArchive/FolderB/FileB                    OK
Adding    /temp/FolderToArchive/FolderB/FileA                    OK
Adding    /temp/FolderToArchive/FolderB/FileC                    OK
Adding    /temp/FolderToArchive/FolderA                          OK
Adding    /temp/FolderToArchive/FolderB                          OK
Adding    /temp/FolderToArchive                                  OK
Locking archive
Done

此时压缩包内的目录结构正确, 竟然是预期的目录结构

[root@server WorkFolder]# rar l acesheep.rar

RAR 6.10   Copyright (c) 1993-2022 Alexander Roshal   24 Jan 2022
Registered to WinRAR

Archive: acesheep.rar
Details: RAR 5, lock

 Attributes      Size     Date    Time   Name
----------- ---------  ---------- -----  ----
 -rw-r--r--         0  2022-03-28 22:44  FolderToArchive/File2
 -rw-r--r--         0  2022-03-28 22:44  FolderToArchive/File1
 -rw-r--r--         0  2022-03-28 22:44  FolderToArchive/FolderA/FileX
 -rw-r--r--         0  2022-03-28 22:44  FolderToArchive/FolderA/FileY
 -rw-r--r--         0  2022-03-28 22:44  FolderToArchive/FolderA/FileZ
 -rw-r--r--         0  2022-03-28 22:44  FolderToArchive/File3
 -rw-r--r--         0  2022-03-28 22:44  FolderToArchive/FolderB/FileB
 -rw-r--r--         0  2022-03-28 22:44  FolderToArchive/FolderB/FileA
 -rw-r--r--         0  2022-03-28 22:44  FolderToArchive/FolderB/FileC
 drwxr-xr-x         0  2022-03-28 22:44  FolderToArchive/FolderA
 drwxr-xr-x         0  2022-03-28 22:44  FolderToArchive/FolderB
 drwxr-xr-x         0  2022-03-28 22:44  FolderToArchive
----------- ---------  ---------- -----  ----
                    0                    12

重命名压缩包内的文件夹

如果当文件夹名称中包含空格或特殊字符时, 希望在压缩时重命名文件夹, 可以结合 -ep1-ap<path> 参数。例如, 将文件夹重命名为 RenameFolder

[root@server WorkFolder]# rar a -m5 -ma5 -md128MB -r -k -ep1 -apRenameFolder acesheep.rar /temp/FolderToArchive/

RAR 6.10   Copyright (c) 1993-2022 Alexander Roshal   24 Jan 2022
Registered to WinRAR

Creating archive acesheep.rar

Adding    /temp/FolderToArchive/File2                            OK
Adding    /temp/FolderToArchive/File1                            OK
Adding    /temp/FolderToArchive/FolderA/FileX                    OK
Adding    /temp/FolderToArchive/FolderA/FileY                    OK
Adding    /temp/FolderToArchive/FolderA/FileZ                    OK
Adding    /temp/FolderToArchive/File3                            OK
Adding    /temp/FolderToArchive/FolderB/FileB                    OK
Adding    /temp/FolderToArchive/FolderB/FileA                    OK
Adding    /temp/FolderToArchive/FolderB/FileC                    OK
Adding    /temp/FolderToArchive/FolderA                          OK
Adding    /temp/FolderToArchive/FolderB                          OK
Locking archive
Done

这样压缩包内就是新的文件夹名称了, 完美!

[root@server WorkFolder]# rar l acesheep.rar

RAR 6.10   Copyright (c) 1993-2022 Alexander Roshal   24 Jan 2022
Registered to WinRAR

Archive: acesheep.rar
Details: RAR 5, lock

 Attributes      Size     Date    Time   Name
----------- ---------  ---------- -----  ----
 -rw-r--r--         0  2022-03-28 22:44  RenameFolder/File2
 -rw-r--r--         0  2022-03-28 22:44  RenameFolder/File1
 -rw-r--r--         0  2022-03-28 22:44  RenameFolder/FolderA/FileX
 -rw-r--r--         0  2022-03-28 22:44  RenameFolder/FolderA/FileY
 -rw-r--r--         0  2022-03-28 22:44  RenameFolder/FolderA/FileZ
 -rw-r--r--         0  2022-03-28 22:44  RenameFolder/File3
 -rw-r--r--         0  2022-03-28 22:44  RenameFolder/FolderB/FileB
 -rw-r--r--         0  2022-03-28 22:44  RenameFolder/FolderB/FileA
 -rw-r--r--         0  2022-03-28 22:44  RenameFolder/FolderB/FileC
 drwxr-xr-x         0  2022-03-28 22:44  RenameFolder/FolderA
 drwxr-xr-x         0  2022-03-28 22:44  RenameFolder/FolderB
----------- ---------  ---------- -----  ----
                    0                    11

完整帮助

RAR 6.10   Copyright (c) 1993-2022 Alexander Roshal   24 Jan 2022
Registered to WinRAR

Usage:     rar <command> -<switch 1> -<switch N> <archive> <files...>
               <@listfiles...> <path_to_extract\>

<Commands>
  a             Add files to archive
  c             Add archive comment
  ch            Change archive parameters
  cw            Write archive comment to file
  d             Delete files from archive
  e             Extract files without archived paths
  f             Freshen files in archive
  i[par]=<str>  Find string in archives
  k             Lock archive
  l[t[a],b]     List archive contents [technical[all], bare]
  m[f]          Move to archive [files only]
  p             Print file to stdout
  r             Repair archive
  rc            Reconstruct missing volumes
  rn            Rename archived files
  rr[N]         Add data recovery record
  rv[N]         Create recovery volumes
  s[name|-]     Convert archive to or from SFX
  t             Test archive files
  u             Update files in archive
  v[t[a],b]     Verbosely list archive contents [technical[all],bare]
  x             Extract files with full path

<Switches>
  -             Stop switches scanning
  @[+]          Disable [enable] file lists
  ad[1,2]       Alternate destination path
  ag[format]    Generate archive name using the current date
  ai            Ignore file attributes
  ap<path>      Set path inside archive
  as            Synchronize archive contents
  c-            Disable comments show
  cfg-          Disable read configuration
  cl            Convert names to lower case
  cu            Convert names to upper case
  df            Delete files after archiving
  dh            Open shared files
  ds            Disable name sort for solid archive
  dw            Wipe files after archiving
  e[+]<attr>    Set file exclude and include attributes
  ed            Do not add empty directories
  ep            Exclude paths from names
  ep1           Exclude base directory from names, 压缩包第一层直接是文件夹内容
  ep2           默认值, 不用指定
  ep3           Expand paths to full including the drive letter
  ep4<path>     Exclude the path prefix from names
  f             Freshen files
  hp[password]  Encrypt both file data and headers
  ht[b|c]       Select hash type [BLAKE2,CRC32] for file checksum
  id[c,d,n,p,q] Display or disable messages
  ierr          Send all messages to stderr
  ilog[name]    Log errors to file
  inul          Disable all messages
  isnd[-]       Control notification sounds
  iver          Display the version number
  k             Lock archive
  kb            Keep broken extracted files
  log[f][=name] Write names to log file
  m<0..5>       Set compression level (0-store...3-default...5-maximal)
  ma[4|5]       Specify a version of archiving format
  mc<par>       Set advanced compression parameters
  md<n>[k,m,g]  Dictionary size in KB, MB or GB
  me[par]       Set encryption parameters
  ms[ext;ext]   Specify file types to store
  mt<threads>   Set the number of threads
  n<file>       Additionally filter included files
  n@            Read additional filter masks from stdin
  n@<list>      Read additional filter masks from list file
  o[+|-]        Set the overwrite mode
  oh            Save hard links as the link instead of the file
  oi[0-4][:min] Save identical files as references
  ol[a]         Process symbolic links as the link [absolute paths]
  op<path>      Set the output path for extracted files
  or            Rename files automatically
  ow            Save or restore file owner and group
  p[password]   Set password
  qo[-|+]       Add quick open information [none|force]
  r             Recurse subdirectories
  r-            Disable recursion
  r0            Recurse subdirectories for wildcard names only
  rr[N]         Add data recovery record
  rv[N]         Create recovery volumes
  s[<N>,v[-],e] Create solid archive
  s-            Disable solid archiving
  sc<chr>[obj]  Specify the character set
  sfx[name]     Create SFX archive
  si[name]      Read data from standard input (stdin)
  sl<size>      Process files with size less than specified
  sm<size>      Process files with size more than specified
  t             Test files after archiving
  ta[mcao]<d>   Process files modified after <d> YYYYMMDDHHMMSS date
  tb[mcao]<d>   Process files modified before <d> YYYYMMDDHHMMSS date
  tk            Keep original archive time
  tl            Set archive time to latest file
  tn[mcao]<t>   Process files newer than <t> time
  to[mcao]<t>   Process files older than <t> time
  ts[m,c,a,p]   Save or restore time (modification, creation, access, preserve)
  u             Update files
  v<size>[k,b]  Create volumes with size=<size>*1000 [*1024, *1]
  ver[n]        File version control
  vn            Use the old style volume naming scheme
  vp            Pause before each volume
  w<path>       Assign work directory
  x<file>       Exclude specified file
  x@            Read file names to exclude from stdin
  x@<list>      Exclude files listed in specified list file
  y             Assume Yes on all queries
  z[file]       Read archive comment from file

原文

How to transfer WinRAR key to Linux
How to Open, Extract and Create RAR Files in Linux
Switch -V<n>[k|b|f|m|M|g|G] - create volumes
WinRAR storing file paths [closed]

最后更新于 2022-03-29
使用 Hugo 构建
主题 StackJimmy 设计