Linux/Unix systems allow you to create links or additional names in different directories of a file system. Hard links are the old Unix way of creating links, with symbolic links (or symlinks) being the more modern approach. All files have their own hard link by default, so we are creating additional directory entries for a given file when we create hard links.
Hard links cannot:
- Reference a file outside of its own filesystem or disk partition.
- Reference a directory.
Creating a Hard Link
To create a hard link, we will enter the ln command with the following syntax:
ln [target file] [hard link name]
Let's create a simple text file for our first hard link.
Now we will create our first hard link.
You can see the link we've made when running ls in the directory where it was made.
Time to test our link.
Success!
Removing Hard Links
Hard links look very similar to the files themselves as directories containing hard links don't show any special indication. When deleting hard links remember that you must remove all links to a file or the contents of that file will remain until every link is gone.
To check how many links exist for a file, check the number that is between the file permissions and the user permissions. In this case, we have 2. To remove this file we must remove both of these hard links.
I have deleted the original file name, however, the link we created is still functional because the file still exists until all links have been removed. This can be helpful in case you think you've accidentally deleted the original file.
To remove the link, simply use the rm command followed by the hard link name.
Both links have been deleted and the file has been removed.
Comments
0 comments
Please sign in to leave a comment.