Editors can follow several strategies to save a file. The two major variants are to overwrite the existing file, or to write to a new file and move it in place. Writing to a new file and moving it in place has the nice property that at any point in time, reading from the file gives you a complete version of the file (one instant the old one, the next instant the new one). If the file is overwritten in place, there's a time during which it is incomplete, which is problematic if some other program accesses it just then or if the system crashes.
Nano apparently overwrites the existing file. Your script detects the point when it's finished writing (the close_write
event) and runs rsync
at that point. Note that it is possible for rsync to grab an incomplete version of the file, if you save twice in quick succession, before rsync has completed its job from the first save.
Vim, on the other hand, uses the write-then-move strategy — something to the effect of
echo 'new content' >somefile.new
mv -f somefile.new somefile
What happens to the old version of the file is that it gets deleted at the point where the new version is moved into place. At this point, the inotifywait
command returns, because the file it's been told to watch no longer exists. (The new somefile
is a different file with the same name.) If Vim had been configured to make a backup file, what would happen is something like
echo 'new content' >somefile.new
ln somefile somefile.old
mv -f somefile.new somefile
and inotifywait
would now be watching the backup.
For more information on file save strategies, see How is it possible to do a live update while a program is running? and File permissions and saving
Vim can be told to use the overwrite strategy: turn off the backupcopy
option (:set nobackupcopy
). This is risky, as indicated above.
To handle both save strategies, watch the directory and filter both close_write
and moved_to
events for somefile
.
inotifywait -m -e close_write,moved_to --format %e/%f . |
while IFS=/ read -r events file; do
if [ "$file" = "somefile" ]; then
…
fi
done
backupcopy
option is off. – Gilles 'SO- stop being evil' Mar 09 '15 at 00:16