Bash Monitoring directory and moving files out to a new location

159 views Asked by At

I am looking to monitor a directory for any new csv files written in to the directory.
The new csv files on creation will be appended with data. Once the file is written and closed. The file needs to be moved to a new directory.

Monitor dir: /tmp/test/test/  
destination dir for copy : /tmp/test/test1/

I have done up the following script but when running it moves the whole directory instead of the file, please help

#!/bin/bash


inotifywait -m --format '%w%f' /tmp/test/test/ -e close | while read file;  
do  
mv "$file" "/tmp/test/test1/"  
done
1

There are 1 answers

1
Larry Lopez On

ifnotify also notifies on the local directory. When it does that you move the file.

You can modify the code to this to get it to work:

    inotifywait -m --format '%w%f' /tmp/test/test/ -e close | while read file;
    do
        if [ "$file" != "/tmp/test/test" ] then
            mv "$file" "/tmp/test/test1"
        fi
    done