Linux で vi のスワップファイルから内容を復元する方法は次の通り。
Linux で vi を強制終了したりすると、
編集中のファイルがスワップファイルとして退避される。
その場合、再度ファイルを vi で編集しようとすると次のような
メッセージが表示される。
例は、/tmp/test.txt がスワップファイルとして退避された場合。
# vi test.txt E325: ATTENTION Found a swap file by the name ".test.txt.swp" owned by: root dated: Tue Jun 3 10:17:36 2008 file name: /tmp/test.txt modified: YES user name: root host name: rhib01 process ID: 14756 While opening file "test.txt" dated: Tue Jun 3 10:19:10 2008 NEWER than swap file! (1) Another program may be editing the same file. If this is the case, be careful not to end up with two different instances of the same file when making changes. Quit, or continue with caution. (2) An edit session for this file crashed. If this is the case, use ":recover" or "vim -r test.txt" to recover the changes (see ":help recovery"). If you did this already, delete the swap file ".test.txt.swp" to avoid this message. "test.txt" 7L, 49C Hit ENTER or type command to continue
この場合も ENTER キーを押せば、通常通り vi で編集が可能。
その後、スワップファイルから復元しないでメッセージも出力したくない場合は、
以下のようなファイルを削除すれば良い。
.test.txt.swo
.test.txt.swp
/var/tmp/test.txt.swp
スワップファイルから復元する場合は次の通り。
例は、.test.txt.swo から復元している。
# vi -r test.txt Swap files found: Using specified name: 1. .test.txt.swo owned by: root dated: Tue Jun 3 10:18:10 2008 file name: /tmp/test.txt modified: YES user name: root host name: rhib01 process ID: 14822 2. .test.txt.swp owned by: root dated: Tue Jun 3 10:17:36 2008 file name: /tmp/test.txt modified: YES user name: root host name: rhib01 process ID: 14756 In directory ~/tmp: -- none -- In directory /var/tmp: 3. test.txt.swp owned by: itou dated: Tue Jun 3 10:15:54 2008 file name: /tmp/test.txt modified: YES user name: itou host name: rhib01 process ID: 14689 In directory /tmp: -- none -- Enter number of swap file to use (0 to quit): 1 Using swap file ".test.txt.swo" Original file "/tmp/test.txt" E308: Warning: Original file may have been changed Recovery completed. You should check if everything is OK. (You might want to write out this file under another name and run diff with the original file to check for changes) Delete the .swp file afterwards. Hit ENTER or type command to continue
このあと ENTER キーを押せば、任意に編集および保存が可能。
復元後も不要になったスワップファイルを削除するまで、
毎回同じメッセージが出力されるので、
不要になったスワップファイルは削除しておく。