原文: http://www.networkworld.com/article/2931534/it-management/what-are-unix-swap-swp-files.html

---------------------------------------------------------------------------------------------------------------

When someone mentions a "swap file", the first thing that comes into your mind might be the kind of file that you would create to increase the swap space on a Unix system. Think swapfile and the swapon command. But there's another kind of swap file that comes into play, generally when you're trying to edit a file that might have encountered some difficulties during a prior edit. That type of swap file is created by your system to help ensure that you don't lose important file content when something goes wrong. And it generally sits in the same directory as the original file and has .swp as its extension.

These swap files store content for the specific file -- for example, while you're editing a file with vim. They are set up when you start an edit session and then automatically removed when you're done unless some problem occurs and your editing session doesn't complete properly. In that case, vim will offer you a chance to recover your work where you left off.

If you were sitting in a directory containing a file that is currently being edited with vim, you might see the type of file that I'm referring to. If the file being edited were called chkAccts.sh, for example, the swap file set up when you begin your edit would be called .chkAccts.sh.swp and you could spot it sitting in the same directory were you to look for it from another window or login session.

These swap files have two purposes: 1) to help keep you from losing content if you run into some kind of editing glitch and 2) to keep you from editing the same file at the same time from each of two sessions. If an editing session crashes or you try to edit the file a second time from a separate window, vim will warn you by issuing a fairly verbose message indicating that the swap file exists and teling you that the reason is one of the two that I just mentioned. It will also supply the command needed to start the editing session using the contents of the swap file.

If you edit the file again, despite the warning, but fail to remove the swap file, vim will continue to warn you. It will show you something like this. In fact, it will show you this every time you edit the file -- as long as the .swp is still sitting there under your fingertips.

E325: ATTENTION
Found a swap file by the name ".chkAccts.sh.swp"
owned by: root dated: Thu May 28 09:33:16 2015
file name: /home/shs/bin/chkAccts.sh.swp
modified: YES
user name: shs host name: boson
process ID: 11354
While opening file "chkAccts.sh.swp"
dated: Thu May 28 10:41:05 2015
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 ShareAccessReview.csv"
to recover the changes (see ":help recovery").
If you did this already, delete the swap file ".ShareAccessReview.csv.swp"
to avoid this message.
"chkAccts.sh.swp" 45L, 2941C Using swap file ".chkAccts.sh.swp"
Original file "/home/oracle/data/chkAccts.sh"
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. Press ENTER or type command to continue

You might recognize some of the original file the contents if you try to look at a .swp file, but it's not going to look like exactly like the original file. It will contain your original text, but also some other content. It is, after all, a dump of some portion of your editing.  They shadow your editing session.

$ cat .isanumber.swp
3210#"! Utp▒N▒D▒ad=usvad▒▒▒▒▒▒▒xutfi echo "$1 is a number"else exit 1 echo "ERROR: not a number!" > /dev/stderrthenif ! [ "$1" -eq "$1" 2> /dev/null ]#!/bin/bash$

If you want to abandon prior editing changes that might be recorded in the swap file, just look for a file with the same name as the one you were editing, but starting with a period and having the extension .swp added to the end and remove it. Problem resolved.

$ rm .chkAccts.sh.swp
rm: remove regular file `.chkAccts.sh.swp'? y

If, for some reason, you want your swap files to be stored somewhere other than your current directory, you can create a .vimrc file and insert a command such as directory=/tmp to use a different directory. You can also disable the behavior (creating swap files) altogether by setting up your .vimrc file like the one shown below. Though I think the protection is generally a good thing, some Unix folks prefer to use other means to ensure their files aren't subject to loss and think that the appearance of .swp files on their systems is more trouble than it's worth. The tradeoff should probably depend on whether you waste a lot of time cleaning up .swp files that you don't need or are grateful every time one of them saves work in progress when you editing session runs into problems.

$ cat .vimrc
set directory=/tmp
set nobackup
set noswapfile

Swap files don't generally cause a lot of trouble, but can be annoying if you keep having to look at vim's warnings every time you go to edit a file. A little cleanup and they'll stop generating warnings.

Join the Network World communities on Facebook and LinkedIn to comment on topics that are top of mind.

What are Unix swap (.swp) files?

最新文章

  1. iPhone添加邮箱
  2. sql group by 理解
  3. 如何保证access_token长期有效
  4. No saved view state could be found for the view identifier
  5. 【html】【21】高级篇--搜索框
  6. SVN服务器的本地搭建和使用
  7. 用Stax方式处理xml
  8. 序列化与反序列化的单例模式实现和readResolve()
  9. RxJava(一) create操作符的用法和源码分析
  10. Python函数化编程整理
  11. 5、Docker容器网络
  12. Linux 文件恢复
  13. jquery.validate动态更改校验规则
  14. C语言中重要的小知识汇总
  15. 如何用移动硬盘安装win7 系统
  16. Centos7远程桌面 vnc/vnc-server的设置
  17. file not found app文件
  18. js 中object对象的操作
  19. docker容器怎么设置开机启动
  20. ResNet网络结构

热门文章

  1. cocos2d-x lua中实现异步加载纹理
  2. C#与正则表达式的例子
  3. jquery滚轮事件
  4. tensorboard在windows系统浏览器显示空白的解决
  5. vue学习图解
  6. x86汇编之十(使用字符串)
  7. oracle文件 结构01
  8. zabbix web监控
  9. 牛客多校Round 3
  10. post请求重定向到get请求问题