Difference between hard link and copy command

Can anyone let me know the difference between hard link and copy command..

1) from my knowledge hard link wont span across file system and it will link to inode.

2) But in space point of view both hard link and copying a file occupies a same space. Then what is the major difference between both the commands ,.

Any help on this is very thankful.

if you change the content for both the original file or the hardlink, the content is changed in the original file (thus changed in the hardlink). if you change the content in a copied file, the original file doesn't change its content. just the file you decided to edit.

-bash-3.00# echo test >> orig.file
-bash-3.00# cat orig.file

test
-bash-3.00# ln orig.file hardlink.file
-bash-3.00# cat orig.file

test
-bash-3.00# cat hardlink.file

test

both files have same content

-bash-3.00# echo hard test >> hardlink.file
-bash-3.00# cat hardlink.file

test
hard test
-bash-3.00# cat orig.file

test
hard test

edit hardlink.file and all content changes

-bash-3.00# cp orig.file copied.file
-bash-3.00# cat copied.file

test
hard test
-bash-3.00# echo copy test >> copied.file
-bash-3.00# cat copied.file

test
hard test
copy test
-bash-3.00# cat orig.file

test
hard test
-bash-3.00# cat hardlink.file

test
hard test

change to copied file doesn't show same with original file but hardlink and orig file show same content.

hardlink - is a pointer, name, that points to a data; i.e. it`s just an alternative filename; it has same inode number as the file it was created from

copy - obviously, copy of the data; point to a different direction that file it was copyed from; has different inode number

also difference is in system calls, but that`s somewhat deep-diving into issue

Thanks for your nice reply,.

This is incorrect, hard-linking occupies almost no more space (just metadata) while copying (unless you are using ZFS deduplication which is quite unlikely at that time) will double the space usage.

Here is an oversimplified analogy:

Think of a hard link as a pointer in the filesystem. Just like in C, it has to be dereferenced. But instead of what a pointer does: refer to a separate place in memory, with a hardlink we are referred to a place on the physical disk. The link acts as a proxy for the "real" filename.

The C pointer uses very little memory, but can reference a giant object in memory.

A hard link uses very little disks space but can reference a huge file.

How hard links are implemented is not by a universally defined object, but is up to the filesystem and the drivers to resolve. This is like C: a pointer can be a "big-endian" or a "little-endian" object in memory, and the compiler works out how to reference the object the pointer is "aimed" at. The filesystem + driver does the same. We do not worry about how to figure it out at all.

Links have consequences. One of them is that you do not ever want dangling links - links "aimed" at non-existent. It is possible to create a series: link -> link -> physical file. This is where it is easy to get in trouble. You can create circular links in some circumstances.

Copy duplicates a whole file. Link references a whole file.

By design, you cannot have dangling links when dealing with hard-links. They can only exist in the context of symlinks.

jilliagre -

In really old filesystems it was possible. Ever see a hard-linked directory? It is not allowed now. Read up on it if you want. Why would such a thing be mentioned or considered a problem, you ask?

Maybe it happened a long time ago...

As per above thread, hard link appears to be a just pointer. Soft link is pointer too. Than, what is difference between hard and soft link?

Thanks,
Deepak

You are right, dangling hard-links are actually still possible. For example when using ufs with journaling disabled and after a filesystem corruption or simply after running the clri command.

I though you were confusing with symbolic links because your circular link example "link -> link -> physical file" is something quite hard to achieve with hard-links (hardlinks refer to inodes, not files) but quite common with symlinks.

---------- Post updated at 10:14 ---------- Previous update was at 10:10 ----------

They point to different things.
A hard-link point to actual data while a symbolic (or soft) link point to a filename / path that may exist or not.

If you remove a hard-link, there will be no way to refer to the data unless other hard-links still exist to the very same data while if you remove a symlink, you don't loose data, just a way to refer to it.

the file that has hardlinks is not deleted unless all hardlinks are removed.

and if the file that is pointed to by a symlink is deleted, the symlink becomes invalid, ie, pointing to an non existing file.

this is how i understand the differences between the two in simplest form.

and only after all processes having the file open will close it.

Indeed, making it a dangling link.