TonyStark
TonyStark

Reputation: 379

Move Multiple sequence file using bash

I want to move sequence 20 file in different folder so i use below code to move file but not work...

echo "Moving {$(( $j*20 -19))..$(( $j*20 ))}.png ------- > Folder $i"
mv {$(( $j*20 -19))..$(( $j*20 ))}.png $i;

So i get output in terminal

Moving {1..20}.png ------- > Folder 1
mv: cannot stat ‘{1..20}.png’: No such file or directory

But there is already 1.png to 20.png image file + Folder... So how to move sequence file like

{1..20}.png -> Folder 1
{21..40}.png -> Folder 2

Thank you!!!

Upvotes: 0

Views: 360

Answers (2)

Tom Fenech
Tom Fenech

Reputation: 74695

I don't think that it is possible to combine brace expansion with arithmetic expressions as you are doing. Specifically, ranges like {a..b} must contain literal values, not variables.

I would suggest that instead, you used a for loop:

for ((n=j*20-19;n<=j*20;++n)); do mv "$n.png" "$i"; done

The disadvantage of the above approach is that mv is called many times, rather than once. As suggested in the comments (thanks chepner), you could use an array to reduce the number of calls:

files=()
for ((n=j*20-19;n<=j*20;++n)); do files+=( "$n.png" ); done
mv "${files[@]}" "$i"

"${files[@]}" is the full contents of the array, so all of the files are moved in one call to mv.

Upvotes: 2

Rambo Ramon
Rambo Ramon

Reputation: 1034

You have to evaluate the resulting string again with eval For example

eval "mv {$(( $j*20 -19))..$(( $j*20 ))}.png folder$j"

Upvotes: -1

Related Questions