Detailed description of the parameters used by the linuxln command (ln-s soft link)

Source: Internet
Author: User
This is a very important command in linux. please be familiar with it. The function is to create a non-same 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.

This is a very important command in linux. please be familiar with it. Its function is to create a non-same 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.
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

-S indicates the symbol (symbolic.
Note: First, the ln command will keep the synchronization of each link file. that is to say, no matter where you change it, other files will change the same. second, there are two types of ln links: soft link and hard link. the soft link is ln-s *****. it 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, that is, a file generated using ln command, and you can use ls-l command to view it, the link path is displayed.

Ln is another important command in linux. its function is to create a synchronization link for a file in another location. 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.

1. command format:

Ln [parameter] [source file or directory] [target file or directory]

2. command functions:

In a Linux file system, there is a so-called link. we can regard it as the alias of the file, and there are two types of links: hard link) with symbolic link, a hard link means 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.

Soft link:

1. soft links exist as paths. Similar to shortcuts in Windows
2. soft links can be cross-file systems, but hard links cannot.
3. soft links can be used to link a non-existent file name
4. soft links can be used to link directories

Hard link:

1. hard links exist in the form of file copies. But it does not occupy the actual space.
2. hard links cannot be created for directories.
3. hard links can only be created in the same file system

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, there are two types of ln links: soft link and hard link. the soft link is the target file of the ln-s source file. it will only generate an image of the file at the position you selected, it does not occupy disk space. it is hard linked to the target file of the ln source file without the parameter-s. it will generate a file of the same size as the source file at the position you selected, both soft links and hard links are synchronized.

Ln commands are used to link files or directories. for example, if you specify more than two files or directories at the same time, and the final 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.


3. command parameters:

Required parameters:

-B: delete the file, overwrite the previously created link.
-D allows the superuser to create hard links to directories.
-F force execution
-I interaction mode. if a file exists, the system prompts the user to overwrite the file.
-N indicates the symbolic link as a general directory.
-S soft link (symbolic link)
-V: detailed handling process


Select parameters:

-S "-S <字尾备份字符串> "Or" -- suffix = <字尾备份字符串> "

-V "-V <备份方式> "Or" -- version-control = <备份方式> "

-- Help: displays help information.

-- Version: displays version information.

4. example:

Instance 1: Create a soft link to the file

Command:

Ln-s log2013.log link2013

Output:


Copy codeThe code is as follows:
[Root @ localhost test] # ll
-Rw-r -- 1 root bin 61 11-13 06:03 log2013.log
[Root @ localhost test] # ln-s log2013.log link2013
[Root @ localhost test] # ll
Lrwxrwxrwx 1 root 11 12-07 16:01 link2013-> log2013.log
-Rw-r -- 1 root bin 61 11-13 06:03 log2013.log

Note:

Create a soft link link2013 for the log2013.log file. if log2013.log is lost, link2013 will become invalid.


Instance 2: Create a hard link to the file

Command:

Ln log2013.log ln2013

Output:


Copy codeThe code is as follows:
[Root @ localhost test] # ll
Lrwxrwxrwx 1 root 11 12-07 16:01 link2013-> log2013.log
-Rw-r -- 1 root bin 61 11-13 06:03 log2013.log
[Root @ localhost test] # ln log2013.log ln2013
[Root @ localhost test] # ll
Lrwxrwxrwx 1 root 11 12-07 16:01 link2013-> log2013.log
-Rw-r -- 2 root bin 61 11-13 06:03 ln2013
-Rw-r -- 2 root bin 61 11-13 06:03 log2013.log

Note:

Create a hard link ln2013 for log2013.log. log2013.log has the same attributes as ln2013.

Instance 3: connect the two instances. after the link is complete, delete and recreate the original file.

Command:

Output:


