Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the ln command of Linux

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how to use the ln command of Linux". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn how to use the ln command of Linux.

Linux common command ln command is used to create links for files, link types are divided into hard links and symbolic links, the default link type is hard links. You must use the "- s" option if you want to create symbolic links.

Ln is used to create links for files

Note: the symbolic link file is not a separate file, and many of its attributes depend on the source file, so it is meaningless to set access permissions to the symbolic link file.

Syntax ln [options]... [- T] destination link name (first format) or: ln [option]. Target (second format) or: ln [options]. The target. Directory (third format) or: ln [options]. -t directory target. (fourth format) option-- backup [= CONTROL] create a backup file for each existing target file-b similar to-- backup But do not accept any parameters-d,-F,-- directory to create a hard link to a directory (only for superusers)-f,-- force forcibly delete any existing target files-I,-- interactive asks the user before overwriting existing files -L,-- logical dereference as the target of the symbolic link-n,-- no-dereference treats the destination directory of the symbolic link as a general file -P,-- physical links directly to symbolic links-r,-- relative creates symbolic links relative to link locations-s,-- symbolic establishes symbolic links to source files instead of hard links -S,-- after suffix=SUFFIX backs up the target file with the "- b" parameter, a backup string is added to the suffix of the backup file. The preset backup string is the symbol "~". Users can change it through the "- S" parameter. -t,-- target-directory=DIRECTORY specifies the DIRECTORY-T in which to create the link,-- no-target-directory treats "LINK_NAME" as a regular file-v,-- verbose prints the name of each linked file-- help displays this help information and exits-- version displays version information and exits the parameter source file: specify the linked source file. If you use the-s option to create a symbolic link, the Source File can be a file or directory. When creating a hard link, the Source File parameter can only be a file

Destination file: specifies the destination link file for the source file.

None, off # do not backup (even if the-backup option is used) numbered, t # backup files plus numbers to sort existing, nil # use numbers if digital backup files already exist, otherwise back up simple in the normal way, never # always use the normal backup instance to link the file m2.c under directory / usr/mengqc/mub1 to the file a2.c under 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 ln is executed, a2.c is not available in the / usr/liu directory, indicating that m2.c and a2.c are linked (note that they are physically the same file), and you can see the change in the number of links using the ls-l command.

Create a symbolic link file abc under the directory / usr/liu to point to the directory / usr/mengqc/mub1

After ln-s / usr/mengqc/mub1 / usr/liu/abc executes the command, the path represented by / usr/mengqc/mub1 is stored in a file called / usr/liu/abc.

Extended knowledge Linux has the ability to give multiple names to a file, called a link. Linked files can be stored in the same directory, but they must have different file names instead of repeatedly backing up the same data on the hard disk. In addition, the linked file can also have the same file name, but stored in a different directory, so that as long as you modify the file in one directory, you can modify the linked files with the same name in all directories. For the linked files of a file, we can assign them different access permissions to control the sharing of information and enhance security.

There are two forms of file links, hard links and symbolic links.

When a hard link is established, a directory entry for the target file is added to another directory or in this directory, so that a file is registered in multiple directories. The m2.c file shown in the figure creates directory entries in both the directories mub1 and liu.

After you create a hard link, the I node number (Inode) of an existing file is used by multiple directory file entries. The number of hard links for a file can be seen in the second column of the long list format of the directory, and the number of links for files without additional links is l.

By default, the ln command creates hard links. The ln command increases the number of links, and the rm command reduces the number of links. A file is not physically deleted from the file system unless the number of links is 0.

There are the following restrictions on hard links:

You cannot make hard links to directory files.

Hard links cannot be made between different file systems. That is, the linked file and the linked file must be on the same file system.

Symbolic links, also known as soft links, link a pathname to a file. These files are a special type of file. In fact, it is just a text file (such as the abc file in the figure) that contains the pathname of another file to which it provides a link, as shown by the dotted arrow in the figure. The other file is the one that actually contains all the data. When all commands to read and write the contents of a file are used for symbolic links, they will come in and access the actual file in the direction of the link.

! Symbolic connection

Unlike a hard link, a symbolic link is indeed a new file, of course it has a different I node number; a hard link does not create a new file.

Symbolic links have no restrictions on hard links. You can make symbolic links to directory files or between different file systems.

When establishing a symbolic link with the ln-s command, it is best to use an absolute pathname for the source file. This allows symbolic links to be made in any working directory. When the source file uses a relative path, you cannot link if the current working path is different from the path of the symbolic link file you want to create.

Symbolic links keep the difference between a link and a source file or directory:

Delete the source file or directory, only the data is deleted, and the link is not deleted. Once the source file is created with the same file name, the link continues to point to the new data for that file.

In the long list of directories, symbolic links are displayed as a special file type, with the first letter l.

The size of a symbolic link is the number of bytes in the pathname of its linked file.

When listing files with the ln-s command, you can see that there is an arrow pointing to the source file or directory after the symbolic link name, such as lrwxrwxrwx... 14 jun 20 10:20 / etc/motd- > / original_file where the number "14" for "file size" indicates that the source file name original_file is exactly 14 characters.

Thank you for your reading, the above is the content of "how to use the ln command of Linux". After the study of this article, I believe you have a deeper understanding of how to use the ln command of Linux, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report