【问题标题】:Create a RAR archive for each subdirectory in a directory - Centos/Ubuntu为目录中的每个子目录创建一个 RAR 存档 - Centos/Ubuntu
【发布时间】:2014-10-07 14:31:26
【问题描述】:

如果在 Ubuntu 或 Centos 上使用 RAR,这是用于压缩单个文件夹(包含子文件夹)的命令,或者是用于压缩单个存档中目录内的所有文件夹的命令:

rar a -m0 -r name.rar

在哪里

-m0 是压缩级别
-r 递归模式

但是如果我想压缩每个文件夹呢?使用哪些命令?

我想在压缩每个文件夹时保留目录树。

我也试过这个命令,

for folder in */; do echo rar a -m0 -r "${folder%/}.rar" "$folder"; done

但它没有给我任何动作,但在终端出现:

user@hppro ~
$ ls -l
total 3
drwxr-xr-x+ 1 user None  0 Aug  2 03:17 abcd_dir
drwxr-xr-x+ 1 user None  0 Aug  2 03:18 defgh_dir
-rwx------  1 user None 86 Dec 24  2020 blahfile

user@hppro ~
$ for folder in */; do echo rar a -m0 -r "${folder%/}.rar" "$folder"; done
rar a -m0 -r abcd_dir.rar abcd_dir/
rar a -m0 -r defgh_dir.rar defgh_dir/

user@hppro ~
$

参数替换${folder%/} 用于去除文件夹名称末尾的“/”。但是这段代码不起作用。

来源:http://www.linuxquestions.org/questions/linux-newbie-8/compress-folder-in-rar-652612/

P.S:我使用的是 Centos 或 Ubuntu,而不是 Windows。

【问题讨论】:

    标签: linux shell ubuntu rar


    【解决方案1】:

    您可能只需要删除echo

    for folder in */; do rar a -m0 -r "${folder%/}.rar" "$folder"; done
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-21
      • 2017-10-15
      • 2020-06-17
      相关资源
      最近更新 更多