Detailed explanation of ln commands in linux commands (creating soft links and hard links)

Source: Internet
Author: User
Ln is another important command in linux. its function is to create a synchronous link for a file in another location, which can be soft link or hard link. Soft links are similar to shortcuts for windows. The following are usage methods and examples.

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: ll
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: The hard link of log2013.log is created in the test3 directory, and the log2013.log file in the test3 directory is modified and synchronized 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 :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.

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.