2

Is there any way to have 2 files of the same name in different folders, and any time a change is made to one of the files, the same change is automatically implemented in the other?

1 Answers1

3

If the two files are located on the same filesystem (i.e., not on two different partitions), then you could create one file as a hard link:

ln /path/to/one_file /path/somewhere/other_file

After having done this, /path/to/one_file and /path/somewhere/other_file are two names for exactly the same file. If you delete one, the contents will still be available through the other name.

This would work for as long as a program does not unlink one of the files and re-creates it.

Likewise, you could create a symbolic link from one name to the other:

ln -s /path/to/one_file /path/somewhere/other_file

In this case, it's /path/to/one_file that contains the actual data, while /path/somewhere/other_file is just a "pointer" (symbolic link) to it.

This does not require that the two paths are on the same filesystem, but if a program unlinks the symbolic link and recreates it as a file, the association is broken, just as for hard links.

Kusalananda
  • 333,661
  • This works. While trying it out, I noticed that even in the case of the hard link, /path/to/one_file has to be the file that contains the actual data.(OS - Debian Stable) – Pratyush Das May 28 '18 at 20:07
  • @PratyushDas Try it a few times and play around with it. When you create a hard link you create a new name for the file contents. The data is then stored under both names, not in one or the other location. – Kusalananda May 28 '18 at 20:09
  • That makes sense. I was a little confused because I deleted /path/to/one_file (only /path/somewhere/other_file exists after this) before reloading my configuration, and got the error that the particular file does not exist. Thanks. – Pratyush Das May 28 '18 at 20:12