溫馨提示×

Linux批量RAR壓縮的腳本實(shí)現(xiàn)

小樊
83
2024-06-28 12:32:28

#!/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
        # 進(jìn)行RAR壓縮
        rar a $target_dir$output_file $file
    fi
done

echo "RAR壓縮完成"

將以上代碼保存為一個(gè).sh文件,然后給予執(zhí)行權(quán)限:

chmod +x batch_rar.sh

接著執(zhí)行該腳本文件即可實(shí)現(xiàn)批量RAR壓縮。注意替換腳本中的source_dir、target_diroutput_file變量為實(shí)際路徑和文件名。

0