Linux/Unix – Creating symlink (hard and soft link)
Links are special files that point to other files; when you act on a file that is a link, you act on the file it points to. There are two kinds of links: hard links and symbolic links.
A hard link is another name for an existing file; there is no difference between the link and the original file. So if you make a hard link from file `foo’ to file `bar’, and then remove file `bar’, file `foo’ is also removed.
Each file has at least one hard link, which is the original file name itself. Directories always have at least two hard links−−the directory name itself (which appears in its parent directory) and the special file `.’ inside the directory.
Likewise, when you make a new subdirectory, the parent directory gains a new hard link for the special file `..’ inside the new subdirectory.
A symbolic link (sometimes called a “symlink” or “soft link”) passes most operations−−such as reading and writing−−to the file it points to, just as a hard link does. However, if you remove a symlink, you remove only the symlink itself, and not the original file.
Use ln (“link”) to make links between files. Give as arguments the name of the source file to link from and the name of the new file to link to. By default, ln makes hard links. To create a hard link from `ultrapark’ to `emerald−city’, type:
$ ln ultrapark emerald−city
This command makes a hard link from an existing file, `ultrapark’, to a new file, `emerald−city’. You can read and edit file `emerald−city’ just as you would `ultrapark’; any changes you make to `emerald−city’ are also written to `ultrapark (and vice versa). If you remove the file `emerald−city’, file `ultrapark’ is also removed. To create a symlink instead of a hard link, use the `−s’ option. To create a symbolic link from `ultrapark’ to `emerald−city’, type:
$ ln −s ultrapark emerald−city
After running this command, you can read and edit `emerald−city’; any changes you make to `emerald−city’ will be written to `ultrapark’ (and vice versa). But if you remove the file `emerald−city’, the file `ultrapark’ will not be removed.