ln Command in Linux



ln is a Linux command that allows you to create links between files on the system. There are two types of links that can be created with this command, namely hard links and symbolic (soft) links. A hard link acts as another name for the file, while a symbolic link is like a shortcut pointing to the file.

This command is quite flexible and helps you manage your files more efficiently. In case you have deleted the original file in a hard link setup, the link still retains the data. However, if you delete the original file in a symbolic link setup, the link will break since it points to a non-existent file.

Table of Contents

Here is a comprehensive guide to the options available with the ln command −

Syntax of ln Command

The basic syntax for the ln command in Linux is as follows −

ln [OPTION] TARGET LINK_NAME

Where −

  • [OPTION] specifies the options you want to use with the command.
  • TARGET is the path to the existing file you want to create a link to.
  • LINK_NAME is the name of the new link you are creating.

ln Command Options

Here are the various options available for the ln command, which allow you to customize how links are created and managed in Linux.

Options Description
-s Generate a symbolic (soft) link rather than a hard link.
-f Force the creation of the link, even if the target file is already present.
-v Display detailed information about the files being processed.
-i Seek approval before overwriting an existing file.
-n Handle the destination as a regular file even if it's a symbolic link pointing to a directory.
-b Create a copy of each existing destination file before proceeding.
-t Specify the directory in which to create the link.
-L Dereference TARGET if it is a symbolic link.
-P Do not follow symbolic links; always make hard links directly to the target.
-T Treat LINK_NAME as a normal file, even if it is a directory.

Examples of ln Command in Linux

Let's explore some examples of Linux ln command −

  • Create a Hard Link
  • Create a Symbolic Link
  • Force Creating a Link
  • Interactive Mode
  • Backing Up Files
  • Creating Relative Symbolic Links
  • Creating a Link in Specific Directory
  • Verbosely Show Files Being Processed

Create a Hard Link

Hard links create an additional name for an existing file, pointing directly to the file's data on the disk. To create a hard link, use the ln command followed by the source file and the link name. This will create link1.txt as a hard link to file1.txt

ln file1.txt link1.txt
ln Command in Linux1

Creating a Symbolic Link

Symbolic links, or symlinks, act like shortcuts to the target file, storing the path to the target rather than the data itself. You can use the -s option with ln to create a symlink −

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

This command creates symlink.txt, which points to file.txt.

Force Creating a Link

If the destination file already exists, and you need to overwrite it with a new link, you can use the -f option. This forces the creation of the link, replacing the existing file −

ln -f file1.txt link1.txt
ln Command in Linux2

Interactive Mode

For a safer approach, especially when there's a risk of overwriting files, simply use the -i option. This prompts you for confirmation before any overwriting happens −

ln -i file1.txt link1.txt

You'll be asked to confirm before link1.txt is created if it already exists.

ln Command in Linux3

Backing Up Files

In case you want to make a backup of any existing destination files before overwriting them, use the -b option. This ensures that a backup is created before the new link is established −

ln -b file1.txt link1.txt
ln Command in Linux4

Creating Relative Symbolic Links

For relative symbolic links, use the -r option that creates a symlink that maintains relative paths, which can be useful for project structures −

ln -s -r /path/to/original/file.txt symlink.txt

Creating a Link in a Specific Directory

When you need the link in a specific directory, the -t option comes in handy. This command creates a link to file1.txt within the /target/directory −

ln -t /target/directory file1.txt

Verbosely Show Files Being Processed

To see detailed information about what the ln command is doing, use the -v option. This will show you each link creation step-by-step −

ln -v file1.txt link1.txt

These examples demonstrate how the ln command can be used in different scenarios to create and manage links effectively on a Linux system.

Conclusion

The ln is a versatile command used in Linux for creating links between files. With the help of this command, you create both hard and soft links and manage your files efficiently.

In this tutorial, we provided an overview of the syntax, various options, and practical examples of using the ln command in Linux. With these insights, you can confidently use the ln command to streamline your file management tasks.

Advertisements