Instructions for the LN command in the Linux command encyclopedia (creating soft links and hard links)

Source: Internet
Author: User

LN is another very important command in Linux, and its function is to create a synchronized link for a file in another location, broken down into soft links and hard links. Soft links are equivalent to Windows shortcuts, and here are the ways to use and examples

LN is another very important command in Linux, Its function is to create a synchronized link for a file in another location. When we need to use the same file in different directories, we don't need to put a file in every required directory, we just put the file in a fixed directory, and then The other directory uses the ln command to link it so that it does not have to use disk space repeatedly.

1. Command format:
ln [parameter] [source file or directory] [destination file or directory]

2. Command function:
Linux file system, there is so-called link, we can consider it as the alias of the file, and the link can be divided into two: Hard link and soft link (symbolic link), hard link means that a file can have multiple names, The soft link is the way to produce a special file, the contents of which is pointing to the location of another file. Hard links exist in the same file system, but soft links can span different file systems.

Soft Links:
1. Soft links, in the form of a path exists. Similar to shortcuts in the Windows operating system
2. Soft links can cross file system, hard links can not
3. Soft links can link to a nonexistent file name
4. Soft links can be linked to the directory

Hard Links:
1. Hard link, in the form of a copy of the file. But does not occupy the actual space.
2. Do not allow the creation of hard links to the directory
3. Hard links can only be created in the same file system
Here are two points to note:
First, the LN command maintains the synchronization of each linked file, meaning that no matter where you change it, the other files will change the same.
Second, Ln's links are soft links and hard links two, soft link is the ln–s source file destination file, it will only generate a file in your selected location image, will not occupy disk space, hard link ln source file destination file, no parameter-s, It will generate a file of the same size as the source file in the location you selected, whether it is a soft link or a hard link, and the file keeps changing synchronously.
The ln instruction is used in a linked file or directory, such as specifying more than two files or directories at the same time, and the final destination is a directory that already exists, and all of the previously specified files or directories are copied to the directory. If you specify multiple files or directories at the same time, and the final destination is not an existing directory, an error message appears.

3. Command parameters:
Necessary parameters:
-B Delete, overwrite previously established link
-D allows super users to make hard links to directories
-F Force Execution
-I interactive mode, file presence prompts the user whether to overwrite
-N treats symbolic links as generic directories
-S soft link (symbolic link)
-V displays detailed processing procedures

Select parameters:
-S "-s< tail backup string >" or "--suffix=< tail backup string >"
-V "-v< backup method >" or "--version-control=< backup method >"
--HELP Display Help information
--version displaying version information

4. Usage examples:
Example 1: Create a soft link to a file
Command: Ln-s log2013.log link2013
Output:

 The code is as follows:
[email protected] test]# LL
-rw-r--r--1 Root bin 11-13 06:03 Log2013.log
[Email protected] test]# ln-s log2013.log link2013
[email protected] test]# LL
lrwxrwxrwx 1 root root one 12-07 16:01 link2013 log2013.log
-rw-r--r--1 Root bin 11-13 06:03 Log2013.log
Description: Create a soft link link2013 for the Log2013.log file, link2013 will fail if Log2013.log is lost

Example 2: Create a hard link to a file
Command: LN log2013.log ln2013
Output:

The code is as follows:
[email protected] test]# LL
lrwxrwxrwx 1 root root one 12-07 16:01 link2013 log2013.log
-rw-r--r--1 Root bin 11-13 06:03 Log2013.log
[Email protected] test]# LN log2013.log ln2013
[email protected] test]# LL
lrwxrwxrwx 1 root root one 12-07 16:01 link2013 log2013.log
-rw-r--r--2 Root bin 11-13 06:03 ln2013
-rw-r--r--2 Root bin 11-13 06:03 Log2013.log
Description: Create a hard link for log2013.log ln2013,log2013.log the same as the properties of ln2013

Example 3: Connect the above two instances, after the link is complete, delete and rebuild the link original file
Command: LL
Output:

 The code is as follows:
