[Solved] Found a swap file by the name .XXX.swp


How To

Are you getting "Found a swap file by the name" for some file with .swp extension? Are you trying to open a file using some editor such as vim or vi and getting this message? Do you want to get rid of this message? The solution is quite easy, just delete the reported .swp file but hey It is important that you fix this properly or you may loose unsaved data.

In this tutorial I will share the steps to fix this WARNING message in a proper way, there can be various scenarios under which you get this message so please read the text properly before taking any ACTION

 

What is .swp file? Why do I need this file ?

  • Before we go into the solution part, do you know what is this .swp file and why is it there? and if you really need this file?
  • Please NOTE even though the extension uses .swp, these files are not related SWAP memory so do not get confuse with them
  • A .swp file is created as soon as we open a file using Vim/Vi editor
  • On Unix and Linux, a '.' is prepended to swap file names in the same directory as the edited file.
  • This avoids that the swap file shows up in a directory listing as Unix and Linux will consider it as hidden file.
  • Vim stores the things you changed in a swap file.
  • Using the original file you started from plus the swap file you can mostly recover your work.
  • You can see the name of the current swap file using :sw[apname]
  • The name of the swap file is normally the same as the file you are editing, with the extension ".swp"
  • If this file already exists (e.g., when you are recovering from a crash) a warning is given and another extension is used, ".swo", ".swn", etc.
  • An existing .swp file will never be overwritten.
  • The swap file is deleted as soon as Vim stops editing the file.

You can learn more about this file using

# vim +help\ swap-file

 

Is it safe to delete .swp file

  • The answer is not that straight forward
  • It is possible that the same file is being modified by different users at the same time using any editor.
  • So assuming user root is modifying the content of /tmp/file in terminal 1 and another user logs in as root from different terminal and attempts to access the same file for editing
  • In such case you will get
        process ID: 1453 (still running)
  • So we know that someone is already accessing this file at PID 1453
# ps -p 1453
  PID TTY          TIME CMD
 1453 pts/1    00:00:00 vim
  • So in such case it is recommended to quit the editor without making any changes.
  • As if you choose to Continue with Edit then there will be two different swap file created with different extensions, as you can see here:
# ls -l /tmp/.file.sw*
-rw-r--r-- 1 root root 12288 May 30 11:54 /tmp/.file.swo
-rw-r--r-- 1 root root 12288 May 30 11:54 /tmp/.file.swp
  • This can cause problem because there will be two different version of same file
  • So if you get below message along with "Found a swap file by the name"
process ID: 1453 (still running)
  • Then you should choose to Quit or Abort so that the changes from first user are intact and do not delete the .swp file. Although you can open the file in Read Only mode to just view the changes
Swap file "/tmp/.file.swp" already exists!
[O]pen Read-Only, (E)dit anyway, (R)ecover, (Q)uit, (A)bort:

 

How to fix "Found a swap file by the name"?

The fix is quite simple to overcome this warning i.e. just go ahead and delete the reported .swp file using rm -f /path/.file.swp
But with this you loose any chance of file recovery. So it is important that you first recover the content from .swp file before you delete it.

A Sample snippet of this WARNING

E325: ATTENTION
Found a swap file by the name "/tmp/.file.swp"
          owned by: root   dated: Sat May 30 13:37:50 2020
         file name: /tmp/file
          modified: no
         user name: root   host name: client.example.com
        process ID: 1756
While opening file "/tmp/file"
             dated: Sat May 30 12:52:50 2020

