原文: 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?

What are Unix swap (.swp) files?的更多相关文章

  1. Supervisor(Linux/Unix进程管理工具)安装与配置

    参考链接:https://blog.csdn.net/xyang81/article/details/51555473 Supervisor(http://supervisord.org/)是用Pyt ...

  2. Git 一些关于 Git、Github 的学习资源

    一些关于 Git.Github 的学习资源 昨天浏览 Github 的是时候发现了 Githug 这个游戏,这个游戏用来帮助菜鸟们学习使用 Git 的. Githug is designed to g ...

  3. 大神的vim配置

    大神的vim配置,O(∩_∩)O spf13-vim : Steve Francia's Vim Distribution __ _ _____ _ ___ _ __ / _/ |___ / __ _ ...

  4. docker Dockerfile 创建镜像

    Docker 组件 1. docker client : docker的客户端 2. docker server : docker daemon的主要组成部分,接受用户通过docker client发 ...

  5. slatstack Master的配置

    Salt系统非常简单并且易于配置,Salt系统的两个组件都有各自的配置文件.如"salt-master"是通过主配置文件来配置的,"salt-minion"是通 ...

  6. Solaris/Linux 命令手册

    无意翻到之前收藏的一个文档,共享一下. Solaris/Linux 命令手册 1. 系统 # passwd:修改口令 # exit:退出系统 2. 文件 # cp:复制文件或目录,参数:-a递归目录, ...

  7. man lsof

    LSOF(8)                                                                LSOF(8) NAME       lsof - lis ...

  8. shell命令lsof

    PREFACE linux一切皆是文件,共有7中文件类型 1.普通文件(regular file) 2.目录文件(directory file) 3.块特殊文件(block special file) ...

  9. 后台进程管理supervisor

    一.安装 该工具是python的一个软件包,需要安装python及其python-setuptools 二.安装完后会在python的bin目录下找到对应的命令文件 三.安装supervisor为服务 ...

随机推荐

  1. SQL 几个查看性能的语句

    1.查找目前SQL Server所执行的SQL语法,并展示资源情况: SELECT s2.dbid , DB_NAME(s2.dbid) AS [数据库名] , --s1.sql_handle , ( ...

  2. 如何利用sql注入进行爆库

    SQL注入能做什么 在<SQL注入基础>一文介绍了SQL注入的基本原理和实验方法,那接下来就要问一下,SQL注入到底能什么? 估计很多朋友会这样认为:利用SQL注入最多只能获取当前表中的所 ...

  3. JMeter怎样测试WebSocket,示例演示(二)

    一.测试案例演示 以  http://www.websocket.org/echo.html 网站为例. 地址为:ws://echo.websocket.org 二.长连接的影响 1.没有勾选stre ...

  4. 服务器主机&软件性能测试自定标准

    PS:最近一直致力于代理ip的服务搭建,其中就要根据客户群体的不同来测试搭建环境和搭建软件的性能,但是不同的客户群体所处的环境和使用的软件是不同的,而业内又没有一套完整的评估方法.在忽略网络本身来讲, ...

  5. 01Struts 2

    Struts 2 Struts2是一个基于MVC设计模式的Web应用框架,它本质上相当于一个servlet,在MVC设计模式中,Struts2作为控制器(Controller)来建立模型与视图的数据交 ...

  6. 03JavaScript运算符与表达式

    JavaScript运算符与表达式 2.5运算符与表达式 2.5.1赋值运算符 运算符 意义 运算符 意义 = x=5 /= x=x/y += x=x+y %= 求余赋值 -= x=x-y *= x= ...

  7. CMU Database Systems - Two-phase Locking

    首先锁是用来做互斥的,解决并发执行时的数据不一致问题 如图会导致,不可重复读 如果这里用lock就可以解决,数据库里面有个LockManager来作为master,负责锁的记录和授权 数据库里面的基本 ...

  8. 网络编程-socketserver

    网络编程使用socketserver,通常包括以下几步:一.定义类,并继承socketserver.BaseRequestHandler 二.重写handle方法 三.实例化TCPServer,并传递 ...

  9. 使用Java中Calendar类测试当前年月日

    import java.util.Calendar; public class time { public static void main(String[] args) { Calendar cal ...

  10. [Luogu] P3258 [JLOI2014]松鼠的新家

    题目描述 松鼠的新家是一棵树,前几天刚刚装修了新家,新家有n个房间,并且有n-1根树枝连接,每个房间都可以相互到达,且俩个房间之间的路线都是唯一的.天哪,他居然真的住在”树“上. 松鼠想邀请小熊维尼前 ...