原文: 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. Hbase源码分析:RPC概况

    RPC是hbase中Master,RegionServer和Client三者之间通信交流的纽带.了解hbase的rpc机制能够为通过源码学习hbase奠定良好的基础.因为了解了hbase的rpc机制能 ...

  2. 前端--3、JavaScript

    引入方式: 直接在HTML中写入(了解) 写到文件中引入 声明变量 变量赋值方式 单个变量赋值 多变量的变量赋值 数据类型 数字and字符串 boolean undefined 数据类型的存储 数组 ...

  3. FCC 基础JavaScript 练习1

    1.JavaScript中的注释方式有以下两种 // This is an in-line comment. /* This is a multi-line comment */ 2.avaScrip ...

  4. UVM基础之------uvm phases机制

    代码的书写顺序会影响代码的实现,在不同的时间做不同的事情,这是UVM phase的设计哲学,UVM phase提供了一个通用的TB phase 解决方案.支持显示的隐式的同步方案,运行时刻的线程控制和 ...

  5. Farseer.net轻量级开源框架 入门篇:修改数据详解

    导航 目   录:Farseer.net轻量级开源框架 目录 上一篇:Farseer.net轻量级开源框架 入门篇: 添加数据详解 下一篇:Farseer.net轻量级开源框架 入门篇: 删除数据详解 ...

  6. Linux的网卡由eth0变成了eth1或eth2,如何修复??

    背景:做linux下分布式测试的时候,重新安装了两个linux虚拟机,结果分布式脚本没有做好,分布式也没有做成. 今天想练练linux命令,打开vmware,启动linux1 虚拟机,使用ifconf ...

  7. Java基础(九)--反射

    什么是反射? 在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法:对于任意一个对象,都能够调用它的任意一个方法和属性 这种动态获取的信息以及动态调用对象的方法的功能称为反射机制. 反射的前 ...

  8. iOS-关于一些手势冲突问题(scrollView 嵌套 tableView)

    简单说下关于开发中容易遇到的父试图添加手势与子试图点击事件冲突,UIScrollView 嵌套 UIScrollView . UIScrollView 嵌套 UITableView的情况手势冲突问题: ...

  9. reversed()函数和sorted()函数

    #reversed()反转排序,可对列表.元组.区间等进行排序 #练习1 a = range(10) a_list = [x for x in reversed(a)] print(a_list) # ...

  10. JAVA基础——链表结构之单链表

    链表:一种数据存储结构.学链表首先要搞懂数组,按朋友的话说,数组和链表的关系就相当于QQ2008和QQ2009. 除非要通过索引频繁访问各个数据,不然大多数情况下都可以用链表代替数组. 链表部分主要要 ...