2024-10-29
Before jumping into the ln
command itself, let’s clarify the difference between hard links and symbolic links:
Hard Links: A hard link is essentially another directory entry pointing to the same inode as the original file. Think of it as creating an alias. Deleting one hard link doesn’t affect the others; the file data remains intact as long as at least one hard link exists. Hard links cannot span different filesystems.
Symbolic Links (Soft Links): A symbolic link, also known as a soft link, is a file that contains a path to another file. It’s like a shortcut. Deleting a symbolic link doesn’t affect the original file; however, if the original file is deleted, the symbolic link becomes broken (pointing to a non-existent file). Symbolic links can span different filesystems.
ln
CommandThe basic syntax of the ln
command is:
ln [OPTION]... TARGET LINK_NAME
Where:
TARGET
: The path to the original file (the target of the link).LINK_NAME
: The desired name for the link.To create a hard link, simply use the ln
command without any options:
ln myfile.txt myfile_link.txt
This creates a hard link named myfile_link.txt
pointing to the same inode as myfile.txt
. Both files will now have the same size and modification times. Try running ls -li myfile.txt myfile_link.txt
to verify they share the same inode number (the first number in the output).
You can also create hard links in different directories:
ln myfile.txt /home/user/documents/myfile_link.txt
This creates a hard link in the /home/user/documents
directory. Remember, hard links cannot cross filesystems.
To create a symbolic link, use the -s
option:
ln -s myfile.txt myfile_symlink.txt
This creates a symbolic link myfile_symlink.txt
that points to myfile.txt
. The ls -l
command will show a l
indicating it’s a symbolic link, and the output will include the path to the target file.
You can create symbolic links that point to directories as well:
ln -s /home/user/documents mydocs_link
If you try to create a hard link with a name that already exists, you’ll receive an error. For symbolic links, the existing file will be overwritten if it’s not a directory.
The TARGET
and LINK_NAME
in the ln
command can be either relative or absolute paths. Relative paths are relative to the current working directory.
ln -s data/myfile.txt myfile_link.txt
ln -s /home/user/data/myfile.txt /tmp/myfile_link.txt
If the original file of a symbolic link is deleted, the link becomes broken. You can identify broken symbolic links using ls -l
: they’ll appear with an indication like ->
followed by a path that doesn’t exist. To fix this, either restore the original file or remove the broken symbolic link using rm
.
This guide offers a solid foundation for effectively utilizing the ln
command in your Linux workflow. Further exploration of its capabilities can improve your Linux administration skills.