GeekBits

Python Create Hard Link - os.link()

In this guide, we will walk you through the os.link() method in detail, from its introduction to practical examples.

2 min read
Python Create Hard Link - os.link()

A hard link in Unix-like filesystems is a feature that allows multiple multiple directory entries (filenames) to point to the same underlying data blocks on disk.

Think of it as a way of creating multiple references to the same file. This makes it appear as if there are multiple copies of the file when in reality, they point to one set of data block on the filesystem.

As developers, we are always looking for way to programmatically interact and work with the system. Most programming languages provides ways of interacting and manipulating the filesystem and Python is not left behind.

One of the method that we can use to interact with the filesystem in python os.link(). This method allow us to create a hard link between two files.

The os.link() method is a part of the os module in Python. The role of this method is to create hard links between files.

The following shows the basic syntax for the metod.

import os
os.link(src, dst)

The function accepts two main parameters as shown:

  • src - Sets the path to the source file, which we wish to link.
  • dst - represents the path to the destination file, which will be created as a hard link to the source file.

Before using the os.link() method, it is good to ensure that the target operating system and Python version support it.

This is because not all systems support hard links, and some might require administrative privileges.

You can check whether os.link() is available using the os.link() method or not as shown:

import os
if hasattr(os, 'link'):
    print("os.link() is available.")
else:
    print("os.link() is not available on this platform.")

Let us start with a basic example on how to use the os.link() method to create a hard link on the filesystem.

import os
src = "source.txt"
dest = "destination.txt"

# Create a hard link
os.link(src, dest)

print(f"Hard link created from {src} to {dest}.")

This example, will create a hard link from the source file to the destination file.

We can verify that the function has successfully created a hard link by checking the inode numbers.

Inodes are basically unique identifiers for files on a Unix-like file system. An example is as shown

import os

src = "source.txt"
dest = "destination.txt"

# Create a hard link
os.link(src_file, dst_file)


src_inode = os.stat(src).st_ino
dst_inode = os.stat(dest).st_ino

if src_inode == dst_inode:
    print(f"{dest} is a hard link to {src}.")
else:
    print(f"{dest} is not a hard link to {src}.")

This should check the inode numbers and return the matchig output if the hard link exists or not.

Conclusion

In this post, we explored how to use the os.link() method to create a hard link between files within a given filesystem.

Sign up for our newsletter

Don't miss anything. Get all the latest posts delivered to your inbox. No spam!