Create hard and soft links

Context: RHCSA certification

Create hard and soft links

To create hard and soft links in Linux, you can use the ln command. The ln command is used to create links between files or directories.

A hard link is a direct link to the inode of a file, which means it points to the same physical location on the disk as the original file. A soft link, also known as a symbolic link or symlink, is a special type of file that points to the filename of another file or directory.

Here's how you can create hard and soft links:

  1. Creating a Hard Link:

    ln <source_file> <hard_link_name>

    Replace <source_file> with the path to the original file, and <hard_link_name> with the desired name for the hard link. The hard link will be created in the same directory as the source file.

    Example:

    ln /path/to/original/file.txt hard_link.txt
  2. Creating a Soft Link:

    ln -s <source_file> <soft_link_name>

    Use the -s option to create a symbolic (soft) link. Replace <source_file> with the path to the original file or directory, and <soft_link_name> with the desired name for the soft link.

    Example:

    ln -s /path/to/original/file.txt soft_link.txt

Note: Soft links can span across different filesystems, while hard links can only be created within the same filesystem.

When you create a hard link, any changes made to the original file will be reflected in all the hard links, as they are essentially the same file. Soft links, on the other hand, are separate files that point to the original file. Deleting the original file will render soft links broken.

Remember to use appropriate file paths and names in the above commands to create the desired links.

You should also read: