简体   繁体   中英

Copy directories on schedule linux

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.

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/ 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM