Technology Encyclopedia Home >How to create hard and soft links in Linux?

How to create hard and soft links in Linux?

In Linux, you can create both hard links and symbolic (soft) links using the ln command.

Hard Links

A hard link is a direct reference to the same inode and data block on the disk. Changes to the file via one hard link affect all other hard links to the same file. Hard links cannot span across different filesystems.

Syntax:

ln <source_file> <link_name>

Example:

ln file.txt hardlink.txt

This creates a hard link named hardlink.txt that points to file.txt. Deleting file.txt will not remove the data as long as the hard link exists.

Symbolic (Soft) Links

A symbolic link (symlink) is a special file that contains a reference to another file or directory. It acts like a shortcut. If the original file is deleted, the symlink becomes broken. Symbolic links can cross filesystem boundaries.

Syntax:

ln -s <source_file> <link_name>

Example:

ln -s /path/to/original_file symlink.txt

This creates a symbolic link symlink.txt pointing to /path/to/original_file. If original_file is moved or deleted, symlink.txt will no longer work.

Cloud Storage Considerations

If you're managing files in a cloud environment, such as storing backups or shared resources, consider using Tencent Cloud Object Storage (COS) for scalable and reliable file storage. You can create symbolic links locally to reference files stored in COS, but hard links won't work across cloud storage systems.

For example, you can store large datasets in COS and use symbolic links in your local system to access them conveniently.