How to Create Symbolic Links in Linux (Soft & Hard Links Explained)

How to Create Symbolic Links in Linux (Soft & Hard Links Explained)

Symbolic links (also called symlinks) are special files in Linux that point to another file or directory. They act like shortcuts, allowing multiple paths to reference the same data without duplication.

This guide explains soft links and hard links, shows when to use which, and walks through common real‑world scenarios with clear, practical examples.


TaskCommand
Create a symbolic (soft) linkln -s target_file link_name
Create a symbolic link to a directoryln -s /path/to/dir link_name
Create a hard linkln target_file link_name
Verify links using lsls -l
Show inode numbers to identify hard linksls -li
Find all hard links to a filefind / -samefile filename
Remove a symbolic linkrm link_name
Remove a hard linkrm link_name
Check link targetreadlink link_name
Display full path of symbolic link targetreadlink -f link_name

This quick reference summarizes the most commonly used soft link and hard link commands in Linux for creating, verifying, and managing file links.


Linux supports two types of links:

  • Soft links (symbolic links)
  • Hard links

Although both create references, they behave very differently.

FeatureSoft LinkHard Link
Inode numberDifferentSame as source
Cross filesystemYesNo
Can link directoriesYesNo
Breaks if source deletedYesNo
Acts like shortcutYesNo

In simple terms, a soft link works like a shortcut or pointer. It remembers the path to a file or directory. If the original file is moved or deleted, the link breaks because the path is no longer valid.

A hard link, on the other hand, is another name for the same file data. Both the original file and the hard link point to the same data on disk. Deleting one name does not remove the data as long as at least one hard link still exists.

Because of this:

  • Soft links are more flexible and portable
  • Hard links are more robust but restricted

There are a few simple ways to identify whether a file is a soft link or a hard link.

Check using ls -l

text
ls -l original.txt soft_link.txt hard_link.txt

Example output:

text
-rw-rw-r-- 2 user user ... original.txt
-rw-rw-r-- 2 user user ... hard_link.txt
lrwxrwxrwx 1 user user ... soft_link.txt -> original.txt
  • Soft links start with l and show -> target_path
  • Hard links look like regular files and share the same link count as the source file

Check inode numbers using stat

If two files have the same inode number, they are hard links to the same data.

text
stat original.txt
stat soft_link.txt
stat hard_link.txt

Key observation from the output:

  • original.txt and hard_link.txt have the same inode number
  • soft_link.txt has a different inode number

Check link count

Soft links do not increase the link count of the source file.

text
ls -l original.txt
  • Link count 1 → single reference
  • Link count greater than 1 → hard link exists

Rule of thumb:
Same inode = hard link
Different inode = soft link

Choosing the correct type of link depends on flexibility, portability, and data safety requirements.

  • Use soft links when you need:

    • Links to directories
    • Cross-filesystem references
    • Flexible shortcuts that can be updated or replaced easily
  • Use hard links when you need:

    • Guaranteed access to file data
    • Disk-efficient duplication
    • Protection against accidental file deletion

The ln command is used to create both soft and hard links.

text
ln [OPTION] TARGET LINK_NAME
  • TARGET → original file or directory
  • LINK_NAME → name of the link to create

Tip: Prefer absolute paths when creating soft links to avoid broken links caused by directory changes.


Soft links (symbolic links) are the most commonly used type of link in Linux. A soft link points to a file path, not the underlying inode, which makes it flexible and suitable for shortcuts, configuration references, and directory aliases.

text
ln -s /tmp/orig_file.txt /root/orig_link

This command creates a symbolic link named orig_link that points to /tmp/orig_file.txt. The link can have a different name from the original file, making it useful for creating readable or standardized paths.

Verify the link:

text
ls -l /root/orig_link

The leading l in the permissions field indicates that the file is a symbolic link, followed by the path it points to.

text
ln -s /tmp/orig_file.txt /root/

When only a directory is provided, the symbolic link is created inside that directory using the same name as the source file. This is useful when you want to mirror files into another location without renaming them.

Relative symbolic links are useful in portable directory structures where absolute paths may change across systems or environments.

Navigate to the destination directory first:

text
cd /tmp/dir1/dir2/dir3/dir4

Create the link using a relative path:

text
ln -s ../../../dir2/src_file src_link

Relative paths avoid hardcoded absolute locations but require careful placement. If directory depth changes, the link may break.

text
ln -s /tmp/dir1/dir2 /root/

Symbolic links can also point to directories. When accessed, the link behaves exactly like the original directory path, making it useful for creating aliases to deeply nested or frequently used locations.


Once symbolic links are created, it is often useful to locate them, verify their targets, or identify broken links in the filesystem.

text
find /tmp -type l

This command searches for all symbolic (soft) links under the /tmp directory and prints their paths.

Hint: Broken symbolic links typically appear in red when using ls --color, indicating that the target file or directory no longer exists.


A hard link is an additional directory entry that points to the same inode as the original file. Both the original file and the hard link are equal references to the same data on disk.

text
ln TARGET LINK_NAME

Important: Hard links can only be created for regular files and must reside on the same filesystem as the source file.

text
ln /tmp/source_file /root/hard_link_file

This creates a second reference to the same file data. Any changes made through either file name are immediately reflected in the other.

text
ls -l /tmp/source_file /root/hard_link_file

Both files display the same inode number and an increased link count, confirming that they reference the same underlying data.


When working on large systems, it can be useful to identify files that share the same inode or have multiple hard links.

Find files sharing the same inode

text
find /root -samefile /tmp/source_file

This lists all files that reference the same inode as /tmp/source_file.

text
find /tmp /root -type f -links +1

This command helps identify files that have more than one hard link, which can be useful during audits, cleanup tasks, or storage analysis.


Symbolic links and hard links can be removed just like regular files. Removing a link only deletes the reference, not the actual data.

text
unlink /path/to/link

The unlink command is designed specifically to remove links and works for both symbolic and hard links.

Remove using rm

text
rm /path/to/link

The rm command can also be used to delete links, as links are treated as regular filesystem entries.

Note: Removing a link does not delete the original file unless it was the last remaining hard link pointing to that data.


Conclusion

Symbolic links are a powerful feature of Linux filesystems. Soft links offer flexibility and portability, while hard links provide robust, inode-level references to shared data.

Understanding when and how to use each type helps you manage files more effectively, build cleaner directory structures, and avoid broken paths in production systems.


Further Reading

Deepak Prasad

Deepak Prasad

R&D Engineer

Founder of GoLinuxCloud with over a decade of expertise in Linux, Python, Go, Laravel, DevOps, Kubernetes, Git, Shell scripting, OpenShift, AWS, Networking, and Security. With extensive experience, he excels across development, DevOps, networking, and security, delivering robust and efficient solutions for diverse projects.