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?
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.
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:
filenameBare linked to the same information. You open Emacs with
filenameAto start editing. After making some changes, you tell Emacs to save the file.
- Emacs renames
filenameBstill points to the same information as
- Emacs saves the edited buffer in a new file
The end result is that
filenameB both point to the original file before editing,
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
.emacsfile: (setq backup-by-copying-when-linked t) For more details on this option, within Emacs type
C-h v, then enter
- Use symbolic links rather than hard links. Symbolic (soft) links
are created by using the
lncommand 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
sourcewith the name of the existing file, and
targetwith the link or new filename connecting to it.
At Indiana University, for personal or departmental Linux or Unix systems support, see At IU, how do I get support for Linux or Unix?
Last modified on May 13, 2009.