One Linux command per day (--LN) command

Source: Internet
Author: User

LN is another very important command in Linux, it is the function of a file in another location to establish a synchronized link, when we need to be in a different directory, the same file, we do not need to put the same file in each required directory, we just in a fixed directory, put the file , and then use the LN command in other directories to link it so that it does not have to be repeatedly consuming disk space.

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

Attention:

First, the LN command will maintain the synchronization of each linked file, meaning that no matter where you change, the other files will change the same;

Second, ln links and soft links and hard links two, soft link is the ln-s source file destination file It will only generate a file in the location you selected image, does not occupy disk space,

Hard Link ln Source file destination file, no parameter-s, it will generate a file in your selected location and the same size as the source file, whether it is a soft link or a hard link, the files are kept synchronized.

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

-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

-V displays detailed processing procedures

Select parameters

-S "-s< tail backup string >" or "--suffix=< tail backup string >"

-V "-V Backup Mode" or "--version-control= Backup Mode"

4. Usage examples:

Example 1: Create a soft link to a file

Command: Ln-s 001.log link001.log

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

Example 2: Create a hard link to a file

Command: LN 001.log ln001.log

Create a hard-link ln001.log for 001.log, the properties of all two files are the same

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

Command:

1. The original file was deleted, and did not affect the hard-link file; The soft-link file flashes continuously under the CentOS system, prompting the original 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 001.log dir

001.log hard links are created in the Dir directory, the 001.log files in the Dir directory are modified, and they are synchronized to the source file-----------as if I didn't test successfully.

Example 5: Creating a soft link to a directory

Command: Ln-sv/opt/soft/test/opt/soft/test1

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 symbolic links too much

3. modifying files in the target directory of the link changes synchronously in the source file directory

    

I just saw a question: When did someone ask when to use a soft link and when to use a hard link?

Soft links are the equivalent of Windows shortcuts, using scenarios:

1. When sharing the same large file in multiple file systems, using soft links avoids creating multiple replicas.

2. When maintaining the version of the dynamic library file, using soft links, after upgrading the library file, you only need to modify the soft link source files, and the program that uses the library does not need to be modified.

A hard link is a pointer to a file index node, and the system does not redistribute the inode for it, and the effect of backing up the data is achieved by using hard links. (actually a backup node)

Both the source file and the hard link are pointing to the same block of disk space.

One Linux command per day (--LN) command

Related Article

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.