• Home
  • Linux
  • Ubuntu
  • Debian
  • CentOS
  • Linux Commands
  • About Us
  • Donate
TecNStuff
Menu
  • Home
  • Linux
  • Ubuntu
  • Debian
  • CentOS
  • Linux Commands
  • About Us
  • Donate

Ln Command in Linux (Create Symbolic Links)

Written by Admin, Updated On June 4, 2020
ln, terminal
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:

  • Soft links – A soft link is indicating the abstract path of another file or directory. It’s also know as Symbolic Links or
  • Hard links – It is referring a specific location of an existing file. It is possible to create multiple hard links for a single file. It’s not possible to create hard links for files and directories on a different file system or partition.

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,

  • FILE – You have to give source of file for which you want to create link.
  • LINK – It’s a symbolic file name.

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.

If our content helps you, please consider buying us a coffee

Thank you for your support.

Share On
Share on Facebook
Share on Twitter
Share on Reddit
Share on Tumblr
 Previous Article How to Enable SSH on Ubuntu 20.04
Next Article   How to Install Vagrant on Ubuntu 20.04

Related Posts

  • How to Install SSH Keys on Ubuntu 22.04

    How to Set up SSH Keys on Ubuntu 22.04

    January 7, 2023
  • How to Install Fail2ban on Ubuntu 22.04

    How to Install and Configure Fail2ban on Ubuntu 22.04

    December 5, 2022
  • How to Enable SSH on Ubuntu 22.04

    How to Enable SSH on Ubuntu 22.04

    December 1, 2022

Leave a Reply Cancel reply

DigitalOcean Referral Badge

Popular Posts

  • How to Install SSH Keys on Ubuntu 22.04
    How to Set up SSH Keys on Ubuntu 22.04 January 7, 2023
  • How to Install Mongodb on Debian 11
    How to Install MongoDB on Debian 11 Linux January 11, 2023
  • How to Install Puppet Agent on Ubuntu 22.04
    How to Install Puppet Agent on Ubuntu 22.04 January 22, 2023
  • How to Install Python 3.11 on Debian 11
    How to Install Python on Debian 11 January 25, 2023
  • How to Change-Hostname Ubuntu 22.04
    How to Change Hostname on Ubuntu 22.04 January 19, 2023
© 2020 TecNStuff All rights reserved. This website is using and storing cookies on your browser. By using this website you agree our Privacy Policy.  Follow us -  Twitter | Facebook