A symbolic link, which is sometimes called a symlink or soft link, is a unique file that directs to another file or directory.
This tutorial will explain how to make symbolic links using the ln command.
Linux/UNIX systems have two distinct types of links:
- Hard links are like extra names given to an existing file in Linux/UNIX systems. They enable two or more filenames to be linked with the same inode. It’s possible to create several hard links for one file. However, hard links cannot be made for files located on a separate partition or filesystem, nor for directories.
- Soft links, also called symbolic links, work similarly to shortcuts in Windows. They are an indirect reference to a file or directory. Unlike hard links, symbolic links can point to a file or directory on a separate filesystem or partition.
Using the ln command
The ln
command is a useful tool for generating links between files in Linux/UNIX systems. By default, the ln command generates hard links. However, if you want to create a symbolic link, you need to use the -s
or --symbolic
option.
To create a symbolic link using ln
, you should follow this syntax:
ln -s [OPTIONS] FILE LINK
- If you provide both the
FILE
andLINK
arguments to theln
command, it will create a link that connects the file specified as the first argument (FILE
) to the file indicated as the second argument (LINK
). - In case you provide only one file argument or a dot (.) as the second argument to the ln command, it will generate a link in the current working directory that points to that file. The name of the symbolic link will be identical to the name of the file it refers to.
By default, when the ln command executes successfully, it doesn’t display any output, and it returns a value of zero.
Creating a symlink to a file
To create a symbolic link to an existing file, you can follow these steps:
- Open your terminal and type the following command:
ln -s source_file symbolic_link
Replace
source_file
with the name of the file you want to create a symbolic link to andsymbolic_link
with the desired name for your symbolic link. If you don’t specify a symbolic link name, the ln command will generate a link in your current directory.
or example, to create a symbolic link namedmy_link.txt
to a file calledmy_file.txt
, you can use the following command:ln -s my_file.txt my_link.txt
- To check if the symbolic link was created successfully, use the
ls
command with the-l
option:ls -l my_link.txt
The output should resemble the following line, indicating that a symbolic link was created:
my_link.txt -> my_file.txt
In the output, the
l
character represents a symbolic link, while the arrow (->
) points to the file the symbolic link references.
Creating Symlinks To a Directory
Creating a symbolic link to a directory follows the same syntax as creating a symbolic link to a file. You need to specify the directory name as the first parameter and the symbolic link name as the second parameter.
For instance, to generate a symbolic link from the /mnt/my_drive/files
directory to the ~/my_files
directory, you can use this command:
ln -s /mnt/my_drive/files ~/my_files
This command creates a symbolic link named my_files
in your home directory that points to the /mnt/my_drive/files
directory.
Overwriting Symlinks
If you attempt to create a symbolic link that already exists, the ln command will display an error message indicating that the operation has failed. For example:
ln -s my_file.txt my_link.txt
ln: failed to create symbolic link 'my_link.txt': File exists
To overwrite the destination path of the symbolic link, you can use the -f or –force option. This will replace the existing link with the new link, without prompting for confirmation. For instance:
ln -sf my_file.txt my_link.txt
This command will create a new symbolic link named my_link.txt
that points to the my_file.txt
file, overwriting any existing my_link.txt
link without asking for confirmation.
Removing Symlinks
You can delete or remove symbolic links using either the unlink or rm command. The syntax for unlink is straightforward:
unlink symlink_to_remove
When removing a symbolic link with the rm command, you can follow the same syntax as when removing a regular file:
rm symlink_to_remove
Note that you should not include a trailing slash /
at the end of the symbolic link name, regardless of the command you use to remove it.
If you move or delete the original source file, the symbolic link will become broken and should be removed.