Open In App

ln command in Linux with Examples

Last Updated : 29 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

The ‘ln’ command in Linux is a powerful utility that allows you to create links between files. These links can either be hard links or soft (symbolic) links. If you’re unfamiliar with these concepts, check out our detailed guide on Hard and Soft Links in Linux to understand their differences, use cases, and how they affect file management.

Let us get a better understanding of the In command in Linux from this article.

What is the ‘ln’ Command in Linux?

The ln command is primarily used to create links for files in Linux, effectively allowing one file to reference another. Doing so allows you to manage files more efficiently without creating duplicates, making this command crucial for optimizing storage and managing files in Unix-like operating systems.

Syntax:

ln [OPTION]... [-T] TARGET LINK_NAME   (1st form)
ln [OPTION]... TARGET... DIRECTORY     (2nd form)
ln [OPTION]... -t DIRECTORY TARGET...  (3rd form)

Basically, ln command is used to create hard links and soft links for files in Linux.

Three Forms of the ‘ln’ Command

The ln command supports three forms, each with specific use cases:

1. 1st Form: ‘ln [OPTION]… [-T] TARGET LINK_NAME’

This form is used to create a link with a specified name (‘LINK_NAME’) to a target file (‘TARGET’). Essentially, you provide the source file and the desired destination link name.

2. 2nd Form: ‘ln [OPTION]… TARGET… DIRECTORY’

In this form, you provide one or more source files (‘TARGET…’) and a directory (‘DIRECTORY’) where the links will be created. Each source file is linked within the specified directory.

3. 3rd Form: ‘ln [OPTION]… -t DIRECTORY TARGET…’

Similar to the second form, but here, you specify the directory first (‘-t DIRECTORY’), followed by the target files. It’s a more flexible way to handle multiple files.

Most Useful Options for ‘In’ Command

  • ‘–verbose’ or ‘-v’: This option will help you to know which file is linked to which file. Basically, it prints the name of each linked file.
  • ‘–help’ or ‘-h’: It will display help option of ln command, you could even see more detailed information in those help command.
  • ‘–version’ or ‘-v’: This will display the version detail, author detail of the command.

Applications of ‘In’ Command

  • Symbolic links (soft links) help avoid long or complex file paths. Instead of remembering a cumbersome path, you can create a short and meaningful symbolic link pointing to the file or directory.
  • Symbolic links make it easy to make some set of files appear to exist in multiple locations without having to make separate copies.
  • Hard links are most useful for keeping file content in a single location by avoiding duplication of what might be a very large amount of data.

Practical Example of creation and deletion of links in Linux

Let us look at some of the examples of In Command in Linux to better understand the concept.

Example 1: Creating a Symbolic Link.

Creating a Symbolic Link

Example 2: Removing a Symbolic Link.

Removing a Symbolic Link

Example 3: Creating a Hard Link.

Creating a Hard Link


Next Article
Article Tags :

Similar Reads