碰见.swp文件咋办?

本人花费半年的时间总结的《Java面试指南》已拿腾讯等大厂offer,已开源在github ,欢迎star!

本文GitHub https://github.com/OUYANGSIHAI/JavaInterview 已收录,这是我花了6个月总结的一线大厂Java面试总结,本人已拿大厂offer,欢迎star

原文链接:blog.ouyangsihai.cn >> 碰见.swp文件咋办?

预计阅读时间:6分钟

《》编写过程中,曾碰见一个问题,算是个小知识点,模拟操作如下。

一个session用vi打开文件,但不退出,或者直接关闭session,造成异常退出的场景,


vi test.txt

另一个session打开相同的文件,会有如下提示,直接回车,可以打开,但是是以只读方式打开的,


vi test.txt

E325: ATTENTION
Found a swap file by the name ".test.txt.swp"
         owned by: oracle   dated: Tue Jul  3 14:36:01 2018
        file name: ~oracle/test.txt
         modified: no
        user name: oracle   host name: vm-kvm12907-app
       process ID: 30834 (still running)
While opening file "test.txt"
            dated: Tue Jul  3 14:35:53 2018

(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.

Swap file ".test.txt.swp" already exists!
[O]pen Read-Only, (E)dit anyway, (R)ecover, (Q)uit, (A)bort:

查看当前路径,会有个叫.test.txt.swp的隐藏文件,


ls -al
total 192
-rw-r--r--  1 oracle oinstall      2 Jul  3 14:35 test.txt
-rw-r--r--  1 oracle oinstall  12288 Jul  3 14:36 .test.txt.swp

其实这是因为,使用vi/vim编辑文件实际是先copy一份临时文件并映射到内存给你编辑,编辑的是临时文件,当执行:w后才保存临时文件到原文件,执行:q后才删除临时文件。每次启动编辑时都会检索这个文件是否已经存在临时文件, 有则询问如何处理,即上面截图信息。

解决方法1

删除临时文件,就可以直接用了。


rm *.swp

解决方法2

使用vi -r选项,回车,可以打开和编辑文件,:w保存即可,


vi -r test.txt

Using swap file ".test.txt.swp"
Original file "~/test.txt"
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.

Press ENTER or type command to continue

关于-r选项,可以接文件名,或者不接文件名,


-r  List swap files, with information about using them for recovery.
-r {file} Recovery mode. 
  The swap file is used to recover a crashed editing session.
  The swap file is a file with the same filename as the text file with ".swp" appended.  See ":help recovery".

不接文件,直接用-r选项,能列出所有.swp文件,


vi -r
Swap files found:
   In current directory:
1. .test.txt.swp
      owned by: oracle   dated: Tue Jul  3 16:06:28 2018
      file name: ~oracle/test.txt
      modified: no
      user name: oracle   host name: vm-kvm12907-app
      process ID: 31641 (still running)
   In directory ~/tmp:
      -- none --
   In directory /var/tmp:
      -- none --
   In directory /tmp:
      -- none --

如果您觉得本文有帮助,欢迎关注转发:bisal的个人杂货铺,

碰见.swp文件咋办?
本人花费半年的时间总结的《Java面试指南》已拿腾讯等大厂offer,已开源在github ,欢迎star!

本文GitHub https://github.com/OUYANGSIHAI/JavaInterview 已收录,这是我花了6个月总结的一线大厂Java面试总结,本人已拿大厂offer,欢迎star

原文链接:blog.ouyangsihai.cn >> 碰见.swp文件咋办?


 上一篇
一个shell解析文件的需求 一个shell解析文件的需求
预计阅读时间:7分钟 由于Oracle没有MySQL或EDB慢日志这种机制,因此最近有一个运维的需求,需要解析应用的日志,进行SQL语句执行时间的统计,进而进行监控。 每个应用集群中的节点,会生成自己的处理日志,日志采用循环复写的机制,单个
下一篇 
《Oracle Concept》第一章-1 《Oracle Concept》第一章-1
预计阅读时间:12分钟 背景: 按照《Oracle Conecpt》的结构一起了解Oracle,这是从入门到精通的基础。 前言《Introduction to Oracle Database》的历史文章: 《》 《》 《》 《》 《》 《》