0

I have one Folder receiving *.DAT file continuously every seconds. I want to split that folder into two folder where the parent(Folder which i want to split into two) folder will move all *.DAT file it receives in those two sub-folder in round robin fashion. Is there any way to do this by bash scripting?

  • A script containing a loop that process new files in the directory? – Tomasz Nov 15 '17 at 9:45
  • continuously every seconds - if content written into a file comes in the continuous manner, you will break a file integrity when starting to move that file in some time point of the writing process. You should set "round-robin" logic at the side/process which is initiating *.DAT files creation, no need to split the final folder – RomanPerekhrest Nov 15 '17 at 10:34
  • If a file is moved on the same filesystem during writing, data integrity is preserved – PiedPiper Nov 15 '17 at 13:12
1

There is your script:

#!/bin/bash
# enter the source dir
cd /tmp/a

# set initial subdir
subdir="b"

# run forever
while true
do
        # get first available *.DAT file
        newfile=`ls -1 *.DAT 2>/dev/null | head -n1`
        if [ "$newfile" != "" ]
        then
                # if the .DAT file exists, move it
                mv ./$newfile /tmp/$subdir/

                # replace subdir for next loop iteration
                if [ "$subdir" == "b" ]
                then
                        subdir="c"
                else
                        subdir="b"
                fi
        else
                # nothing found, wait 1 second
                sleep 1
        fi
done

I've used flat structure for testing

/tmp/a # source dir
/tmp/b # destdir 1
/tmp/c # destdir 2

You'll have to modify it to your situation, but it should work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.