Thangam
Thangam

Reputation: 169

unix shell scripting for moving files sequentially with file names as fixed increment into directories

I have large number of directories basically as increments of a time step say 0.00125. I want to copy the time directories into 8 directories sequentially i.e 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   

The time directories are large in number (around 200 directories) so Im thinking of writing a bash script. Im relatively new to bash scripting but still cant think of a logic to do this!

Upvotes: 3

Views: 222

Answers (1)

Moldova
Moldova

Reputation: 1651

You basically just want to do modular arithmetic on the directory names, with the exception that 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 

should do the trick.

Upvotes: 1

Related Questions