[email protected] test]# LL
lrwxrwxrwx 1 root root one 12-07 16:01 link2013 log2013.log
-rw-r--r--2 Root bin 11-13 06:03 ln2013
-rw-r--r--2 Root bin 11-13 06:03 Log2013.log
[Email protected] test]# RM-RF Log2013.log
[email protected] test]# LL
lrwxrwxrwx 1 root root one 12-07 16:01 link2013 log2013.log
-rw-r--r--1 Root bin 11-13 06:03 ln2013
[email protected] test]# Touch Log2013.log
[email protected] test]# LL
lrwxrwxrwx 1 root root one 12-07 16:01 link2013 log2013.log
-rw-r--r--1 Root bin 11-13 06:03 ln2013
---xrw-r--1 root bin 302108 11-13 06:03 log2012.log
-rw-r--r--1 root root 0 12-07 16:19 log2013.log
[Email protected] 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[[email protected] test]# ll
lrwxrwxrwx 1 root root one 12-07 16:01 link2013 log2013.log
-rw-r--r--1 Root bin 11-13 06:03 ln2013
-rw-r--r--1 root root 12-07 16:21 Log2013.log
[email protected] 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
[email protected] test]# cat ln2013
Hostnamebaidu=baidu.com
Hostnamesina=sina.com
Hostnames=true
Description
1. When the source file is deleted, it does not affect the hard link file; The soft link file flashes continuously under the CentOS system, prompting the source file to no longer exist.
2. After rebuilding the source file, the soft link is not flashing prompt, indicating that the link file system has been linked successfully, after rebuilding, the hard link file is not affected by the source file, the contents of the hard-linked file or the content of the source file before deletion, indicating that the hard link is invalid

Example 4: Link a file to the same name in another directory
Command: LN log2013.log test3
Output:

 The code is as follows:
[Email protected] test]# LN log2013.log test3
[email protected] test]# LL
lrwxrwxrwx 1 root root one 12-07 16:01 link2013 log2013.log
-rw-r--r--1 Root bin 11-13 06:03 ln2013
-rw-r--r--2 root root 12-07 16:21 Log2013.log
[Email protected] test]# CD TEST3
[email protected] test3]# LL
-rw-r--r--2 root root 12-07 16:21 Log2013.log
[Email protected] test3]# VI log2013.log
2013-01
2013-02
2013-03
2013-04
2013-05
2013-06
2013-07
2013-08
2013-09
2013-10[[email protected] test3]# ll
-rw-r--r--2 root root 12-07 16:36 Log2013.log
[Email protected] test3]# CD.
[email protected] test]# LL
lrwxrwxrwx 1 root root one 12-07 16:01 link2013 log2013.log
-rw-r--r--1 Root bin 11-13 06:03 ln2013
-rw-r--r--2 root root 12-07 16:36 Log2013.log
[Email protected] test]#
Description: Created a hard link to log2013.log in the Test3 directory, modified the Log2013.log file in the Test3 directory, and synchronized to the source file

Example 5: Creating a soft link to a directory
Command: LN-SV/OPT/SOFT/TEST/TEST3/OPT/SOFT/TEST/TEST5
Output:

 The code is as follows:
[email protected] test]# LL
Drwxr-xr-x 2 root root 4096 12-07 16:36 test3
Drwxr-xr-x 2 root root 4096 12-07 16:57 test5
[Email protected] test]# CD TEST5
[email protected] test5]# LL
lrwxrwxrwx 1 root root 5 12-07 16:57 test3 test3
[Email protected] test5]# CD TEST3
-BASH:CD:TEST3: Excessive number of layers for symbolic connections
[Email protected] test5]#
[Email protected] test5]#
[email protected] test5]# LL
lrwxrwxrwx 1 root root 5 12-07 16:57 test3 test3
[Email protected] test5]# RM-RF test3
[email protected] test5]# LL
[Email protected] test5]# LN-SV/OPT/SOFT/TEST/TEST3/OPT/SOFT/TEST/TEST5
Create a symbolic link "/opt/soft/test/test5/test3" pointing to "/opt/soft/test/test3"
[email protected] test5]# LL
lrwxrwxrwx 1 root root 12-07 16:59 test3/opt/soft/test/test3
[Email protected] test5]#
[Email protected] test5]# CD TEST3
[email protected] test3]# LL
Total 4
-rw-r--r--2 root root 12-07 16:36 Log2013.log
[email protected] test3]# Touch Log2014.log
[email protected] test3]# LL
Total 4
-rw-r--r--2 root root 12-07 16:36 Log2013.log
-rw-r--r--1 root root 0 12-07 17:05 log2014.log
[Email protected] test3]# CD.
[Email protected] test5]# CD.
Description
1. Directories can only create soft links
2. Directory creation link must use absolute path, relative path creation will not be successful, will prompt: The number of layers of the symbolic connection too many such errors
3. modifying files in the target directory of the link changes synchronously in the source file directory

Instructions for the LN command in the Linux command encyclopedia (creating soft links and hard links)

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.