Logo
  • Ubuntu
  • CentOS
  • Debian
  • Fedora
  • RedHat

Ln Command in Linux (Create Symbolic Links) - DesignLinux

Jun 04 2020
designlinux 0 Comments
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.

Related

Tags: ln, terminal

Chmod Command in Linux (File Permissions)

Prev Post

How to Enable SSH on Ubuntu 20.04

Next Post
Archives
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • July 2022
  • June 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021
  • May 2021
  • April 2021
  • March 2021
  • February 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
Categories
  • AlmaLinux
  • Android
  • Ansible
  • Apache
  • Arch Linux
  • AWS
  • Backups
  • Bash Shell
  • Bodhi Linux
  • CentOS
  • CentOS Stream
  • Chef
  • Cloud Software
  • CMS
  • Commandline Tools
  • Control Panels
  • CouchDB
  • Data Recovery Tools
  • Databases
  • Debian
  • Deepin Linux
  • Desktops
  • Development Tools
  • Docker
  • Download Managers
  • Drupal
  • Editors
  • Elementary OS
  • Encryption Tools
  • Fedora
  • Firewalls
  • FreeBSD
  • FTP
  • GIMP
  • Git
  • Hadoop
  • HAProxy
  • Java
  • Jenkins
  • Joomla
  • Kali Linux
  • KDE
  • Kubernetes
  • KVM
  • Laravel
  • Let's Encrypt
  • LFCA
  • Linux Certifications
  • Linux Commands
  • Linux Desktop
  • Linux Distros
  • Linux IDE
  • Linux Mint
  • Linux Talks
  • Lubuntu
  • LXC
  • Mail Server
  • Manjaro
  • MariaDB
  • MongoDB
  • Monitoring Tools
  • MySQL
  • Network
  • Networking Commands
  • NFS
  • Nginx
  • Nodejs
  • NTP
  • Open Source
  • OpenSUSE
  • Oracle Linux
  • Package Managers
  • Pentoo
  • PHP
  • Podman
  • Postfix Mail Server
  • PostgreSQL
  • Python
  • Questions
  • RedHat
  • Redis Server
  • Rocky Linux
  • Security
  • Shell Scripting
  • SQLite
  • SSH
  • Storage
  • Suse
  • Terminals
  • Text Editors
  • Top Tools
  • Torrent Clients
  • Tutorial
  • Ubuntu
  • Udemy Courses
  • Uncategorized
  • VirtualBox
  • Virtualization
  • VMware
  • VPN
  • VSCode Editor
  • Web Browsers
  • Web Design
  • Web Hosting
  • Web Servers
  • Webmin
  • Windows
  • Windows Subsystem
  • WordPress
  • Zabbix
  • Zentyal
  • Zorin OS
Visits
  • 1
  • 1,058
  • 610,417

DesignLinux.com © All rights reserved

Go to mobile version