如何在 Windows 中執行這個使用 rar/winrar 命令列的 bash 命令?

如何在 Windows 中執行這個使用 rar/winrar 命令列的 bash 命令?

我有一個使用 rar 的 bash 命令

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

我的理解是從https://stackoverflow.com/questions/25299584/create-a-rar-archive-for-each-subdirectory-in-a-directory-centos-ubuntu 它為目錄的每個子目錄建立一個 rar 檔案。但該連結涵蓋了 ubuntu。我想知道如何在 Windows 中運行它。

據我所知,Gnuwin32 充滿了舊版本的程序,而且甚至沒有 rar。

Cygwin 較新,但其軟體包清單中不包含 rar.exe。

如果我嘗試在 cygwin 中執行 $rar ,那麼它會說

rar: command not found

我能做些什麼?

答案1

有兩種方法可以做到這一點。但隨後您需要調整命令以使用批次而不是 bash 運行。

另一種方法是從 cygwin 執行 rar.exe。然後你就可以直接使用你的bash指令了。我們會嘗試這條路線

您可以從 cygwin 內執行外部可執行檔。

你可以很容易地得到 Windows 的 rar 指令,它最終會出現在C:\Program Files\WinRAR\Rar.exe

從 rarlab.com/download.htm 安裝 winrar,其中提到了 rarlab.com/rar/winrar-x64-602.exe

然後你必須知道如何將目錄添加到 shell 的路徑變量,以便你可以從任何目錄運行 rar.exe (在你的情況下,因為使用 cygwin,這就是將目錄添加到路徑變量的 bash 方式)

如果你看的話,C:\ProgramData\Microsoft\Windows\Start Menu\Programs\WinRAR它會有一個如何使用命令列 rar.exe 的手冊

$ /cygdrive/c/Program\ Files/WinRAR/Rar.exe | head -n 7

RAR 6.02 x64   Copyright (c) 1993-2021 Alexander Roshal   11 Jun 2021
Trial version             Type 'rar -?' for help

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



user@hp-probook1 ~
$ PATH=$PATH:/cygdrive/c/Program\ Files/WinRAR

user@hp-probook1 ~
$ /cygdrive/c/Program\ Files/WinRAR/Rar.exe | head -n 7

RAR 6.02 x64   Copyright (c) 1993-2021 Alexander Roshal   11 Jun 2021
Trial version             Type 'rar -?' for help

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


user@hp-probook1 ~
$ rar | head -n 7

RAR 6.02 x64   Copyright (c) 1993-2021 Alexander Roshal   11 Jun 2021
Trial version             Type 'rar -?' for help

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


user@hp-probook1 ~
$

所以你現在看到你已經運行了。

現在您的命令將像在這裡一樣運行

https://stackoverflow.com/questions/25299584/create-a-rar-archive-for-each-subdirectory-in-a-directory-centos-ubuntu

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 ~
$

正如用戶所示,消除了迴聲。並設定cygwin bash的路徑,並安裝winrar包括命令列rar.exe然後執行命令。

在此輸入影像描述

相關內容