(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 /tmp/file"
    to recover the changes (see ":help recovery").
    If you did this already, delete the swap file "/tmp/.file.swp"
    to avoid this message.

 

Recover from swp file into a different file

  • It is possible that your last Vim/Vi session crashed for some reason while you were editing some file and your latest changes were not saved
  • In my case I have /tmp/file which crashed and I could not save the latest content
  • Now when I try to open /tmp/file using vim editor, I get this warning "Found a swap file by the name "/tmp/.file.swp""
  • Now I have an option to RECOVER the content but I am not sure what contents would be restored?

It is good practice to write the recovered file elsewhere. So I will create a new file file_recover, you can use any name as that doesn't matter

# touch /tmp/file_recover

Next open this file using vi/vim editor

Press Esc and enter :rec <path/of/file> to perform the recovery in this new file. For our example :rec /tmp/file

[Solved] Found a swap file by the name
Initiate File Recovery

This command will recover the content of /tmp/file using /tmp/.file.swp into your current file i.e. /tmp/file_recover. Check the status of the Recovery. As it is also possible that if your .swp file is corrupted then Recovery may Fail

[Solved] Found a swap file by the name
Recovery Completed

Next to compare the difference between the recovered and original content, press Esc followed by :DiffOrig and hit Enter

[Solved] Found a swap file by the name
Initiate DiffOrig for Comparison

This will divide your terminal into two window with the difference of content between original and recovered file. Here you can check and decide if you wish to continue with recovery.

[Solved] Found a swap file by the name
Compare the content after recovery
If you choose to continue with Recovery then you can exit from both the files without saving. Press Esc followed by :q to exit without saving

Now open your original file using vi or vim editor which will again show you the warning, you will also see below message at the bottom of the terminal

[Solved] Found a swap file by the name
Fix Found a swap file by the name
Since we know the Recovery works, press "R" to initiate the recovery

[Solved] Found a swap file by the name
Recovery Completed
So the recovery was successful. Press Enter to continue with editing of the document.

Once you are sure the recovery is ok delete the swap file. Otherwise, you will continue to get warning messages that the ".swp" file already exists

# rm -f /tmp/.file.swp

 

Recover from swp file into same file

If you know the contents which has changed and you don't need to do any type of comparison then you can directly go ahead and recover the content from .swp file using vim -r <file>

For example, here I will directly edit the file using vim with -r to open the file and also recover the content from last saved swp buffer

# vim -r /tmp/file

The snippet from output from this command

[Solved] Found a swap file by the name
Use vim -r to recover
This shows that the Recovery was completed. Hit Enter to continue editing the document.

Once you are done save and exit the file and go ahead and delete the swp file to avoid further warning

# rm -f /tmp/.file.swp

 

Delete swp file without recovery

If you do not wish to recover the contents from your last saved state in the swap file. You can just go ahead and delete the .swp file to avoid any further warning.

IMPORTANT NOTE: Once this .swp file is deleted there is no way to know the changes which will get lost in your original file. So I hope you know what you are doing

# rm -f /tmp/.file.swp

 

How to avoid using swap file?

  • So you don't want to use swap file? This is possible but not recommended
  • You can start the editing of a file using vim -n where -n will make sure there are no swap files created when editing the respective file
  • In such case if vim crashes then there is no possible way to recover your unsaved data from this file

In this example from terminal 1 I will open /tmp/file for editing

# vim -n /tmp/file

From a different file, if I check for .swp files for this file

# ls -l /tmp/.file.sw*
ls: cannot access '/tmp/.file.sw*': No such file or directory

As expected no .swp files were created

 

Conclusion

In this tutorial we learned all about swp files, why it is important to use these swp files and how to fix "Found a swap file by the name". It is very much possible that due to some unexpected event your existing vim/vi session crashed which would normally mean that all your saved changes are lost. But NO.

Vim/Vi editor keeps a copy of your changes in these .swp files. The content in these swp files are updated after typing 200 characters or when you have not typed anything for four seconds.This only happens if the buffer was changed, not when you only moved around. The reason why it is not kept up to date all the time is that this would slow down normal work too much.

It is recommended in most scenarios to first compare the buffer difference between original and recovered file content before you go ahead and delete the .swp file

Lastly I hope the steps from the article to to overcome Found a swap file by the name warning on Linux was helpful. So, let me know your suggestions and feedback using the comment section.

 

References

I have used below external references for this tutorial guide
Vim Swap File and Recovery

Deepak Prasad

Deepak Prasad

He is the founder of GoLinuxCloud and brings over a decade of expertise in Linux, Python, Go, Laravel, DevOps, Kubernetes, Git, Shell scripting, OpenShift, AWS, Networking, and Security. With extensive experience, he excels in various domains, from development to DevOps, Networking, and Security, ensuring robust and efficient solutions for diverse projects. You can connect with him on his LinkedIn profile.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!

Leave a Comment