Unix shell to simultaneously move files with filenames in fixed increments to directories

I have a large number of directories mostly since the time step increments say 0.00125. I want to copy temp directories in 8 directories sequentially, that is, the algorithm I can think of is

delta_time = 0.00125
    if directory_name = 0.00125 then mv 0.00125/ to main_dir1
    if directory_name = 0.0025  then mv 0.00250/ to main_dir2
    if directory_name = 0.00375 then mv 0.00375/ to main_dir3
    .
    .
    .
    if directory_name = 0.01    then mv 0.01/ to main_dir8
    if directory_name = 0.01125 then mv 0.01125/ to main_dir1 (again)
    if directory_name = 0.0125  then mv 0.0125/  to main_dir2     
    .
    .
    .
    .
    if directory_name = 0.02    then mv 0.02/ to main_dir8
    .
    .
    so on   

      

Temporary directories are large (about 200 directories) so Im thinking to write a bash script. I'm relatively new to bash scripting but still can't think of the logic to do this!

+3


source to share


1 answer


Basically you just want to do modular arithmetic on directory names, except for even multiples of 8, you want to use 8 instead of 0. So:

#!/bin/sh

increment="0.00125"
maxdir=8
for directory_name in 0.*
do
    if [ -d "$directory_name" ]
    then
        TARGET_DIR_NUM=$(echo "($directory_name / $increment) % $maxdir" | bc)
        if [ $TARGET_DIR_NUM -eq 0 ]
        then
            TARGET_DIR_NUM=$maxdir
        fi 
        echo mv $directory_name main_dir${TARGET_DIR_NUM}
    fi 
done 

      



must do the trick.

+1


source







All Articles