Question

I am looking for help with a script that will allow me to copy the contents of a directory, or the whole directory to another directory on a schedule or when new files arrive in the source.

For example:

/stuff/folder1/file.txt

Copy to:

/stuff/folder2/file.txt

either when new files arrive or on a recurring schedule.

I do use a Centos machine.

Was it helpful?

Solution

You can use the following program to update folder2 whenever you save new files or update folder1:

while inotifywait -r -e modify -e move -e create -e delete; do
    cp -r /stuff/folder1/. /stuff/folder2/
done

For the schedule thing I would add cp -r /stuff/folder1/. /stuff/folder2/ into a cron job. Instead of cp you can also use rsync. Please also have a look on the manpage of inotifywait.

Note: The above script will start the copy after the first file was altered inside the directory folder1. If you modify many files in folder1 in the same time, you might want to put a sleep command inside the while loop. But in this case it is better to add the copy command at the end of the program which alters the files of folder1.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top