The ln
command in Linux is used to create links between files. It can create either hard links or symbolic links. Hard links create a new file that points to the same physical location on the disk as the original file, while symbolic links create a new file that points to the original file using a symbolic path.
Overview
The syntax for the ln
command is as follows:
ln [OPTION]... [-T] TARGET LINK_NAME
ln [OPTION]... TARGET
ln [OPTION]... TARGET... DIRECTORY
Here are some examples of how to use the ln
command:
# Create a hard link to a file
ln file1 file2
# Create a symbolic link to a file
ln -s file1 file2
# Create a hard link to a directory
ln -d dir1 dir2
# Create a symbolic link to a directory
ln -s dir1 dir2
# Create a hard link to a file in a different directory
ln /path/to/file1 /path/to/dir/file2
# Create a symbolic link to a file in a different directory
ln -s /path/to/file1 /path/to/dir/file2
Options
Here are the available options for the ln
command:
Option | Description |
---|---|
-s |
Create a symbolic link |
-f |
Remove existing destination files |
-i |
Prompt before overwriting existing destination files |
-n |
Do not dereference symbolic links |
-v |
Print the name of each link that is created |
-r |
Create links recursively |
-d |
Create a hard link to a directory |
Troubleshooting tips
- Make sure that the source file or directory exists before creating a link to it.
- If you are creating a link to a file or directory in a different directory, make sure that the destination directory exists.
- If you are creating a symbolic link and the source file or directory is moved or deleted, the link will be broken. To avoid this, use a hard link instead.
Notes
- Hard links cannot be created for directories on file systems that do not support them.
- Symbolic links can be created for directories, but they should be used with caution as they can cause issues with some applications.
- When creating a symbolic link, the source path can be either absolute or relative to the destination directory.