linuxshellubunturar

Create a RAR archive for each subdirectory in a directory - Centos/Ubuntu


If using RAR on Ubuntu or Centos, this is the command for compressing a single folder (with subfolders inside), or is the command for compressing all folders inside a directory in a single archive:

rar a -m0 -r name.rar

where

-m0 is level of compression
-r recursive mode

But if I want to compress each folder? Which commands to use?

I want preserve the directory tree on compressing each folder.

I tried also this command,

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

but it gave me no action, but in terminal appears so:

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

The parameter substitution ${folder%/} serves to strip out the "/" at the end of the folder name. But this code does not work.

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

P.S: I use Centos or Ubuntu not Windows.


Solution

  • You probably just need to remove echo:

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