2

I'm trying to learn about inotifywait. I installed inotify-tools:

Start-Date: 2019-08-31  18:11:48
Commandline: apt install inotify-tools
Requested-By: dkb (1000)
Install: inotify-tools:amd64 (3.14-7), libinotifytools0:amd64 (3.14-7, automatic)
End-Date: 2019-08-31  18:11:58

I made this script, jl.sh, to copy any modified file in ~/Desktop/vvtemp over to ~/Public and to timestamp the copied file:

#!/bin/bash
dir_to_watch="/home/dkb/Desktop/vvtemp/"

inotifywait -m -e modify --format '%w%f' "${dir_to_watch}" | while read line
do
s=$line
     newname=/home/dkb/Public/$(date +%Y%m%d%H%M%S)"-${s##*/}"
     cp $line $newname
done

 

  • I created a file called test.txt and started the script in one terminal. There were no errors or warnings.

  • In another terminal, I opened test.txt with nano, edited it, saved it and closed it just the one time.

  • Then I edited the same file using gedit and geany

I looked at the listing of files in the target folder, ~/Public:

dkb@Udin:~/Public$ ll
total 36
drwxr-xr-x  2 dkb dkb 4096 Aug 31 18:27 ./
drwxr-xr-x 17 dkb dkb 4096 Aug 31 18:08 ../
-rw-r--r--  1 dkb dkb 1024 Aug 31 18:24 20190831182416-.test.txt.swp
-rw-r--r--  1 dkb dkb 1024 Aug 31 18:24 20190831182419-.test.txt.swp
-rw-r--r--  1 dkb dkb  159 Aug 31 18:24 20190831182441-test.txt
-rw-r--r--  1 dkb dkb  179 Aug 31 18:26 20190831182619-.goutputstream-QN1A7Z
-rw-r--r--  1 dkb dkb  198 Aug 31 18:26 20190831182638-.goutputstream-UDEB7Z
-rw-r--r--  1 dkb dkb  210 Aug 31 18:27 20190831182722-.goutputstream-0X2K7Z
-rw-r--r--  1 dkb dkb  221 Aug 31 18:27 20190831182733-.goutputstream-01D86Z
dkb@Udin:~/Public$ 
  • The first three files are related to editing and saving test.txt with nano. The first two files have a .swp suffix. The third file is the intended file with the intended timestamped filename.

  • Then I tried editing the same file with gedit or geany. These editors created the backup files, one for each time I modified and saved the file, but the resulting backups, although they had the intended content, had filenames like 20190831182733-.goutputstream-01D86Z. So it seems that the timestamp is honored but the "basename" isn't.

 

My questions are:

  • why are two .swp files created by inotifywait when I use nano?

  • why isn't the intended name generated when I use gedit or geany?

2

Comment "set locking" in /etc/nanorc or ~/.nanorc or $XDG_CONFIG_HOME/nano/nanorc or ~/.config/nano/nanorc

## Enable vim-style lock-files.  This is just to let a vim user know you
## are editing a file [s]he is trying to edit and vice versa.  There are
## no plans to implement vim-style undo state in these files.
#set locking

If you debug inotify you can see that gedit does not modify test:

OPEN test
CREATE .goutputstream-F4776Z
OPEN .goutputstream-F4776Z
ATTRIB .goutputstream-F4776Z
CLOSE_WRITE:CLOSE test
MODIFY .goutputstream-F4776Z
MOVED_FROM .goutputstream-F4776Z
MOVED_TO test
CLOSE_WRITE:CLOSE test
OPEN test
ACCESS test
CLOSE_NOWRITE:CLOSE test

but both uses event "close_write" or you can follow the inotify events and recreate the steps on your backup location.

  • Thanks! I went with close_write and modified /etc/nanorc as you indicated. – Justice for Monica Sep 3 at 13:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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