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:
- Basic Usage:
The basic syntax for creating links withln
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
.
- 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.
- 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.
- Examples:
- Creating a hard link:
ln source_file link_name
This command creates a hard link namedlink_name
that points tosource_file
. - Creating a symbolic link:
ln -s source_file link_name
This command creates a symbolic link namedlink_name
that points tosource_file
. - Creating a hard link with a different name:
ln source_file /path/to/new_link_name
This command creates a hard link namednew_link_name
in the specified path that points tosource_file
. - Creating a symbolic link with a different name:
ln -s source_file /path/to/new_link_name
This command creates a symbolic link namednew_link_name
in the specified path that points tosource_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.