Introduction
A symbolic link, also known as a symbolic link or soft link, is a special type of file that points to another file or directory. Symbolic links are commonly used to create links or aliases for files or directories located in the file system.
In this guide, we'll cover how to use the ln
command to create symbolic links.
Types of connections
There are two types of links in Linux/UNIX systems:
- Physical connections. You can think of a hard link as an additional name for an existing file. Hard links connect two or more filenames with the same inode. You can only create hard links for directories and files located on the same file system or partition. A file can have one or more hard links.
- Soft links. A symbolic link, also known as a soft link, is a type of file that serves as a reference to another file or directory in the file system. It is comparable to a shortcut in the Windows operating system, as it allows you to access a file or directory from a different location without making a copy of it. Symbolic links are useful for organizing files and making it easier to access frequently used files or directories. Unlike a hard link, a symbolic link can point to a file or directory on a different partition or file system.
How to use the ln command
ln
is a command line utility for creating links between files. By default, the ln
command creates hard links. To create a symbolic link, use the -s
( --symbolic
) option.
The syntax of the ln
command to create symbolic links is as follows:
ln -s [OPTIONS] FILE LINK
- If both
FILE
andLINK
are provided, a link will be created from the file specified as the first argument (FILE
) to the file specified as the second argument (LINK
). - If only one file is given as an argument or the second argument is a period (
.
),ln
will create a link to that file in the current working directory. The symbolic link will have the same name as the file it points to.
By default, on success, ln
produces no output and returns zero.
Creating a symbolic link to a file
To create a symbolic link to a particular file, open the terminal and type:
ln -s source_file symbolic_link
Replace source_file
with the name of the existing file you want to create the symbolic link for, and symbolic_link
with the name of the symbolic link.
The symbolic_link
parameter is optional. If you do not specify the symbolic link, the ln
command will create a new link in the current directory:
In the following example, we are creating a symbolic link named my_link.txt
to a file named my_file.txt
:
ln -s my_file.txt my_link.txt
To verify that the symbolic link was created correctly, use the ls
command:
ls -l my_link.txt
The output will look like this:
lrwxrwxrwx 1 noviello users 4 Nov 2 23:03 my_link.txt -> my_file.txt
The l
character is a file flag representing a symbolic link. The ->
symbol shows the file that the symbolic link points to.
Creating symbolic links to a directory
The command for creating a symbolic link to a directory is the same as when creating a symbolic link to a file. Specify the directory name as the first parameter and the symbolic link as the second parameter.
For example, if you want to create a symbolic link from the /mnt/my_drive/movies
directory to the ~/my_movies
directory, you would run:
ln -s /mnt/my_drive/movies ~/my_movies
Override symbolic links
If you try to create a symbolic link that already exists, the ln
command will generate an error message.
ln -s my_file.txt my_link.txt
ln: failed to create symbolic link 'my_link.txt': File exists
To override the target path of the symbolic link, use the -f
( --force
) option.
ln -sf my_file.txt my_link.txt
Removing symbolic links
To delete/remove symbolic links, use the unlink
or rm
command.
The syntax of unlink
is very simple:
unlink symlink_to_remove
Removing a symbolic link using the rm
command is the same as removing a file:
rm symlink_to_remove
No matter which command you use, when removing a symbolic link don't add the trailing /
to the end of its name.
If you delete or move the source file to a different location, the symbolic file will remain dormant (broken) and will need to be removed.
Conclusion
To create a symbolic link in Linux, use the ln
command with the -s
option.
For more information on the ln
command, visit the ln man page or type man ln
in your terminal.
If you have any questions or feedback, feel free to leave a comment.