Open In App

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.



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.



Symlinks are similar to shortcuts on Windows computers. However, Windows does not automatically open the target file or folder when you access a shortcut. Instead, you need to use a special program (like File Explorer) to open shortcuts.

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.

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

Command :

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

For example, to create a link called “test-link” in your home folder that points to a folder on a CD drive.

Command :

ln -s /media/marko/VBox_GAs_6.1.38/cert ~/test-link

Output :

Creating symlink for directory

The ls command will now show the new link file “test-link” in your home folder. If you type ls -l, you’ll see the letter l at the start, which means it’s a link. The output also shows the path of the target folder that the link points to. When you open the “test-link” folder, you’ll see the same contents as the “cert” folder on the CD drive.

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

Error :

ln: failed to create symbolic link '[filename]': File exists

This error means that a file with the same name already exists in the location where you’re trying to create the link.

To replace the existing file with your new link, you can use the ‘-f’ option like this.

Command :

ln -sf [target file/folder] [name for the new link]

Warning : Using the ‘-f’ option will permanently delete the existing file and replace it with your new link.

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.

To find and locate the link files that are not working anymore, you can use the following Command.

Command :

find [folder path] -type l ! -exec test -e {} \; -print

Replace [folder path] with the location where you want to look for broken links. For example, use the (~) symbol to search in your home folder.

Command :

find ~ -type l ! -exec test -e {} \; -print

Output :

Find Broken Symlink

This command will show you a list of all the link files in your home folder that are broken and not working properly anymore.

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

Command :

unlink [name of the link file]

Replace [name of the link file] with the name of the link file you want to remove.

Alternatively, you can also use the rm command to delete a link file, just like you would delete any other file.

Command :

rm [name of the link file]

Neither of these commands will show any output if the deletion is successful.

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.


Article Tags :