I Dabble
I Dabble

Reputation: 331

Make directories incrementally, move file into directory - loop

I'm trying to create a script that will run through a list of files and for each file create an incremental directory and then move that file into the new directory. I'm stuck on getting the file to move.

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

Upvotes: 2

Views: 264

Answers (1)

zerodiff
zerodiff

Reputation: 1700

It looks like you're incrementing $n twice, so when you move the file, the directory doesn't exist. I think you want:

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

Upvotes: 2

Related Questions