How to create a symbolic link

A symbolic link is a “connection” we create between a location and a physical file or directory. The file or directory exists in a known location A in our system and we wish to create a connection to that location from a different location in our system B. An easy way to think of a symbolic link is like a shortcut from one location to another.

A symbolic link is created with the following command

ln -s TARGET NAME_OF_LINK

where ln is the basic unix command, the parameter -s specifies that we wish to create a symbolic link, TARGET should be replaced with the destination of our link and NAME_OF_LINK should be replaced with what we want our link to be named after.

Test cases:

Ommit the NAME_OF_LINK:

If you ommit the NAME_OF_LINK then the symbolic link will be named after the folder or file you specified in TARGET

/$ cd /tmp
/tmp$ ln -s /tmp/folders/folder1/file1.txt

will create a symbolic link in /tmp named file1.txt that will link to /tmp/folders/folder1/file1.txt

Link to non-existing file:

If you choose to create a link to a non-existenting file like:

/$ cd /tmp
/tmp$ ln -s /tmp/folders/folder1/file2.txt file2.txt

will result to a symbolic link in /tmp named file2.txt that points to a file in /tmp/folders/folder1 named file2.txt that does not exist. Trying to use that link to view the contents of the file will produce an error like

/$ cd /tmp
/tmp$ less file2.txt
/tmp$ file2.txt: No such file or directory

Link to non-existing directory (A):

If you choose to create a link to a non-existing directory like

/$ cd /tmp
/tmp$ ln -s /tmp/folders/folder2 folder2

will result to a symbolic link in /tmp named folder2 that points to something named folder2 in  /tmp/folders that does not exist.  Now if you create a file folder2 in /tmp/folders the above symbolic link that we created will function because it will consider that it is linked to the the file folder2. If instead you created a folder named folder2 in /tmp/folders then the above symbolic link will again function because it will now consider that it is linked to the folder folder2. So in this case the symbolic link does not beforehand know whether it is pointing to a file or folder which may be convenient but is ambiguous.

Link to non-existing directory (B):

If you choose to create a link to a non-existing directory like

/$ cd /tmp
/tmp$ ln -s /tmp/folders/folder2/ folder2

will result to a symbolic link in /tmp named folder2 that points to a folder named folder2 in  /tmp/folders that does not currently exist. The ln command now properly considers to be connected to a folder named folder2, so even if you create a file named folder2 in the folders directory your link will still not work untill you create a folder named folder2 in the same directory

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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