E325 Error with permission denied while deleting

I have this message on Linux Mint:

325: ATTENTION
Found a swap file by the name "/etc/default/.grub.swp"
          owned by: root   dated: Fri Mar 29 17:37:00 2024
         file name: /etc/default/grub
          modified: YES
         user name: root   host name: ilugu-ThinkPad-X220
        process ID: 22737
While opening file "/etc/default/grub"
             dated: Fri Mar 29 18:02:31 2024
      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 /etc/default/grub"
    to recover the changes (see ":help recovery").
    If you did this already, delete the swap file "/etc/default/.grub.swp"
    to avoid this message.
"/etc/default/grub" 33 lines, 1209 bytes

I tried to remove it,
but it always gives me the output:

rm /etc/default/.grub.swp

rm: remove write-protected regular file '/etc/default/.grub.swp'? y
rm: cannot remove '/etc/default/.grub.swp': Permission denied
Asked By: PaiThon

||
sudo rm /etc/default/.grub.swp
Answered By: GammaZoid

To get help on a particular error code in the Vim editor, use the :help command:

:help 325

This would show you the sections from the Vim manual related to the error that you are seeing (in this case, the section is tagged both ATTENTION and E325).

In that help text, it’s pointed out that if you get this error, and it says modified: YES, then the file on disk is outdated, and a newer snapshot of the file was successfully saved to the .swp file while you were editing it. Since the process ID: line does not end in the text (still running), we can probably assume that the file was being edited when the editor was interrupted somehow, either by crashing or by the system rebooting.

The error message you show then gives you several alternative ways to resolve this, depending on your needs.

Assuming the editor is not currently running elsewhere with the file in question loaded, you can choose to recover the data that was never saved to the file. You do this with the command suggested by the error message:

vim -r /etc/default/grub

This would restore your interrupted edit session, and you could save the file. Since the root user owns the file (and the .swp file), you would likely have to run the above command using sudo.

The error message also says you can discard the non-saved data by deleting the file /etc/default/.grub.swp. This could mean a loss of the information that you thought you had added to the file but never saved. Note that this is a file owned by the root user, so you would likely have to use sudo to remove it.

Answered By: Kusalananda
Categories: Answers Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.