Copy codeThe code is as follows:
[Root @ localhost test] # ll
Lrwxrwxrwx 1 root 11 12-07 16:01 link2013-> log2013.log
-Rw-r -- 2 root bin 61 11-13 06:03 ln2013
-Rw-r -- 2 root bin 61 11-13 06:03 log2013.log
[Root @ localhost test] # rm-rf log2013.log
[Root @ localhost test] # ll
Lrwxrwxrwx 1 root 11 12-07 16:01 link2013-> log2013.log
-Rw-r -- 1 root bin 61 11-13 06:03 ln2013
[Root @ localhost test] # touch log2013.log
[Root @ localhost test] # ll
Lrwxrwxrwx 1 root 11 12-07 16:01 link2013-> log2013.log
-Rw-r -- 1 root bin 61 11-13 06:03 ln2013
--- Xrw-r -- 1 root bin 302108 11-13 log2012.log
-Rw-r -- 1 root 0 12-07 16:19 log2013.log
[Root @ localhost test] # vi log2013.log
2013-01
2013-02
2013-03
2013-04
2013-05
2013-06
2013-07
2013-08
2013-09
2013-10
2013-11
2013-12 [root @ localhost test] # ll
Lrwxrwxrwx 1 root 11 12-07 16:01 link2013-> log2013.log
-Rw-r -- 1 root bin 61 11-13 06:03 ln2013
-Rw-r -- 1 root 96 12-07 16:21 log2013.log
[Root @ localhost test] # cat link2013
2013-01
2013-02
2013-03
2013-04
2013-05
2013-06
2013-07
2013-08
2013-09
2013-10
2013-11
2013-12
[Root @ localhost test] # cat ln2013
Hostnamebaidu = baidu.com
Hostnamesina = sina.com
Hostnames = true

Note:

1. after the source file is deleted, the hard link file is not affected. the soft link file is continuously flashing in the centos system, prompting that the source file does not exist.

2. after the source file is re-built, the soft link does not flash, indicating that the link is successful and the linked file system is found. after reconstruction, the hard link file is not affected by the source file, the content of the source file before deletion is retained, indicating that the hard link is invalid.


Example 4: link the file to the same name in another directory

Command:

Ln log2013.log test3

Output:


Copy codeThe code is as follows:
[Root @ localhost test] # ln log2013.log test3
[Root @ localhost test] # ll
Lrwxrwxrwx 1 root 11 12-07 16:01 link2013-> log2013.log
-Rw-r -- 1 root bin 61 11-13 06:03 ln2013
-Rw-r -- 2 root 96 12-07 16:21 log2013.log
[Root @ localhost test] # cd test3
[Root @ localhost test3] # ll
-Rw-r -- 2 root 96 12-07 16:21 log2013.log
[Root @ localhost test3] # vi log2013.log
2013-01
2013-02
2013-03
2013-04
2013-05
2013-06
2013-07
2013-08
2013-09
2013-10 [root @ localhost test3] # ll
-Rw-r -- 2 root 80 12-07 16:36 log2013.log
[Root @ localhost test3] # cd ..
[Root @ localhost test] # ll
Lrwxrwxrwx 1 root 11 12-07 16:01 link2013-> log2013.log
-Rw-r -- 1 root bin 61 11-13 06:03 ln2013
-Rw-r -- 2 root 80 12-07 16:36 log2013.log
[Root @ localhost test] #

Note:

Create a hard link for log2013.log in the test3 Directory, modify the log2013.log file in the test3 directory, and synchronize the file to the source file.

Instance 5: create a soft link to the Directory

Command:

Ln-sv/opt/soft/test/test3/opt/soft/test/test5

Output:


Copy codeThe code is as follows:
[Root @ localhost test] # ll
Drwxr-xr-x 2 root 4096 12-07 16:36 test3
Drwxr-xr-x 2 root 4096 12-07 16:57 test5
[Root @ localhost test] # cd test5
[Root @ localhost test5] # ll
Lrwxrwxrwx 1 root 5 12-07 16: 57 test3-> test3
[Root @ localhost test5] # cd test3
-Bash: cd: test3: too many symbolic connections
[Root @ localhost test5] #
[Root @ localhost test5] #
[Root @ localhost test5] # ll
Lrwxrwxrwx 1 root 5 12-07 16: 57 test3-> test3
[Root @ localhost test5] # rm-rf test3
[Root @ localhost test5] # ll
[Root @ localhost test5] # ln-sv/opt/soft/test/test3/opt/soft/test/test5
Create the symbolic link "/opt/soft/test/test3" to "/opt/soft/test/test5/test3"
[Root @ localhost test5] # ll
Lrwxrwxrwx 1 root 20 12-07 16: 59 test3->/opt/soft/test/test3
[Root @ localhost test5] #
[Root @ localhost test5] # cd test3
[Root @ localhost test3] # ll
Total 4
-Rw-r -- 2 root 80 12-07 16:36 log2013.log
[Root @ localhost test3] # touch log2014.log
[Root @ localhost test3] # ll
Total 4
-Rw-r -- 2 root 80 12-07 16:36 log2013.log
-Rw-r -- 1 root 0 12-07 17:05 log2014.log
[Root @ localhost test3] # cd ..
[Root @ localhost test5] # cd ..

