How to create links between files in the Linux?

LinuxOpen SourceOperating System

Before creating links, we need to understand what is link and how many types of link in the Linux system.

Link

Links are shortcuts to access a file. It is a connection between file name and actual data in the system. We can say that a link is a pointer to a file.

Types of links

There are two types of links in the Linux system.

  • Hard Links
  • Soft Links or Symbolic links

Hard Links -

The hard link points to the index node of a file, the data of which is stored on the system and the size of link is equal to actual file size.

Soft Links or Symbolic Links -

The soft links are just shortcuts of actual files in the Linux system. It does not contain the contents of the actual file.

The ln command is used to create links between files in the Linux system. By default, the ln command creates a hard link of the file for creating symbolic links, we use -s or --symbolic option with the ln command.

Syntax

The general syntax of the ln command is as follows.

$ ln [OPTION]... [-T] TARGET LINK_NAME
$ ln [OPTION]... TARGET
$ ln [OPTION]... -t TARGET... DIRECTORY
$ ln [OPTION]... -t DIRECTORY TARGET...

Brief description of options available in the ln command.

Sr.No.Option & Description
1--backup[=CONTROL]
Create a backup of existing destination file with argument
2-b
Create a backup of existing destination file without argument
3-f, --force
Forcefully remove existing destination files
4-i, --interactive
Display a message while removing destination
5-P, --physical
Create hard links directly to symbolic links
6-r, --relative
Make symbolic links relative to link location
7-s, --symbolic
Create soft links instead of hard links
8-t, --target-directory=DIRECTORY
Specify the directory in which to create the links
9-T, --no-target-directory
Treat name of link as a normal file
10-v, --verbose
Prompt what is being done
11--help
Displays a help message and then exits.
12--version
It gives info about the version and then exits.

To create a hard link of the file, we use the command specified by file name and link name as shown in below.

$ ln file.txt link.txt

The created link will be a hard link because by default, using the ln command the hard link will be created and the size of the link is equal to the actual size of the file.

To create symbolic link or soft link of a file, we use -s or --symbolic option with the ln command in the Linux system as shown in below.

$ ln --symbolic file.txt link.txt

The color of the symbolic link is different from the actual file or hard link, and also the contents of the file are only in the actual file the symbolic link is just pointing to the file.

raja
Published on 01-Jul-2021 14:14:06

Advertisements