Linux ln command details and usage, Linux ln command details

Source: Internet
Author: User

Linux ln command details and usage, Linux ln command details

Linux ln command details and usage


Ln is a very important command in linux. Its function is to create a different link for a file in another location. The most common parameter of this command is-s, the specific usage is the target file of the ln-s source file.


1. ln command Overview

When we need to use the same file in different directories, we do not need to put a file that must be the same under each required directory. We only need to put it in a fixed directory, put the file, and then use the ln command link in other directories to link it, without occupying disk space repeatedly. Example: ln-s/bin/less/usr/local/bin/less

This is similar to the shortcut in Windows, but it is different.

-S indicates the symbol (symbolic.

Note the following two points:

First, the ln command will keep the synchronization of each link file. That is to say, other files will change the same no matter which one you have changed;

Second, ln links are divided into two types: Soft links and hard links. The soft links are ln-s *****, which will only generate a file image at your selected location, it does not occupy disk space. The hard link ln *** has no parameter-s. It will generate a file of the same size as the source file at the selected position, both soft links and hard links are synchronized.

If you use ls to view a directory, you will find that some files are followed by a @ symbol, and the color of the file or folder is different from other ones, and my computer is blue, that is, a file generated using the ln command. Use the ls-l command to view the link path displayed.

You can also use the ll command to view the path of the soft connection and its link.



2. ln command details

Command name: ln

Permission: All Users

Usage:Ln [options] source dist

The option format is:

[-BdfinsvF] [-S backup-suffix] [-V {numbered, existing, simple}]

[-- Help] [-- version] [--]

Note: There is a link in the Linux/Unix File System. we can regard it as the alias of the file, and the link can be divided into two types: hard link) symboliclink indicates that a file can have multiple names, while a soft link generates a special file, the content of this file points to another file. Hard links exist in the same file system, but soft links can span different file systems.

Ln source dist is used to generate a connection (dist) to the source. As for hard link or soft link, it is determined by the parameter.

No matter whether it is a hard link or soft link, it will not copy the original file, it will only occupy a very small amount of disk space.

-F: when the link is closed, the file with the same name as dist is deleted first.

-D: allows system administrators to hard link their directories.

-I: When deleting an archive with the same name as dist, first ask

-N: dist is considered as a normal file During Soft link.

-S: symboliclink)

-V: display the file name before the link

-B: Back up the files that will be overwritten or deleted during the link.

-SSUFFIX: adds the backup file to the end of SUFFIX.

-VMETHOD: Specifies the backup method.

-- Help: displays auxiliary instructions

-- Version: displays the version.

Example:

# Generate a symbolic link: zyyln-s xyy zyy for file xyy # generate a hard link for file xyy: zyylnxyy zyy ln-s abc cde # create a soft connection of abc lnabc cde # create a hard connection of abc

PS: If you want to delete a link, you can directly use the rm Link name like a common file.


3. Differences between soft links and hard links

A hard link can be considered as a file with two file names, while a soft link is a new link file created by the system, which points to the file to which it refers. In addition, soft links can be used for files and folders, while hard links are only used for files.

For a file, there is a unique index contact corresponding to it, but for an index contact number, there can be multiple file names corresponding to it. Therefore, the same file on the disk can be accessed through different paths. Note that in Linux, all files, folders, and newly added hard disks can all be processed by looking at the files.

A soft connection or a hard link is also called a symbolic link. Symbolic connection is equivalent to a shortcut in Windows.

Hard connections to folders are not allowed. We usually use many soft connections.

 

Example:

Ln-s source dist # establish a soft connection

Ln source dist # establish a hard connection

A soft link is actually just a piece of text that contains the name of the file it points to. The system automatically jumps to the corresponding file location after seeing the soft link. On the contrary, hard join opens a new directory item for the file. The hard link is equivalent to the original name of the file. in Linux, they are equivalent. For this reason, hard links cannot connect files on two different file systems.

(1) soft connections can span file systems, but hard Connections cannot. In practice, the aa.txt text file under Windows is connected to bb, cc. ln -saa.txt/root/bb in linux/root directory with a shared file. Ln aa.txt/root/bb failed.

(2) I node issues. No matter how many hard connections direct to the same I node, the number of connections at the node increases. As long as the number of connections at the node is not 0, the file will always exist, whether you delete the source file or the connected file. As long as one exists, the file exists (in fact, no source file is connected, because they all point to the same I node ). When you modify the source file or the connection file, other files are synchronized. Soft links do not directly use the I node number as the file pointer, but use the file path name as the pointer. Therefore, deleting the connection file does not affect the source file. However, deleting the source file does not affect the file to be directed. Soft links have their own inode, and there is a small space on the disk to store the path name.

(3) A soft connection can be used to connect a non-existent file name.

(4) soft connections can be used to connect directories.

(5) delete a symbolic link. If a symbolic link is created, delete it.

rm -rf  symbolic_name  

Note that it is not rm-rf symbolic_name/ 


How to use linux ln connection commands?

Linux ln (link) command details

Function Description: connect to a file or directory.

Syntax: ln [-bdfinsv] [-S <character tail backup string>] [-V <backup mode>] [-- help] [-- version] [source file or directory] [target file or directory] or ln [-bdfinsv] [-S <tail backup string>] [-V <backup mode>] [-- help] [-- version] [source file or directory...] [destination Directory]

Note: ln commands are used to connect files or directories. For example, if two or more files or directories are specified at the same time, and the last destination is an existing Directory, all the previously specified files or directories will be copied to this directory. If multiple files or directories are specified at the same time and the destination is not an existing Directory, an error message is displayed.

Parameters:
-B or -- backup is deleted to overwrite the backup before the target file.
-D,-F, or -- directory creates a directory hard connection.
-F or -- force forcibly establish a connection to a file or directory, regardless of whether the file or directory exists.
-I or -- interactive ask the user before overwriting existing files.
-N or -- no-dereference treats the target directory of the symbolic connection as a general file.
-S or -- symbolic creates a symbolic connection to the source file instead of a hard connection.
-S <suffix backup string> or -- suffix = <suffix backup string> use the "-B" parameter to back up the target file.

Linux ln command usage

Yes. In the C directory, create the connection file for files in directory A and directory C and use the ln-s command. For example, if file A is in directory, if the name of The Link in the C directory is a, the ln-s/A/a/C/al can be used. Assume that the directory A and C are in the root directory.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.