How to Create and Remove a Symbolic Link?

Learn via video courses
Topics Covered

Symbolic link Linux, also known as soft links, are a useful feature in Linux systems that allow you to create references to files or directories. To create and remove symbolic links, you can use various commands and methods depending on your operating system.

In this article, we will explore how to create and remove symbolic links in Linux. We will also discuss the difference between soft links and hard links, and how to find and delete broken links.

The main difference between a soft link (symbolic link Linux) and a hard link lies in how they reference files or directories in a file system. Here are the key distinctions:

Soft Link (Symbolic Link)Hard Link
Soft links are simply pointers or references to the target file or directory.Hard links are direct references to the physical location of the target file or directory on the disk.
They act as shortcuts or aliases to the target.They share the same inode and data block with the target.
Soft links have their own inode and data block, independent of the target.Hard links are essentially multiple names for the same file.
They can span across different file systems and even point to non-existent targets.They cannot span across different file systems and can only reference existing files.
If the target is deleted or moved, the soft link becomes broken or dangling.If the original file is deleted, the hard link(s) still point to the data and the file remains accessible.
Soft links can be created for both files and directories.Hard links can only be created for files, not directories.
Soft links are created using the ln command with the -s option in Linux.Hard links are created using the ln command without any additional options in Linux.

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

To create a symbolic link Linux, use the ln command with the -s option. The general syntax is:

Here,

  • <target>: Specifies the file or directory you want to create a link to.
  • <link_name>: Specifies the name and location of the symbolic link you want to create.

By executing this command, you will create a symbolic link named file in the /path/to/link/ directory, which points to the original file located at /path/to/original/file.

Free Courses by top Scaler instructors
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science Course
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science Course

To create a symbolic link (symlink) for a folder in Linux, you can use the ln command with the -s option. Here's an example command:

  • /path/to/original/folder represents the path to the original folder that you want to create a symlink for.
  • /path/to/link/folder represents the path and name of the symlink you want to create.

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more

To remove a symbolic link in Linux, you have two options: using the unlink command or the rm command. Here's how you can remove a symbolic link using either method:

The unlink command is specifically designed to remove links, including symbolic links. To remove a symbolic link, provide the path to the link as an argument to the unlink command. Here's the syntax:

Replace /path/to/link with the actual path to the symbolic link you want to remove. For example, if you have a symbolic link named mylink located in the /path/to/link directory, you would use the following command:

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

The rm command is a general-purpose command used for removing files and directories. It can also be used to remove symbolic links. To remove a symbolic link with rm, simply provide the path to the link as an argument. Here's the syntax:

Replace /path/to/link with the actual path to the symbolic link you want to remove. For example, if you have a symbolic link named mylink located in the /path/to/link directory, you would use the following command:

Note: that when using rm to remove a symbolic link, it will only remove the link itself, not the target file or directory that the link points to.

Sometimes, symbolic links can become broken if the target file or directory is moved or deleted. To find and delete broken links, you can use the find command in combination with the ! -e option, which checks for non-existing files:

Replace /path/to/directory with the directory where you want to search for broken links. This command will find all broken symbolic links within the specified directory.

To delete the broken links found by the find command, you can pipe the output to the xargs command and use rm to remove them:

This command will delete all the broken symbolic links found within the specified directory.

Conclusion

  • Symbolic links Linux (soft links) are powerful tools in Linux that allow you to create references to files and directories.

  • They are created using the ln -s command and can be removed using unlink or rm. Hard links, on the other hand, provide direct references to the physical location of files but have limitations compared to symbolic links.

  • By understanding the concepts of symbolic links and knowing how to create, remove, and manage them, you can enhance your file system organization and flexibility in Linux.