Script to move one folder to multiple folder...

Hi All,
I have to requirement to write a shell script to move file from one folder (A) to another five folder (B,C,D,E,F) and destination folder should be blank. In not blank just skip.
This script will run as a scheduler every 2 minutes. It will check number of files in folder A and move 1 to each destination folder.
For example:
if Folder A has one file, it will move to Folder B and quit.
If folder A has 2 files, move to B and C and quit.
If folder A has 5 files, move to one in each folder. (Each destination folder shoud have unique file)
In case folder A has more than 5 files, it will move 5 and quit, and next time when this script kick-off, it will take...
and so on....
Can anyone help with this please?

School work?

Let me get this straight.

  1. If you don't have any files in directory A, the script should just quit.
  2. If it has 1 file (say file1), move it to directory B and exit.
  3. If it has 2 files (say file1 and file2), move them to directory B and directory, respectively, and so forth, so
  4. if it contains 6 files (say file1, file2, file3, file4, file5 and file6) the script should move them to directory B, directory C, directory D, directory E and directory F, respectively, leaving only file6 in directory A, am I correct?
  5. Then when the script runs the next time, since directory A only contains file6, it should move it to directory B, then exit?

That being said, your forgot to mention what OS you're using and which shell. Also, it would be helpful to see what you've already tried.

Hi gacanepa ,

Thanks for your mail...Your assumption is correct.
Point 4 and 5 are correct ... to make it simple, we may increase the number of destination folder in future in number of files arrives in folder A are more. and script should be smart enough for it, we can paremetrize it...This is for UNIX box and shell script....

What i have been thinking....

Count the number of files in folder A,

run through loop on count...and check if destination folder is blank, if yes, move the file...
and exit whatever come first, loop ends if number of files less than destination folder or if more try loop once again and exit....

i am not written any shell script so will share once i write something...any help will be highly appreciated....

Unix, you mean plain Unix? Good.
And as to the shell, if you're using Unix you should have bash available.
I'll get back to you later with something.

1 Like

Script to do the job, did only basic testing.

#!/bin/bash

root_dir=a
dest_dir=(b c d e f)

test $( ls -lrt $root_dir/ | wc -l ) -le 1 && echo "Nothing to do..." && exit 0

i=0
for dir in ${dest_dir[@]}
do
        if [ $( ls -lrt $dir/ | wc -l) -le 1 ]; then
                good_dir[$i]=$dir
                ((i++))
        fi
done

test $i -le 0 && echo "Destination directories are not empty" && exit 1

i=0
for file in $root_dir/*
do
        test $i -ge ${#good_dir[@]} && break

        printf "Copying $file to ${good_dir[$i]}"
        mv $file ${good_dir[$i]} >/dev/null 2>&1
        test $? -ne 0 && printf " : Error in copying\n" || echo
        ((i++))
done

Scheduling can be done via crontabs.

echo "*/2 * * * * /bin/bash /path/to/your/script >/path/to/your/log 2>&1" | crontab

--ahamed

1 Like

Thanks gacanepa . You got it right, plain unix... Please let me know in case any other information is required...

---------- Post updated at 08:21 PM ---------- Previous update was at 08:20 PM ----------

Thanks ahamad for response..i'll test and let you know....

Try also

S=($(ls a/* 2>/dev/null))                       # assign all source files to an array
T=(b c d e f)                                   # assing all target dirs to an array
for ((i=0; i<${#S[@]};i++))                     # let i run from 0 to # of source files
        do [ -z $(ls ${T[$i]}) ] &&             # test if target dir is empty
              { mv -v ${S[$i]} ${T[$i]} ||      # and move file (verbosely)
                     break                      # if move failed
              }
        done 2>/dev/null                        # redirect all error msgs to null

Hi ahamad.

i am using ksh... i tried to run it and getting error... i guess syntex may differ here....am sorry i said bash earlier....

What is the error you are getting?
Post the output of ksh -x ./your_script

--ahamed