逐步创建目录,将文件移动到目录 - 循环

时间:2014-12-05 17:01:27

标签: bash

我尝试创建一个将运行文件列表的脚本,并为每个文件创建一个增量目录,然后将该文件移动到新目录中。我一直坚持让文件移动。

n=1

for file in /Users/scrawfo/Desktop/untitled\ folder/*.zip; do   
    mkdir /Users/scrawfo/Desktop/untitled\ folder/test/$((n++))
    sleep 5s
    mv $file /Users/scrawfo/Desktop/untitled\ folder/test/"$((n++))" 
done

1 个答案:

答案 0 :(得分:2)

看起来您需要两次递增$n,因此当您移动文件时,该目录不存在。我想你想要:

n=1

for file in /Users/scrawfo/Desktop/untitled\ folder/*.zip; do
    mkdir /Users/scrawfo/Desktop/untitled\ folder/test/$n
    sleep 5s
    mv $file /Users/scrawfo/Desktop/untitled\ folder/test/$n
    # Edited per Etan's comment, much less confusing this way
    $((n++))
done