Learn GNU/Linux Commands (6): Link - ln, unlink

There are 2 types of links in GNU/Linux: soft links (symbolic links), and hard links.


Soft Links or Symbolic Links

Soft links, or symbolic links, are similar to shortcuts in Windows. A symbolic link is eventually a file that records the pathname, either relative or absolute, to another file or directory. Symbolic links are more often used by users.


Hard Links

Hard links are low-level links. When a hard link to a file (hard link to a directory is not allowed) is created, the file system records that the file can be found with one more address (which is under the parent directory of the hard link), and the occurrences of the file increase by 1. When the hard link is removed, the file system records that the file won't be found at where the hard link is, and the occurrences of the file decrease by 1. Unlike symbolic links, hard links can not be created across file systems. That is to say, hard links to files on another partition or disk, or even another computer are not allowed.


Make Link(s)

Links can be made with the "ln" command. "ln" makes hard links by default, to make symbolic links, use the option "-s" as "ln -s".

Symbolic links can also be made with "cp -s".

Make a link

ln TARGET LINK_NAME

Make (create) a link to TARGET with the name LINK_NAME. If LINK_NAME is omitted, make a link named TARGET under the current directory.

  • -s, --symbolic: make symbolic links instead of hard links (the default).

Notes

  • "ln -s" makes a symbolic link that stores the pathname of TARGET given exactly in the command. If the pathname is relative, when the link is moved to a new location, TARGET can not be accessed with the link anymore. So it's better to make symbolic links with absolute pathname. So instead of the command below:
    [texpion@com ~]$ ln -s myfile mylink
    This is better:
    [texpion@com myfolder]$ ln -s $PWD/myfile mylink
  • LINK_NAME is the name of a link. Don't use a pathname as LINK_NAME. For example, don't do this:
    [texpion@com ~]$ ln -s myfile /myfolder/mylink
    The right way:
    [texpion@com ~]$ cd /myfolder
    [texpion@com myfolder]$ ln -s $HOME/myfile mylink
  • We introduced $PWD and $HOME in Part 2.

Make multiple links

ln TARGET... DIRECTORY # or
ln -t DIRECTORY TARGET...

Make (create) links to each TARGET in DIRECTORY.


Remove links

Links can be removed with the "rm" commands. The command below can also remove a link.

unlink FILE

This command can remove not only a link but also a file or a directory per call.


Copy Link(s)

By default, "cp" copies the content of the file that a link refers to under the name of the link, for example:

[texpion@com ~]$ ln -s myfile mylink
[texpion@com ~]$ cp mylink ./myfolder

"cp" makes a file instead of a link under "myfolder" called "mylink", whose content is the same as "myfile".

To make a copy of a symbolic link itself, use "cp -d" or "cp -a" instead.



Related

Comments

Popular

Intel 7th and 8th generation processor graphics driver for Windows 7/8/8.1

AV1 vs VP9 vs AVC (h.264) vs HEVC (h.265): Part II - Fidelity

AV1 vs VP9 vs AVC (h.264) vs HEVC (h.265): Part I - Lossless

AV1 vs VP9 vs AVC (h.264) vs HEVC (h.265): Part IV - Decode

OBS Studio settings for best quality (loseless) recording

AV1 vs VP9 vs AVC (h.264) vs HEVC (h.265): Part III - Quality

Install VMAF on Fedora, CentOS/Red Hat (RHEL)