原文: 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. maxItemsInObjectGraph解释

    maxItemsInObjectGraph:一个整数,指定要序列化或反序列化的最大项数,可以限制对象图中要序列化的项数.默认的就是65535,当客户端与WebService之间传递的是对象要序列化的个 ...

  2. [转]Paging, Searching and Sorting in ASP.Net MVC 5

    本文转自:http://www.c-sharpcorner.com/UploadFile/4b0136/perform-paging-searching-sorting-in-Asp-Net-mvc- ...

  3. vue 父子组件双向绑定

    vue组件有2大特性: 1.全局组件和局部组件 2.父子组件的数据传递 接下来直接用demo直接看如何传值(静态传值) father.vue <template> <div> ...

  4. oracle 入门笔记---分区表的分区交换

    本文参考来自作者:蓝紫 详细内容请阅读原文 : http://www.cnblogs.com/lanzi/archive/2013/01/24/2875838.html 在oracle 11.2环境下 ...

  5. LDA主题模型(理解篇)

    何谓“主题”呢?望文生义就知道是什么意思了,就是诸如一篇文章.一段话.一个句子所表达的中心思想.不过从统计模型的角度来说, 我们是用一个特定的词频分布来刻画主题的,并认为一篇文章.一段话.一个句子是从 ...

  6. 机器学习_K近邻Python代码详解

    k近邻优点:精度高.对异常值不敏感.无数据输入假定:k近邻缺点:计算复杂度高.空间复杂度高 import numpy as npimport operatorfrom os import listdi ...

  7. 06Microsoft SQL Server 完整性约束

    Microsoft SQL Server 完整性约束 标识 IDENTITY自动编号 CREATE TABLE table_name( id ,), NAME ) not null, sex ) de ...

  8. 用Docker构建Nginx镜像

    1构建Nginx镜像 1建立工作目录 [root@localhost ]# mkdir 1nginx [root@localhost 1nginx]# cd 1nginx/ [root@localho ...

  9. java.net.MalformedURLException: no protocol: www.baidu.com

    URL url = new URL("www.baidu.com");改为 URL url = new URL("http://www.baidu.com");

  10. [Luogu] P3225 [HNOI2012]矿场搭建

    题目描述 煤矿工地可以看成是由隧道连接挖煤点组成的无向图.为安全起见,希望在工地发生事故时所有挖煤点的工人都能有一条出路逃到救援出口处.于是矿主决定在某些挖煤点设立救援出口,使得无论哪一个挖煤点坍塌之 ...