Introduction
Before we start to discuss ln command in Linux (Create Symbolic Links), let's first understand-What is Symbolic Link ?
A symbolic link often called a symlink or soft link is a file that points to another file or directory.
The ln
command in Linux is used to create symbolic links, also known as soft links or symlinks. The symbolic link allows you to access the target file or directory from a different location without duplicating the content. The ln
command provides a quick and efficient way to create and manage symbolic links.
In this tutorial, we'll go through how to make symbolic links with the ln
command. We will also address a few FAQs on ln command in Linux.
Link Types
In Linux/UNIX systems, there are two types of links:
- Hard links: Links that are difficult to find. A hard link can be thought of as an additional name for an existing file. Hard links are when two or more file names are linked to the same inode. For a single file, you can create one or more hard links. Hard connections between directories and files on separate file systems or partitions are not possible.
- Soft links: On Windows, a soft link is similar to a shortcut. It's a pointer to a file or location that's not directly accessible. A symbolic link, unlike a hard link, can point to a file or directory on another file system or partition.
How to Use the ln
Command
ln
is a command-line tool for linking files together. The ln
command generates hard links by default. Use the -s
(--symbolic
) option to build a symbolic connection.
The following is the ln
command syntax for generating symbolic links:
ln -s [OPTIONS] FILE LINK
- If both the
FILE
andLINK
arguments are present,ln
will construct a link from the first parameter (FILE
) to the second argument (LINK
). - If the second parameter is a dot (
.
),ln
will build a link to that file in the current working directory. The symlink's name will be the same as the file to which it points.
ln
does not produce any output and returns zero by default when it succeeds.
Creating Symlink to a File
Open your terminal and type the following command to create a symbolic link to a certain file:
ln -s source_file symbolic_link
Replace source_file
with the name of the existing file for which the symbolic link is to be created, and symbolic_link
with the symbolic link's name.
Optionally, the symbolic_link
parameter can be used. The ln
command will create a new symbolic link in your current directory if you don't specify it:
We'll make a symbolic link called my_link.txt
to a file called my_file.txt
in the following example:
ln -s my_file.txt my_link.txt
Use the ls
command to see if the symlink was created successfully:
ls -l my_link.txt
You will get an output like below:
Output
lrwxrwxrwx 1 linuxize users 4 Nov 2 23:03 my_link.txt -> my_file.txt
A symbolic link is represented by the l
character, which is a file-type flag. The ->
symbol denotes the file to which the symlink refers.
Creating Symlinks To a Directory
Making a symbolic link to a directory uses the same command as creating a symbolic link to a file. The first parameter should be the directory name, and the second should be the symlink.
For instance, to build a symbolic link from the /mnt/my drive/movies
directory to the ~/my_movies
directory, type:
ln -s /mnt/my_drive/movies ~/my_movies
Overwriting Symlinks
The ln
command will produce an error message if you try to create a symbolic link that already exists.
ln -s my_file.txt my_link.txt
Output
ln: failed to create symbolic link 'my_link.txt': File exists
Use the -f
(--force
) option to rewrite the symlink's destination path.
ln -sf my_file.txt my_link.txt
Removing Symlinks
Use the unlink
or rm
commands to delete or remove symbolic links.
The unlink has a fairly basic syntax:
unlink symlink_to_remove
Using the rm
command to remove a symbolic link is the same as removing a file:
rm symlink_to_remove
When eliminating a symbolic link, do not append the /
trailing slash to the end of its name, regardless of which command you use.
The symbolic file will be left dangling (broken) if you delete or move the source file to a different location. It should be destroyed.
FAQs for Ln Command in Linux
How do I use the ln
command to create symbolic links?
To create a symbolic link with the ln
command, use the following syntax: ln -s target link_name
. Replace target
with the file or directory you want to link to, and link_name
with the name of the symbolic link you are creating.
What is the difference between a symbolic link and a hard link?
A symbolic link is a reference to another file or directory, while a hard link is an additional reference to the same underlying file. Symbolic links can point to files on different file-systems, while hard links must reside on the same file-system.
What is the advantage of using symbolic links?
Symbolic links provide flexibility in organizing files and directories. They allow you to reference content from different locations without duplicating it, making it easier to manage and access files.
How can I create a symbolic link to a file?
To create a symbolic link to a file, use the ln -s
command followed by the path to the target file and the desired name for the symbolic link.
How can I create a symbolic link to a file?
Yes, you can create a symbolic link to a directory using the same ln -s
command. Specify the target directory and the name for the symbolic link.
What happens if the target file or directory does not exist?
If the target file or directory does not exist, the symbolic link will be created anyway, but it will be a "broken" link. Attempting to access the link will result in an error until the target file or directory is created.
Can I rename a symbolic link?
Yes, you can rename a symbolic link by moving it to a new name/location using the mv
command. Alternatively, you can create a new symbolic link with the desired name.
Conclusion
In Linux, use the ln
command with the -s
option to create a symbolic link.
If you have any queries, please leave a comment below and we’ll be happy to respond to them.