Introduction
A link creates a reference to a file or folder. Symbolic links are used in Linux for managing and collating files.
In this guide, learn how to use the ln
command to create symbolic links in Linux.
Prerequisites
- A system running Linux
- Access to a terminal window / command line (Activities > Search > type Terminal)
- (optional) A user account with sudo or root privileges (needed to access certain protected files and directories)
Ln Command to Create Symbolic Links
To use the ln
command, open a terminal window and enter the command with the following format:
ln [-sf] [source] [destination]
- By default, the
ln
command creates a hard link. - Use the
-s
option to create a soft (symbolic) link. - The
-f
option will force the command to overwrite a file that already exists. - Source is the file or directory being linked to.
- Destination is the location to save the link – if this is left blank, the symlink is stored in the current working directory.
For example, create a symbolic link with:
ln -s test_file.txt link_file.txt
This creates a symbolic link (link_file.txt) that points to the test_file.txt.
To verify whether the symlink has been created, use the ls command:
ls -l link_file.txt
Create a Symbolic Link to Linux Directory
A symbolic link can refer to a directory. To create a symbolic link to a directory in Linux:
ln -s /mnt/external_drive/stock_photos ~/stock_photos
This example creates a symbolic link named stock_photos in the home (~/) directory. The link refers to the stock_photos directory on an external_drive.
Note: If the system has a connection to another computer, such as a corporate network or a remote server, symlinks can be linked to resources on those remote systems.
Force Overwrite Symbolic Links
You might receive an error message as displayed in the image below:
The error message means that there’s already a file in the destination named link_file.txt. Use the -f
option to force the system to overwrite the destination link:
ln -sf test_file.txt link_file.txt
Note: Using the -f
option will permanently delete the existing file.
Deleting or Removing Links
If the original file is moved, deleted, or becomes unavailable (such as a server going offline), the link will be unusable. To remove a symbolic link, use either the rm
(remove) or unlink
command:
rm link_file.txt
unlink link_file.txt
Soft Links vs Hard Links
The ln
command can be used to create two different kinds of links:
- Soft links
- Hard links
Soft (Symbolic) Links
A soft link, sometimes called a symbolic link or symlink, points to the location or path of the original file. It works like a hyperlink on the internet.
Here are a few important aspects of a soft link:
- If the symbolic link file is deleted, the original data remains.
- If the original file is moved or deleted, the symbolic link won’t work.
- A soft link can refer to a file on a different file system.
- Soft links are often used to quickly access a frequently-used file without typing the whole location.
Hard Links
When a file is stored on a hard drive, several things happen:
- The data is physically written to the disk.
- A reference file, calledinode, is created to point to the location of the data.
- A filename is created to refer to the inode data.
A hard link works by creating another filename that refers to the inode data of the original file. In practice, this is similar to creating a copy of the file.
Here are a few important aspects of hard links:
- If the original file is deleted, the file data can still be accessed through other hard links.
- If the original file is moved, hard links still work.
- A hard link can only refer to a file on the same file system.
- The inode and file data are permanently deleted when the number of hard links is zero.
Conclusion
You should now have a solid understanding of hard and symbolic (soft) links, and how to work with them. Use the ln
command to create links and verify using the ls
command.
原创文章,作者:745907710,如若转载,请注明出处:https://blog.ytso.com/224226.html