Linux ln command

The ln command in Linux is used to create links between files. It can create hard links or symbolic links, which are references to other files or directories.

Here’s an overview of how to use the ln command:

  1. Basic Usage:
    The basic syntax for creating links with ln is:
   ln source_file link_name

This command creates a hard link or symbolic link with the specified link_name that points to the source_file.

  1. Creating Hard Links:
    To create a hard link, use the -f (force) option:
   ln -f source_file link_name

Hard links are direct references to the original file. They share the same inode and occupy the same disk space. Any changes made to the original file will be reflected in all hard links.

  1. Creating Symbolic Links:
    To create a symbolic link, use the -s (symbolic) option:
   ln -s source_file link_name

Symbolic links, also known as soft links, are special files that point to the original file by its path. They are independent files and can reference files across different filesystems. If the original file is moved or deleted, the symbolic link becomes broken.

  1. Examples:
  • Creating a hard link: ln source_file link_name This command creates a hard link named link_name that points to source_file.
  • Creating a symbolic link: ln -s source_file link_name This command creates a symbolic link named link_name that points to source_file.
  • Creating a hard link with a different name: ln source_file /path/to/new_link_name This command creates a hard link named new_link_name in the specified path that points to source_file.
  • Creating a symbolic link with a different name:
    ln -s source_file /path/to/new_link_name
    This command creates a symbolic link named new_link_name in the specified path that points to source_file.

The ln command is useful for creating links to files or directories, allowing you to access them from multiple locations or provide alternative names for files. Note that creating links requires appropriate permissions and can only be done within the same filesystem.

For more information about the ln command and its options, you can refer to the manual page by typing man ln in your terminal.