Creating and Managing Hard Links
Creating hard links is a straightforward process using the ln
command in the Linux terminal. The basic syntax is:
ln <source_file> <link_name>
Here, <source_file>
is the existing file you want to create a hard link for, and <link_name>
is the name of the new hard link.
For example, to create a hard link named backup.txt
for the file original.txt
, you would run:
ln original.txt backup.txt
Once the hard link is created, you can manage the files just like any other files in the file system. For instance, you can view the file details, modify the contents, or delete the files.
## View file details
ls -l
## Output:
## -rw-rw-r-- 2 user user 0 Apr 12 12:34 original.txt
## -rw-rw-r-- 2 user user 0 Apr 12 12:34 backup.txt
## Modify the file through a hard link
echo "Hello, world!" >> backup.txt
## Delete a hard link
rm backup.txt
## The file is still accessible through the original link
cat original.txt
## Output:
## Hello, world!
In the example above, we first create a hard link backup.txt
for the original.txt
file. We then modify the file through the backup.txt
hard link, and the changes are reflected in the original.txt
file as well. Finally, we delete the backup.txt
hard link, but the file content is still accessible through the original.txt
link.
It's important to note that hard links cannot cross file system boundaries, meaning you cannot create a hard link for a file located on a different file system or partition.