#!/bin/bash
# 指定需要压缩的文件夹
source_dir="/path/to/source_dir/"
# 指定压缩后的文件夹
target_dir="/path/to/target_dir/"
# 指定压缩后的文件名
output_file="output.rar"
# 切换到源文件夹目录
cd $source_dir
# 遍历源文件夹下的所有文件
for file in $(ls $source_dir)
do
    # 判断是否为文件
    if [ -f $file ]; then
        # 进行RAR压缩
        rar a $target_dir$output_file $file
    fi
done
echo "RAR压缩完成"
将以上代码保存为一个.sh文件,然后给予执行权限:
chmod +x batch_rar.sh
接着执行该脚本文件即可实现批量RAR压缩。注意替换脚本中的source_dir、target_dir和output_file变量为实际路径和文件名。