Is there a difference between hardlinking with cp -l or ln?

The results of both has to be the same, in that a hard link is created to the original file.

The difference is in the intended usage and therefore the options available to each command. For example, cp can use recursion whereas ln cannot:

cp -lr <src> <target>

will create hard links in <target> to all files in <src>. (it creates new directories; not links) The result will be that the directory tree structure under <target> will look identical to the one under <src>. It will differ from cp -r <src> <target> in that using the latter will copy each file and folder and give each a new inode whereas the former just uses hard links on files and therefore simply increases their Links count.

When used to copy a single file, as in your example, then the results will be the identical.


link uses the least system calls, followed by ln and finally cp:

$ strace link f.txt g.txt | wc --lines
282

$ strace ln --symbolic f.txt g.txt | wc --lines
311

$ strace ln f.txt g.txt | wc --lines
334

$ strace cp --symbolic f.txt g.txt | wc --lines
394

$ strace cp --link f.txt g.txt | wc --lines
410

Tags:

Ln

Cp

Hard Link