Search code examples
linuxshellunixfile-copying

Monitor for trigger file and copy all files in that directory


I'm new to Unix Scripting. Sorry if this question sounds stupid.
I have a script where it copies files from Landingzone to Archive directory.

Now, I want to write a script where it checks for test.txt file (which is like a trigger file), only if it is found then copy all files that arrived before test.txt files from Landingzone to Archive. Please let me know how to do this?

I'm mentioning this as script because I've couple more commands apart from copying.


Solution

  • This should work

    ARCHIVE=...             # archive directory
    cd Landingzone
    
    if [ -f test.txt ]; then
        find . -type f -maxdepth 1 ! -name test.txt ! -newer test.txt -exec cp {} $ARCHIVE ';'
    fi
    

    explanation: thanks to if [ -f ... ]; everything is performed only if test.txt exists. Then we call find to

    • search for normal files only ('-f')
    • exclude subdirectories (-maxdepth 1)
    • exclude test.txt itself (! -name test.txt)
    • exclude files that are newer than test.txt (! -newer test.txt)
    • copy all files found to the archive directory (-exec ...)