ARCHIVED: With Emacs, when I edit one file that I have used the ln command to link to another file, why are the two files different after I exit Emacs?

This content has been archived, and is no longer maintained by Indiana University. Information here may no longer be accurate, and links may no longer be available or reliable.

With Emacs, when you edit one file that you have used the ln command to link to another file, you may find that the two files are different after you exit Emacs. To understand this problem, you have to understand what Emacs does when it makes a backup and also what happens when you use the ln command to link two filenames to one file.

The problem

Emacs creates a backup of your file as it was originally loaded into the buffer (or saved from the buffer, if it was a new file) the first time you re-save the buffer. Then Emacs renames the original file by adding a ~ (tilde) to the end, and saves the contents of the edited buffer to a new file with the same name as the original file.

By default, the ln command makes a direct (hard) link between the contents of the file you give it as its first argument and the filename you give it as the second argument. The old and new filenames point to the same piece of information on disk.

Here's what happens when you edit a hard-linked file with Emacs:

  1. Assume filenameA and filenameB are linked to the same information. You open Emacs with filenameA to start editing. After making some changes, you tell Emacs to save the file.
  2. Emacs renames filenameA to filenameA~. However, filenameB still points to the same information as filenameA~.
  3. Emacs saves the edited buffer in a new file called filenameA.

The end result is that filenameA~ and filenameB both point to the original file before editing, but filenameA points to a new file with the contents of the Emacs buffer you have edited.


Here are two simple ways to get around this problem:

  • Tell Emacs to make backups by copying the old contents to a new file and saving the new contents to the same file anytime a file has more than one filename. To make Emacs do this by default, add this line to your .emacs file:
      (setq backup-by-copying-when-linked t) 
    For more details on this option, within Emacs type C-h v, then enter make-backup-files.
  • Use symbolic links rather than hard links. Symbolic (soft) links are created by using the ln command with the  -s  option. A symbolic link file points to a file in a more indirect way. You can think of this as if the symbolic link is pointing to the filename dynamically rather than to the actual information the file contains. To make a symbolic link, enter:
      ln -s source target
    Replace source with the name of the existing file, and target with the link or new filename connecting to it.

At Indiana University, for personal or departmental Linux or Unix systems support, see Get help for Linux or Unix at IU.

This is document acxl in the Knowledge Base.
Last modified on 2018-01-18 10:31:43.