-1

Consider two directories A and B.

A contains a text file A.txt which changes its contents every 3 seconds.

The shell script is to move A.txt to directory B and rename it to A_1.txt.

After 3 seconds the script should copy A.txt as A_2.txt in directory B.

The script should not die until a delimiter is pressed.

closed as unclear what you're asking by roaima, DarkHeart, Archemar, Eric Renouf, Jeff Schaller May 7 '17 at 13:26

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 3
    What's the question? – roaima May 7 '17 at 7:28
2

Using bash or ksh93 and inotifywait:

i=0
while true; do
    inotifywait -e close_write A/A.txt
    cp A/A.txt "B/A_$(( ++i )).txt"
done

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 B directory.

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.

  • 1
    works fine for me, though inotifywait is rather verbose, so maybe use inotifywait -qq – ilkkachu May 7 '17 at 9:09

Not the answer you're looking for? Browse other questions tagged or ask your own question.