Skip to content

Linux ln command

The Linux ln command is used to create links between files. It can create either hard links, which point directly to the target file’s inode, or symbolic links, which point to the target file’s pathname. Hard links allow multiple names to refer to the same file on the disk, while symbolic links are references to the target file. The ln command requires the path to the target file and the name for the new link as arguments. Various options are available to control the behavior of the link creation, such as -s for symbolic links and -i to prompt before overwriting existing files. Understanding how to use the ln command effectively can help manage file organization and improve system efficiency.

ln Syntax:

Terminal window
ln [option] [source_file] [target_file]

Options:

OptionDescription
-sCreate a symbolic link
-bCreate a backup of existing target file
-fRemove existing target file if it cannot be linked
-iPrompt before overwriting an existing file
-LFollow symbolic links when resolving links
-nDo not dereference symbolic links
-rMake symbolic links relative to link location
-vDisplay information about linking process

Parameters:

ParameterDescription
source_fileThe file to link from
target_fileThe desired name or path for the link

ln Usage:

Terminal window
ln file1 file2

Creates a hard link named “file2” pointing to “file1”.

Terminal window
ln -s source_file link_name

Creates a symbolic link named “link_name” pointing to “source_file”.

Terminal window
ln file1 file2 file3 directory

Creates hard links for “file1,” “file2,” and “file3” in the specified directory.

Terminal window
ln -sf new_target existing_symlink

Forces the update of an existing symbolic link named “existing_symlink” to point to “new_target”.

{Questions}

To create a symbolic link in Linux, use the -s option with the ln command followed by the source file and the destination path:

Terminal window
ln -s /path/to/source/file /path/to/destination/link

To create a hard link in Linux, use the ln command without any options, followed by the source file and the destination path:

Terminal window
ln /path/to/source/file /path/to/destination/link

To force the creation of a link in Linux, use the -f option with the ln command before specifying the source file and destination path:

Terminal window
ln -f /path/to/source/file /path/to/destination/link

To create multiple symbolic links to a single source file in Linux, you can specify multiple destination paths separated by spaces after the source file:

Terminal window
ln -s /path/to/source/file /path/to/destination/link1 /path/to/destination/link2

To check if a symbolic link exists in a directory in Linux, you can use the ls command with the -l option to display detailed information about files:

Terminal window
ls -l /path/to/directory

To update a symbolic link to point to a new destination in Linux, you can simply create a new symbolic link with the -f option, overriding the previous link:

Terminal window
ln -sf /path/to/new/source/file /path/to/destination/link

Applications of the ln command

  • Creating hard links
  • Creating soft links (symbolic links)
  • Renaming files or directories
  • Updating existing links
  • Linking files across different directories