How to Use ln Command to Create Symbolic Links in Linux
Last Updated :
08 Apr, 2024
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)?
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.
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:
- The -s option tells ln to create a link. Without this option, it creates a different type of link called a "hard link".
- [target file] is the file you want to link to.
- [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.
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.
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.
Overwrite Symbolic Links
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.
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.
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.
Remove Symbolic Links
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.
Similar Reads
How to Create and Use Alias Command in Linux
Imagine you're lost in a maze of complicated Linux commands. You stumble upon a secret doorway marked "Alias," and inside you find shortcuts to all your favorite commands! That's what creating aliases is like. You get to make your own mini-commands for the long ones you use all the time, making thin
6 min read
Remove a Symbolic link to a Directory in Linux
Symbolic links (symlinks) are very important when managing files under Linux since they allow users to make shortcuts of directories and files without copying data. While they help improve the system organization and efficiency, sometimes you might be required to remove a symlink from a directory to
8 min read
How to Create Directory in Linux | mkdir Command
In Linux, the 'mkdir' command is like a magic wand for creating folders super easily. 'mkdir' stands for "make directory," and it helps you organize your computer stuff by creating folders with just one command. Whether you're making one folder or a bunch of them in a row, 'mkdir' is there to help y
7 min read
How to Move File in Linux | mv Command
The `mv` command in Linux is like a superhero tool that can do a bunch of cool stuff with your files and folders. Think of it as a digital moving truck that helps you shift things around in your computer. Whether you want to tidy up your folders, give your files new names, or send them to different
7 min read
How to create a pointer to another pointer in a linked list?
In this article we cover how to create a pointer to another pointer in a linked list, it can be singly, doubly, or circularly. What is Linked List: A linked list is a linear data structure, in which the elements are not stored at contiguous memory locations. The elements in a linked list are linked
7 min read
How to Create a Shell Script in linux
Shell is an interface of the operating system. It accepts commands from users and interprets them to the operating system. If you want to run a bunch of commands together, you can do so by creating a shell script. Shell scripts are very useful if you need to do a task routinely, like taking a backup
7 min read
How to Insert a Directory to PATH in Linux
Adding a directory to the PATH in Linux allows the system to locate and execute scripts or programs stored in that directory without requiring the full path each time. When you type a command in the Linux terminal, the shell searches for the corresponding executable file to run it. These executables
6 min read
How to Rename File in Linux | rename Command
Changing the names of files in Linux is something we often do, and the "rename" command is like a helpful friend for this job. This guide is like a journey to becoming really good at renaming files on Linux, showing you how handy and useful the "rename" command can be. Whether you're just starting o
8 min read
How to Make Script Executable in Linux | chmod Command
In Unix operating systems, the chmod command is used to change the access mode of a file. The name is an abbreviation of change mode. Which states that every file and directory has a set of permissions that control the permissions like who can read, write or execute the file. In this the permissions
7 min read
How to Create a File in the Linux Using the Terminal?
In this article, we will learn to create a file in the Linux/Unix system using the terminal. In the Linux/Unix system, there are the following ways available to creating files. Using the touch commandUsing the cat commandUsing redirection operatorUsing the echo commandUsing the heredocUsing the dd c
4 min read