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.


About msotela

This blog is for anyone who wants to access the power of a Linux system as a systems administrator or user. You may be a Linux enthusiast, a Linux professional, or possibly a computer professional who is increasingly finding the Windows systems in your data center supplanted by Linux boxes.

Posted on March 15, 2009, in Unix/Linux. Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: