Search code examples
linuxbashfilemove

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!!!


Solution

  • 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.