Note:

1. the directory can only create soft links
2. the directory creation link must use an absolute path. the relative path cannot be created successfully. the following error occurs: too many layers of symbolic connections.
3. files modified in the target Directory of the link will be synchronized and changed in the source directory.

There are two types of links: Hard Link and Symbolic Link ). When creating a hard link, the linked file and the linked file must be in the same file system, and cannot establish a hard link to the directory. This problem does not exist for symbolic links. By default, ln generates hard links.

Ln command

Purpose
Link file.
Syntax
Link a file to a file
Ln [-f |-n] [-s] SourceFile [TargetFile]
Link one or more files to a directory
Ln [-f |-n] [-s] SourceFile... TargetDirectory
Description
The ln command links the file specified in the SourceFile parameter to the file specified in the TargetFile parameter, or links it to the file in another directory specified in the TargetDirectory parameter. By default, the ln command creates a hard link. If you need to use the ln command to create a symbolic link, specify the-s flag.
A symbolic link is an indirect pointer to a file. its directory item contains the file name linked to it. Symbolic links may span the file system and may point to directories.
If you are linking a file to a new name, you can only list one file. If you link to a directory, you can list multiple files.
The TargetFile parameter is optional. If no target file is specified, the ln command creates a new file in the current directory. The new file inherits the file name specified in the SourceFile parameter. See example 5.

Note:
If the-s flag is not used, files cannot be linked between file systems.
If TargetDirectory is already a symbolic link to the directory, the ln command treats the existing target as a file. This means that commands similar to ln-fs somepath/lname symdir do not follow the existing symdir symbolic link. Instead, it creates a new symbolic link from somepath/lname to symdir.
Flag
-F prompts the ln command to replace any existing destination path. If the target path already exists without specifying the-f flag, the ln command will not create a new link, but will write a diagnostic message to the standard error and continue to link the remaining SourceFiles.
-N is specified. if the link is an existing file, do not overwrite the file content. -F indicates that the flag is reset. This is the default action.
-S promotes the ln command to create a symbolic link. The symbolic link contains the name of the file to which it is linked. When you open a link, the referenced file is used. The stat call to the symbolic link returns the target file of the link. the lstat call must be completed to obtain the link information. You can use readlink to read the content of a symbolic link. A symbolic link may span the file system and point to a directory.
Note: When the SourceFile parameter is specified for the-s flag, the absolute path must be used. If no absolute path is specified, unexpected results may occur when the SourceFile and TargetFile parameters are in different directories. You do not need to have a source file before creating a symbolic link.

Exit status

This command returns the following exit values:
0. all specified files are successfully linked.
> 0 indicates an error.

Example

0. to create another link (alias) to a file, enter:
Ln-f chap1 intro
This will link Chapter 1 to the new name, intro. If intro does not exist, the file name is created. If the intro already exists, the file will be replaced with a link pointing to Chapter 1. The file names of chapter 1 and intro point to the same file. Any change to one of them 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 other names.

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

Ln index manual
This will link the index to the new name, manual/index.

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

2. to link several files to another directory name, enter:

Ln chap2 jim/chap3/home/manual
This will link chap2 to the new name/home/manual/chap2; and jim/chap3 to the new name/home/manual/chap3.

3. if you want to use the pattern matching character in the ln command, enter:

N manual /*.
This will link all files in the manual directory to the current directory. (point) and give them the same name in the manual directory.

Note: a space must be entered between the asterisks and periods.

4. to create a symbolic link, enter:

Ln-s/tmp/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 content.

5. if you want to get the same result without specifying the TargetFile parameter, enter:

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.