How to Create Symbolic Link in Linux (ln Command)

You can use the ln command to create links (shortcuts) to files and folders in Linux. The sections below explain how to do it with some examples.

Creating a Link for a File

To create a link to a file, open the terminal and type the following command.

Command :

ln -s [target file] [name for the link]

The command has these parts:

  1. The -s option tells ln to create a link. Without this option, it creates a different type of link called a “hard link”.
  2. [target file] is the file you want to link to.
  3. [name for the link] is the name you want to give the link file. If you leave this part out, the link will be created in the current folder.

For example, to create a link called “link-file.txt” that points to the file “target-file.txt” in the “test” folder, type the below command.

Command :

ln -s test/target-file.txt link-file.txt

Output :

Creating a link file

The command won’t show any output. But if you type ls, you’ll see the new link file “link-file.txt”.

To see more details about the link file, you can type the below Command.

Command :

ls -l link-file.txt

Output :

Details about link file

The letter l at the start shows it’s a link file. The output also shows the path to the target file that the link points to.

How to Use ln Command to Create Symbolic Links in Linux

A symbolic link (symlink) is like a shortcut that points to a file or folder on Linux and other similar operating systems. Symlinks can be useful for organizing files and folders, or for making it easier to access files from different locations.

In this guide, you’ll learn how to create symbolic links in Linux using the ‘ln’ command.

Use ln Command to Create Symbolic Links in Linux

  • What Is a Symlink (Symbolic Link)?
  • How to Create Symbolic Link in Linux (ln Command)
  • Create Symlink for Directory
  • Overwrite Symbolic Links
  • Find Broken Symbolic Links
  • Remove Symbolic Links

Similar Reads

What Is a Symlink (Symbolic Link)?

A symlink is a special file that contains the location (path) of another file or folder. This other file or folder is called the “target”. When you try to access the symlink, the operating system automatically looks at the location stored inside the symlink. It then shows you the contents of the target file or folder....

How to Create Symbolic Link in Linux (ln Command)

You can use the ln command to create links (shortcuts) to files and folders in Linux. The sections below explain how to do it with some examples....

Create Symlink for Directory

A link can also point to a folder’s location (path). Use this command to create a link to a folder in Linux....

Overwrite Symbolic Links

Sometimes, when you try to create a new link file, you might see this error message....

Find Broken Symbolic Links

Sometimes, a link file may stop working if the original file or folder it points to gets moved, deleted, or becomes unavailable (like if a server goes offline). However, the system does not automatically remove these broken link files....

Remove Symbolic Links

If a link file is broken or you don’t need it anymore, you can delete it using the unlink command....

Conclusion

Symbolic links, or symlinks, are like shortcuts that point to files or folders on your Linux system. They allow you to access the target file or folder from a different location. To create a symlink, use the ln -s command followed by the path of the target file/folder, and then the name you want to give the symlink. If a symlink is broken (pointing to a non-existent target), you can find such broken links using the find command with specific options. To remove a symlink you no longer need, simply use the unlink or rm command followed by the name of the symlink file. Remember, symlinks are useful for organizing your files and folders, and for making frequently accessed items more easily accessible....