0

I was just playing with some basic Unix commands with following operations

  1. create a file 'one'
  2. create a link 'two' to 'one' (ln one two)
  3. Edit the file 'one' and put words - one, two, three, four on separate lines.
  4. Checked contents of 'two' - it has the same contents, so far so good.
  5. Create a soft link 'three' to one. three also has same contents
  6. Verified the number of links using ls -l.
  7. Edited file 'one' and added word 'five' on a separate line.
  8. Checked that files 'two' and 'three' have the same contents - so far so good
  9. Edited soft link three (vim three) and added the word 'six' at the end.
  10. Checked all the three files now have one to six in words.

Question - I understand if file 'one' gets contents of file 'three'. But why does file 'two' also get them?

If I do ls -l, I see that files one and two have 28 bytes, whereas file three has only 3 bytes (maybe for six). What is the reason for this?

Now if I remove file 'one', I see that three is still shown to be linked to one, but I cannot cat three and get error that file does not exist. But then why it is shown in the ls command?

  • Read up on symbolic links, which are just that - links. Hard links would be different. This is not about programming, so not really suitable for SO. – Sami Kuhmonen Dec 16 '15 at 4:47
3

why does file 'two' also get them?

cause ln(1) make hard links by default, and 'two' is a hard link of 'one', according to the man page:

A hard link to a file is indistinguishable from the original directory entry; any changes to a file are effectively independent of the name used to reference the file.


If I do ls -l, I see that files one and two have 28 bytes, whereas file three has only 3 bytes (maybe for six). What is the reason for this?

cause the file content has 28 bytes, like this:

$ wc -c <<<'one two three four five six'
      28

except new line char replaced by space.

for the file 'three', it's a symbol link. a symbol link contains the name of the file to which it is linked. so 'three' would have size of the name of file 'one', and it's 3 bytes.


Now if I remove file 'one', I see that three is still shown to be linked to one, but I cannot cat three and get error that file does not exist. But then why it is shown in the ls command?

If you remove file 'one', 'three' becomes a broken symbol link. Symbol links are a specific file type, unless you remove it explicitly, it would not disappear when the file it linked to is removed.

  • You may then ask, "why is there both 'hard-links' and 'symbolic-links'?" - One difference I think is that hard-links, because they refer to the same physical location (i-node) have to be on the same physical file-system(partition) however symbol-links being just file-names (either relative OR absolute) can point to a file on a different file-system. Of course such "cross-dev" links can make the work of utilities such as 'find' harder (symbolic-links can be circular e.g. if "fileA" is a symlink to "fileB" and "fileB" is one to "fileA") - which is avoided by certain option arguments... – SlySven Dec 21 '15 at 18:09

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.