重命名并移动多个图像

时间:2017-06-16 12:18:39

标签: bash shell

我想移动并重命名一些jpg图像。它们位于13个不同的文件夹中,命名方式不同。我试着写一个简短的脚本,为我做的工作。不幸的是,第二个for循环不起作用。行echo $ file回应我" IMAGES"而不是图像名称。

#!/bin/sh

path="/Users/fflach/Desktop/Training/Kindernahrung"
k=0

for i in {1..13} ;
    do
        currentPath="$path $i 1,30 m"
        IMAGES="$currentPath/*.jpg"
        for file in IMAGES ;
            do
                k=k+1
                moveFile="$currentPath/$file"
                echo $file
                destination="/Users/fflach/Desktop/Training/New/$k.jpg"
                mv $moveFile $destination
            done
    done

如何更改第二个循环以使其遍历所有.jpg命名图像?

1 个答案:

答案 0 :(得分:2)

因为$ currentPath和$ destination变量包含空格必须在双引号之间以避免拆分。

增加k:typeset -i kk=k+1或使用算术((k=k+1))

#!/bin/sh

path="/Users/fflach/Desktop/Training/Kindernahrung"
typeset -i k=0

for i in {1..13} ;
do
    currentPath="$path $i 1,30 m"
    for file in "$currentPath"/*.jpg ;
    do
        k=k+1
        moveFile=$file
        echo "$file"
        destination="/Users/fflach/Desktop/Training/New/$k.jpg"
        mv "$moveFile" "$destination"
    done
done
相关问题