ln

Used to create links to files

Supplementary instructions

ln command is used to create links for files. There are two types of links: hard links and symbolic links. The default link type is hard links. If you want to create a symbolic link you must use the "-s" option.

Note: The symbolic link file is not an independent file. Many of its attributes depend on the source file, so it makes no sense to set access permissions for the symbolic link file.

grammar

ln [options]... [-T] target link name (first format)
Or: ln [option]... target (second format)
Or: ln [option]... target... directory (third format)
Or: ln [option]... -t directory target... (the fourth format)

Options

--backup[=CONTROL] # Create a backup file for each existing target file
-b # Similar to --backup, but does not accept any parameters
-d, -F, --directory # Create a hard link to a directory (superuser only)
-f, --force # Forcefully delete any existing target files
-i, --interactive # Ask user before overwriting existing files
-L, --logical # Dereference the target of a symbolic link
-n, --no-dereference # Treat the destination directory of the symbolic link as a normal file
-P, --physical # Directly convert hard links to symbolic links
-r, --relative # Create a symbolic link relative to the link location
-s, --symbolic # Create symbolic links to source files instead of hard links
-S, --suffix=SUFFIX # After using the "-b" parameter to back up the target file, a backup string will be added to the suffix of the backup file. The default backup string is the symbol "~". The user can pass " -S" parameter to change it
-t, --target-directory=DIRECTORY #Specify the DIRECTORY in which to create the link
-T, --no-target-directory # Treat "LINK_NAME" as a regular file
-v, --verbose #Print the name of each linked file
--help #Display this help message and exit
--version #Display version information and exit

Parameters

none, off # No backup (even if the --backup option is used)
numbered, t # Sort backup files with numbers
existing, nil # If the backup file with number already exists, use the number, otherwise use the normal backup method
simple, never # Always use normal backup method

Example

Link the file m2.c in the directory /usr/mengqc/mub1 to the file a2.c in the directory /usr/liu

cd /usr/mengqc
ln mub1/m2.c /usr/liu/a2.c

Before executing the ln command, the a2.c file does not exist in the directory /usr/liu. After executing ln, there is only a2.c in the /usr/liu directory, indicating that m2.c and a2.c are linked (note that the two are physically the same file), use ls -l You can see changes in the number of links with the command.

Create soft link

Create a symbolic link file abc in the directory /usr/liu and point it to the directory /usr/mengqc/mub1

ln -s /usr/mengqc/mub1 /usr/liu/abc

After executing this command, the path represented by /usr/mengqc/mub1 will be stored in a file named /usr/liu/abc.

Create hard link

Create a hard link to the file and create a hard link ln2022 for log2022.log. The properties of log2022.log and ln2022 are the same.

ln log2022.log ln2022

Output:

[root@localhost test]#ll
lrwxrwxrwx 1 root root 11 12-07 16:01 link2013 -> log2022.log
-rw-r--r-- 1 root bin 61 11-13 06:03 log2022.log
[root@localhost test]# ln log2022.log ln2022
[root@localhost test]#ll
lrwxrwxrwx 1 root root 11 12-07 16:01 link2013 -> log2022.log
-rw-r--r-- 2 root bin 61 11-13 06:03 ln2022
-rw-r--r-- 2 root bin 61 11-13 06:03 log2022.log

Expand knowledge

Linux has the function of giving multiple names to a file, which is called linking. Linked files can be stored in the same directory, but must have different file names, without having to back up the same data repeatedly on the hard disk. In addition, the linked files can also have the same file name but be stored in different directories. In this way, as long as the file in one directory is modified, the linked files with the same name in all directories can be modified. For each linked file of a file, we can assign different access permissions to them to control the sharing of information and enhance security.

File links come in two forms, hard links and symbolic links.

ln function description: It is to establish a synchronized link for a certain file in another location. When we need to use the same file in different directories, we do not need to put a must be the same in every required directory. file, we only need to put the file in a fixed directory, and then use the ln command to link it in another directory, without repeatedly occupying disk space.

⚠️ The ln command will maintain the synchronization of each linked file, that is, no matter which one you change, the other files will also undergo the same changes.

Soft link:

Hard link

When establishing a hard link, add a directory entry of the target file in another directory or this directory, so that one file is registered in multiple directories. The m2.c file shown below creates directory entries in both directories mub1 and liu.

ls -ailR
.:
total 16
922730 drwxr-xr-x 4 root root 4096 Jun 17 11:18 .
393217 drwxrwxrwt. 9 root root 4096 Jun 17 11:19 ..
922733 drwxr-xr-x 2 root root 4096 Jun 17 11:18 liu
922731 -rw-r--r-- 3 root root 0 Jun 17 11:18 m2.c
922732 drwxr-xr-x 2 root root 4096 Jun 17 11:18 mub1

./liu:
total 8
922733 drwxr-xr-x 2 root root 4096 Jun 17 11:18 .
922730 drwxr-xr-x 4 root root 4096 Jun 17 11:18 ..
922731 -rw-r--r-- 3 root root 0 Jun 17 11:18 m2.c

./mub1:
total 8
922732 drwxr-xr-x 2 root root 4096 Jun 17 11:18 .
922730 drwxr-xr-x 4 root root 4096 Jun 17 11:18 ..
922731 -rw-r--r-- 3 root root 0 Jun 17 11:18 m2.c

After creating a hard link, the index node number (inode) of the existing file will be used by multiple directory file entries. The number of hard links for a file can be seen in the second column of the directory's long listing format, files without additional links have a link count of 1.

By default, the ln command creates hard links. The ln command will increase the number of links, and the rm command will decrease the number of links. A file will not be physically deleted from the file system unless its link count is 0.

There are following restrictions on hard links:

Symbolic link (soft link)

A symbolic link, also called a soft link, links a pathname to a file. These files are a special type of file. In fact, it's just a text file (the abc file shown below) that contains the pathname of another file to which it provides a link, as indicated by the dotted arrow. The other file is the one that actually contains all the data. All commands that read and write the contents of a file when used on a symbolic link will proceed along the link direction to access the actual file.

$ls -il
total 0
922736 lrwxrwxrwx 1 root root 5 Jun 17 11:27 abc -> a.txt
922735 -rw-r--r-- 1 root root 0 Jun 17 11:27 a.txt

Unlike a hard link, a symbolic link is indeed a new file, albeit with a different inode number; a hard link does not create a new file.

Symbolic links do not have the restrictions of hard links. Symbolic links can be made to directory files or between different file systems.

When using the ln -s command to create a symbolic link, it is best to use an absolute path name for the source file. This allows symlinks to be made in any working directory. When the source file uses a relative path, if the current working path is different from the path of the symbolic link file to be created, the link cannot be made.

Symbolic links maintain the distinction between the link and the source file or directory: