Using the LN command in Linux to establish a connection between files is explained

Source: Internet
Author: User
Tags manual file system backup

There are two ' link ' concepts in the UNIX world, commonly referred to as hard and soft connections. A hard connection is just a filename. (a file can have several file names and only delete the last file name from the disk before deleting the file.) The number of filenames is determined by LS (1). All file names are in the same state, and there is no LQ source name, RQ said. Typically, all the names of a file in a file system contain the same data information, but this is not required. A soft connection (or symbolic connection) is completely different: it is a small, specified file that contains the path information. As a result, soft connections can point to files in different file systems (such as files on different machine file systems mounted by NFS), or even to a file that does not necessarily exist. When a soft connection file is accessed (the system calls open (2) or stat (2)), the operating system replaces the access intervention point of the file with the path it contains, thereby enabling access to the indicated file. (Use the command RM (1) and unlink (2) to delete the connection, but not to delete the file that the file points to.) The system specifies that Lstat (2) and Readlink (2) are invoked to read the status of the connection file and the file it refers to. There is a difference between the operation of the soft connection file or the point to file operation, because of different system calls to different operating system parts. )

LN creates a connection between the files. By default, a hard connection is generated, and the-s option produces a symbolic (soft) connection.

If only one file name is given, then LN will generate a connection to the file in the current directory, that is, to create a connection in the current directory (within the GNU context) with the name equivalent to that file (the last). If the last parameter is an existing directory name, LN will give each source file a connection to the same file name as the source file in that directory (see--no-dereference below for different scenarios); If only two file names are given, LN will generate a connection to the source file; If the last parameter is not a directory name or more than two file names, the error occurs.

By default, LN does not delete existing files or symbolic connections. (therefore, it can be used to lock the target file, that is, when the dest no longer exists, but option-F can be enforced.)

In an existing implementation, only superuser can establish a hard connection to the directory. POSIX prohibits system calls to link (2) and LN to establish a hard connection to the directory (but allow hard connections between different file systems).

How to use: ln [options] Source dist, where option is in the form of:

[-BDFINSVF] [-S Backup-suffix] [-v {numbered,existing,simple}] [--help] [--version] [--]

Description: Linux/unix file system, there is a so-called link, we can treat it as a file alias, and the link can be divided into two kinds: hard link (hard link) and soft link (symbolic link), hard link means that a file can have more than one name, The soft link is to create a special file that points to the location of another file. Hard links exist in the same file system, while soft links can span different file systems.

The LN source Dist produces a link (dist) to source, as with a hard link or a soft link is determined by the parameter.

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

-F: First delete files with dist file name when linking

-D: Allows system managers to hard link their own directories

-I: Ask before deleting files with the same file name as Dist

-N: Dist is considered a general file when soft links are made

-S: Soft link (symbolic link)

-V: Displays the file name before the link

-B: Backup files that will be overwritten or deleted when the link is made

-S SUFFIX: Add the backup file to the end of the SUFFIX

-V Method: Specify how the backup should be

--HELP: Show Auxiliary Instructions

--version: Display version

Attention:

If you do not use the-s flag, you cannot link files between file systems.

If TargetDirectory is already linked to a symbolic link on the directory, the LN command treats the existing target as a file. This means that a command similar to LN-FS Somepath/lname Symdir does not follow an existing Symdir symbolic link, and instead, it creates a new symbolic link from somepath/lname to Symdir.

Exit status

This command returns the following exit values:

0 all the specified files have been successfully linked.

>0 an error occurred.

Cases:

The code is as follows:

[Root@localhost test_ln]# ln-s/home/zhangy/heartbeat./#在当前目录下, establish heartbeat soft connection

[Root@localhost test_ln]# ln-s/home/zhangy/test./#建立目录的软件接

[Root@localhost test_ln]# ln/home/zhangy/tee.txt./#建立硬连接

[Root@localhost test_ln]# LL

Total 4

lrwxrwxrwx 1 root 05-11 11:04 heartbeat->/home/zhangy/heartbeat #文件软件接

-rw-r--r--2 Root 0 2010-11-24 tee.txt #文件硬连接

lrwxrwxrwx 1 root 05-11 11:04 test->/home/zhangy/test #目录软件接

1. In order to create another link to a file (alias), enter:

The code is as follows:

Ln-f CHAP1 Intro

This will link Chap1 to the new name, intro. If intro does not exist, the file name is created. If intro already exists, the file is replaced with a link to chap1. The Chap1 and intro file names will then point to the same file. Changes to either of these will appear in the other. If a file name is deleted by the RM command, the file is not completely deleted because it still exists with a different name.

2. To link the file to the same name in another directory, enter:

The code is as follows:

ln Index Manual

This will link the index to the new name, Manual/index.

Note: The intro in Example 1 is the name of a file; in Example 2, manual is an existing directory.

3. To link several files to a name in another directory, enter:

The code is as follows:

ln CHAP2 jim/chap3/home/manual

This links chap2 to the new name/HOME/MANUAL/CHAP2, and links jim/chap3 to the new name/HOME/MANUAL/CHAP3.

4. If you want to use pattern matching characters in the ln command, enter:

The code is as follows:

ln manual/*.

This links all files in the manual directory to the current directory. (dot), give them the same name in the manual directory.

Note: You must enter a space between the asterisk and the period.

5. In order to create a symbolic link, enter:

The code is as follows:

Ln-s/tmp/toc TOC

This creates a symbolic link TOC in the current directory. The TOC file points to the/tmp/toc file. If the/tmp/toc file already exists, the Cat TOC command can list its contents.

6. If you want to get the same result without indicating the targetfile parameter, enter:

The code is as follows:

Ln-s/tmp/toc

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.