On Linux, I created a file (file1):

echo Hello file1 > file1

Then, I created a symbolic link (file2), to file1:

ln -s file1 file2

After that, I removed file1:

rm file1

Lastly, I edited file2 to be different:

echo Changed file2 > file2

After all of these steps were executed, I observed that file1 and file2 now contain "Changed file2".

From what I understand, after file1 is removed, the symbolic link (file2) no longer works. However, the interesting part which I hope to get an answer to, is that editing file2 causes file1 to be restored (despite file1 being removed earlier on).

Thank you very much for your time.


The first file, file1, was not actually restored by your process. It was instead created when you ran echo Changed file2 > file2. file2 was a link to a then-non-existent file file1; a symlink does not have to point to an existing file. Because it was a symlink, the system resolved it and redirected the output of the echo command to file1, creating that file, and leaving the file2 symlink in place pointing to file1.

  • Lovely, appreciate the quick response. I should have been more careful with my words about it being created instead of being restored. Thank you! – infinityr00t Apr 14 '19 at 14:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.