How to run this bash command, that uses rar/winrar command line, within windows

bashwinrar

I have this bash command that uses rar

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

I understand from https://stackoverflow.com/questions/25299584/create-a-rar-archive-for-each-subdirectory-in-a-directory-centos-ubuntu that it creates a rar archive of each subdirectory of a directory. But that link covers ubuntu. I'm wondering how i'd run that in Windows.

I understand that Gnuwin32 is full of old versions of programs and doesn't even have rar anyway.

Cygwin is newer but doesn't include rar.exe in its list of packages.

If I try even doing $rar in cygwin, then it says

rar: command not found

What can I do?

Best Answer

There's two ways you could do this.. one way is to run rar.exe from cmd.exe without cygwin. But then you'd need to adjust your command to run with batch rather than bash.

The other way is to run rar.exe from cygwin. Then you can use your bash command directly. We'll try that route

You can run external executables from within cygwin.

you can get the rar command for windows quite easily and it ends up in C:\Program Files\WinRAR\Rar.exe

Install winrar from rarlab.com/download.htm which mentions rarlab.com/rar/winrar-x64-602.exe

Then you have to know how to add a directory to a shell's path variable, so you can run rar.exe from any directory (and in your case since using cygwin, that'd be the bash way of adding a directory to the path variable)

And if you look C:\ProgramData\Microsoft\Windows\Start Menu\Programs\WinRAR it'll have a manual for how to use the command line 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 ~
$

So you see now you have rar running.

And now your command will run just as good as it does here

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

And as the user showed, having removed the echo. And set up cygwin bash's path, and installed winrar including the command line rar.exe and then running the command.

enter image description here

Related Question