【20240709】海量圖片匯出需求,shell指令碼

Junshu發表於2024-07-09

[root@localhost images]# cat junshuv3.sh 
#!/bin/bash

# 確保指令碼在 ~/images 目錄下執行
if [ "$(pwd)" != "$HOME/images" ]; then
    cd ~/images
fi

# 建立目標目錄 junshu,如果不存在則建立
mkdir -p junshu

# 獲取CSV檔案中的總行數,用於進度條
total_lines=$(wc -l < AiImgs-5Rope.csv)
current_line=0
bar_length=100

# 逐行讀取 AiImgs-5Rope.csv 檔案
while IFS=',' read -r id image_path
do
    # 去除路徑末尾的回車符
    image_path=$(echo $image_path | tr -d '\r')

    # 確定原圖片路徑
    src_path="$image_path"

    # 複製圖片到目標目錄 junshu,並保留原檔名
    if [ -f "$src_path" ]; then
        cp "$src_path" "junshu/$(basename "$src_path")"
    else
        echo "File $src_path does not exist."
    fi

    # 更新進度
    current_line=$((current_line + 1))
    progress=$((current_line * 100 / total_lines))
    filled_length=$((bar_length * current_line / total_lines))
    bar=$(printf "%-${bar_length}s" "#" | sed "s/ /#/g")
    empty_bar=$(printf "%-${bar_length}s" " ")

    echo -ne "Progress: [${bar:0:filled_length}${empty_bar:filled_length}] $progress% ($current_line/$total_lines)\r"
done < AiImgs-5Rope.csv

# 完成後換行
echo -ne '\n'

# 壓縮目標目錄為 AiImgs-5rope.zip
zip -r AiImgs-5rope.zip junshu

相關文章