while true; do
inotifywait -e close_write A/A.txt
cp A/A.txt "B/A_$(( ++i )).txt"
The script terminates when Ctrl+C is pressed.
Note: Since I'm not on Linux, I can't get
inotifywait on my machine to actually catch the
close_write event, but I'm hoping it will do so properly on Linux.
The call to
inotifywait will return as soon as an external process, which has opened the file
A/A.txt for writing, finishes writing and closes the file. This allows the script to carry on with copying the file to the
Doing it this way is more convenient than trying to synchronize the script with a process that writes to the file every once in a while.