Why last modified time of a directory changes when any file inside the directory is viewed (i.e. opened in vi editor but not saved) ?
e.g.
#Created a directory named 'testdir'
$ mkdir testdir
$ ls -lrt | tail -1
drwxr-xr-x 2 root root 4096 Dec 5 14:37 testdir
#Created a file named 'testfile.txt' inside directory 'testdir'
$ cd testdir/
$ touch testfile.txt
$ ls -l
total 0
-rw-r--r-- 1 root root 0 Dec 5 14:38 testfile.txt
#Modification time of the directory 'testdir' is changed to 'Dec 5 14:38' as expected.
$ ls -lrt ../ | tail -1
drwxr-xr-x 2 root root 4096 Dec 5 14:38 testdir
#Opened the file 'testfile.txt' in Vi editor, but I quit using "!q", so nothing saved.
$ vi testfile.txt
#No Change in modification time of file 'testfile.txt'
$ ls -l
total 0
-rw-r--r-- 1 root root 0 Dec 5 14:38 testfile.txt
#But modification time of the directory 'testdir' is changed.
$ ls -lrt ../ | tail -1
drwxr-xr-x 2 root root 4096 Dec 5 14:40 testdir
Its because when we open a file in vi, a hidden swap file (with .filename.swp extension) is created. The swap file is used for recovery of the vi session in case vi crashes (e.g. when system reboots, suppose you accidentally close your vi session with 'Ctrl q' etc). This swap file is deleted when we save and exit the file in vi editor. The creation of the swap file and later deletion of this swap file on normal vi exit is the cause of the change in modification time of a directory.
2 comments:
To avoid this you may:
- put a command resembling the
following ones in your .vimrc:
:set dir=dh2:tmp (for Amiga)
:set dir=~/tmp (for Unix)
:set dir=c:\\tmp (for MS-DOS and Win32)
@kfl62, thanks, its useful.
Post a Comment