Site icon DesignLinux

Ln Command in Linux (Create Symbolic Links)

How to Create Symbolic Links

A symbolic link is used to make links between files using ln command. A symbolic link, also known as a symlink or soft link, is a special type of file that points to another file or directory. This tutorial covers how to use the ln command to create symbolic or soft links.

Links Types

There are following two types of links in Linux/UNIX systems:

How Create Symbolic Links

To create a symbolic link you have to use ln command-line utility. By default, the ln command creates hard links. To create a symbolic link use, the -s (--symbolic) option.

Following is the basic syntax for ln command:

ln -s [OPTIONS] FILE LINK

Here,

If given only one argument and second argument is dot (.) then ln command will create a link to that file in current working directory. The name of the symlink will be the same as the name of the file it points to.

Creating Symlink To a File

In following format you can create symbolic link:

ln -s source_filename symbolic_link

Here, you have to replace source_filename with your existing file name for which you want to create symbolic link and symbolic_link with the name of the symbolic link.

The symbolic_link is an optional argument and if you will not provide, ln command will create a symbolic link in current working directory.

For example, to create a symbolic link for /home/file.php as file_link.php

ln -s /home/file.php file_link.php

You can verify using ls command that symlink is created successfully.

It will show output as following:

lrwxrwxrwx 1 tecnstuff tnsgrp 4 May 2 14:03 /home/file.php -> file_link.php

The-> symbol shows the file the symlink points to.

Creating Symlinks To a Directory

Creating symlink to a directory is the same as creating symlink for a file. Give the source directory as first argument and symlink as second argument.

For instance, to create a symbolic link for /etc/nginx/sites-available/example.com to the /etc/nginx/sites-enabled/ directory you would run:

ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/

Overwriting Symlinks

When a symbolic link is already exists and you try to create, the ln command will throw an error message.

ln -s /home/file.php file_link.php
ln: failed to create symbolic link 'file_link.php': File exists

You can overwrite the symlink using -f (--force) option.

ln -f -s /home/file.php file_link.php

Deleting Symlinks

You can delete or remove symlinks using unlink or rm command.

Below is the syntax for unlink command:

unlink name_of_symlink

Removing a symbolic link using the rm command is the same as when removing a file:

rm symlink_to_remove

Conclusion

This tutorial shown you how to create a symbolic link in Linux using the ln command with the -s option. To know more about the ln command, visit the ln man page.

If you have any questions or feedback, feel free to leave a comment.

Exit mobile version