One Linux command per day (5): in command

Source: Internet
Author: User

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 exist in the form of paths. 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 a 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 hard links to be created for directories

(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:

[email protected] test]# ll-rw-r--r--1Root bin A  One- -  .:Genevalog2013.log[[email protected] test]# ln-s Log2013.log link2013[[email protected] test]# lllrwxrwxrwx1Root root One  A- -  -: onlink2013Log2013.log-rw-r--r--1Root bin A  One- -  .:GenevaLog2013.log

Description

Create soft link link2013 for Log2013.log file, link2013 will expire if Log2013.log is lost

Example 2: Create a hard link to a file

Command: LN log2013.log ln2013

Output:

[email protected] test]# lllrwxrwxrwx1Root root One  A- -  -: onlink2013Log2013.log-rw-r--r--1Root bin A  One- -  .:Genevalog2013.log[[email protected] test]# ln log2013.log ln2013[[email protected] test]# lllrwxrwxrwx1Root root One  A- -  -: onlink2013Log2013.log-rw-r--r--2Root bin A  One- -  .:Genevaln2013-rw-r--r--2Root bin A  One- -  .:GenevaLog2013.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:

Output:

[email protected] test]# lllrwxrwxrwx1Root root One  A- -  -: onlink2013Log2013.log-rw-r--r--2Root bin A  One- -  .:Genevaln2013-rw-r--r--2Root bin A  One- -  .:GenevaLog2013.log[[email protected] test]# RM-RF Log2013.log [[email protected] test]# lllrwxrwxrwx1Root root One  A- -  -: onlink2013Log2013.log-rw-r--r--1Root bin A  One- -  .:GenevaLn2013[[email protected] test]# touch Log2013.log[[email protected] test]# lllrwxrwxrwx1Root root One  A- -  -: onlink2013Log2013.log-rw-r--r--1Root bin A  One- -  .:Genevaln2013---xrw-r--1Root bin302108  One- -  .:GenevaLog2012.log-rw-r--r--1Root root0  A- -  -: +Log2013.log[[email protected] test]# VI log2013.log -- on -- Geneva --Geneva --Geneva -- to -- . -- - -- , -- the --Ten -- One -- A[email protected] test]# lllrwxrwxrwx1Root root One  A- -  -: onlink2013Log2013.log-rw-r--r--1Root bin A  One- -  .:Genevaln2013-rw-r--r--1Root root the  A- -  -: +Log2013.log[[email protected] test]# cat link2013 -- on -- Geneva --Geneva --Geneva -- to -- . -- - -- , -- the --Ten -- One -- A[email protected] test]# cat ln2013 Hostnamebaidu=Baidu.comhostnamesina=Sina.comhostnames=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:

[[email protected] test]# ln log2013.log test3[[email protected] test]# lllrwxrwxrwx1Root root One  A- -  -: onlink2013Log2013.log-rw-r--r--1Root bin A  One- -  .:Genevaln2013-rw-r--r--2Root root the  A- -  -: +Log2013.log[[email protected] test]# CD Test3[[email protected] test3]# ll-rw-r--r--2Root root the  A- -  -: +Log2013.log[[email protected] test3]# VI log2013.log -- on -- Geneva --Geneva --Geneva -- to -- . -- - -- , -- the --Ten[email protected] test3]# ll-rw-r--r--2Root root the  A- -  -: $Log2013.log[[email protected] test3]# CD. [Email protected] test]# lllrwxrwxrwx1Root root One  A- -  -: onlink2013Log2013.log-rw-r--r--1Root bin A  One- -  .:Genevaln2013-rw-r--r--2Root root the  A- -  -: $Log2013.log[[email protected] test]#

Description

Log2013.log hard links are created in the Test3 directory, the Log2013.log files in the Test3 directory are modified, and they are 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:

[email protected] test]# LLDRWXR-xr-x2Root root4096  A- -  -: $TEST3DRWXR-xr-x2Root root4096  A- -  -: $Test5[[email protected] test]# CD Test5[[email protected] test5]# lllrwxrwxrwx1Root root5  A- -  -: $Test3Test3[[email protected] test5]# CD test3-BASH:CD:TEST3: Excessive number of layers for symbolic connections [[email protected] test5]# [[email protected] test5]# [[email protected] test5]# LLLRWXRWXRW X1Root root5  A- -  -: $Test3Test3[[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 pointer to the/opt/soft/test/test3 "Symbolic Link"/opt/soft/test/test5/test3 "[[email protected] test5]# lllrwxrwxrwx1Root root -  A- -  -: -Test3-/opt/soft/test/Test3[[email protected] test5]# [[email protected] test5]# CD Test3[[email protected] test3]# ll Total4-rw-r--r--2Root root the  A- -  -: $Log2013.log[[email protected] test3]# touch Log2014.log[[email protected] test3]# ll Total4-rw-r--r--2Root root the  A- -  -: $Log2013.log-rw-r--r--1Root root0  A- -  -: toLog2014.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

One Linux command per day (5): in command

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.