Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The ln command of linux uses detailed explanation of parameters.

2025-04-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

The main content of this article is "detailed explanation of the parameters of linux's ln command". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn the "linux ln command to use parameters detailed explanation" bar!

This is a very important command in linux, please be familiar with it. Its function is to establish a different link for a file in another location. The most commonly used parameter of this command is-s. The specific usage is: ln-s source file target file.

When we need to use the same file in a different directory, we don't need to put the same file in every directory we need, we just put the file in a fixed directory, and then link it with the ln command (link) in another directory, without repeatedly occupying disk space.

For example: ln-s / bin/less / usr/local/bin/less

-s means code name (symbolic).

There are two points to note here: first, the ln command keeps the synchronization of every linked file, that is, 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 ln-s *, it will only generate a file image in the location of your choice, will not take up disk space, hard link ln *, no parameter-s, it will generate a file of the same size in your selected location and the source file, whether soft link or hard link, the file will keep synchronized changes.

If you look at a directory with ls and find that some files are followed by an @ symbol, that is a file generated with the ln command. Use the ls-l command to view it, and you can see the path of the displayed link.

Ln is another very important command in linux. Its function is to establish a synchronous link for a file in another location. When we need to use the same file in a different directory, we don't need to put the same file in every directory we need, we just put the file in a fixed directory, and then link it with the ln command (link) in another directory, without repeatedly occupying disk space.

1. Command format:

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

2. Command function:

Linux file system, there are so-called link, we can regard it as an alias for the file, and links can be divided into two types: hard link (hard link) and soft link (symbolic link), hard link means that an file can have multiple names, while the soft link way is to generate a special file, the content of the file is to point to the location of another file. Hard links exist in the same file system, while 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

two。 Soft links can cross file systems, but hard links cannot

3. A soft link can link to a file name that does not exist.

4. Soft links can link directories

Hard links:

1. Hard links, in the form of a copy of the file. But it doesn't take up real space.

two。 Hard links to directories are not allowed

3. Hard links can only be created in the same file system

Here are two points to pay attention to:

First, the ln command keeps the synchronization of every linked file, that is, no matter where you change, the other files will change the same.

Second, ln links are divided into soft links and hard links, soft links are ln-s source file target files, it will only generate an image of a file in the location you choose, will not take up disk space, hard link ln source file target files, no parameter-s, it will generate a file of the same size as the source file in your selected location, whether soft link or hard link The files are kept synchronized.

The ln instruction is used to link files or directories, just as if you specify more than two files or directories, and the final destination is an existing directory, all previously specified files or directories will be copied to that directory. If you specify more than one file or directory at the same time, and the final destination is not an existing directory, an error message appears.

3. Command parameters:

Required parameters:

-b delete, overwrite previously established links

-d allows superusers to make hard links to directories

-f Enforcement

-I interactive mode, the presence of a file prompts the user whether to overwrite

-n treat symbolic links as general directories

-s soft links (symbolic links)

-v shows the detailed process

Select parameters:

-S "- S" or "--suffix="

-V "- V" or "--version-control="

-- help displays help information

-- version displays version information

4. Use an example:

Example 1: create a soft link to a file

Command:

Ln-s log2013.log link2013

Output:

The code is as follows:

[root@localhost test] # ll

-rw-r--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 root 11 12-07 16:01 link2013-> log2013.log

-rw-r--r-- 1 root bin 61 11-13 06:03 log2013.log

Description:

Create a soft link link2013 for the log2013.log file. If the log2013.log is lost, the link2013 will fail.

Example 2: create a hard link to a file

Command:

Ln log2013.log ln2013

Output:

The code is as follows:

[root@localhost test] # ll

Lrwxrwxrwx 1 root root 11 12-07 16:01 link2013-> log2013.log

-rw-r--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 root 11 12-07 16:01 link2013-> log2013.log

-rw-r--r-- 2 root bin 61 11-13 06:03 ln2013

-rw-r--r-- 2 root bin 61 11-13 06:03 log2013.log

Description:

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

Example 3: connect the above two instances, and delete and rebuild the linked original file after the link

Command:

Output:

The code is as follows:

[root@localhost test] # ll

Lrwxrwxrwx 1 root root 11 12-07 16:01 link2013-> log2013.log

-rw-r--r-- 2 root bin 61 11-13 06:03 ln2013

-rw-r--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 root 11 12-07 16:01 link2013-> log2013.log

-rw-r--r-- 1 root bin 61 11-13 06:03 ln2013

[root@localhost test] # touch log2013.log

[root@localhost test] # ll

Lrwxrwxrwx 1 root root 11 12-07 16:01 link2013-> log2013.log

-rw-r--r-- 1 root bin 61 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

[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 root 11 12-07 16:01 link2013-> log2013.log

-rw-r--r-- 1 root bin 61 11-13 06:03 ln2013

-rw-r--r-- 1 root 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

Description:

1. After the source file is deleted, the hard link file is not affected; the soft link file flashes constantly under the centos system, indicating that the source file no longer exists

two。 After rebuilding the source file, the soft link does not flash, indicating that the link has been successful and the linked file system has been found; after reconstruction, the hard link file is not affected by the source file, and the content of the hard link file still retains the content of the source file before deletion, indicating that the hard link has been invalidated

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

Command:

Ln log2013.log test3

Output:

The code is as follows:

[root@localhost test] # ln log2013.log test3

[root@localhost test] # ll

Lrwxrwxrwx 1 root root 11 12-07 16:01 link2013-> log2013.log

-rw-r--r-- 1 root bin 61 11-13 06:03 ln2013

-rw-r--r-- 2 root root 96 12-07 16:21 log2013.log

[root@localhost test] # cd test3

[root@localhost test3] # ll

-rw-r--r-- 2 root 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--r-- 2 root root 80 12-07 16:36 log2013.log

[root@localhost test3] # cd..

[root@localhost test] # ll

Lrwxrwxrwx 1 root root 11 12-07 16:01 link2013-> log2013.log

-rw-r--r-- 1 root bin 61 11-13 06:03 ln2013

-rw-r--r-- 2 root root 80 12-07 16:36 log2013.log

[root@localhost test] #

Description:

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

Example 5: create a soft link to a directory

Command:

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

Output:

The code is as follows:

[root@localhost 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

[root@localhost test] # cd test5

[root@localhost test5] # ll

Lrwxrwxrwx 1 root root 5 12-07 16:57 test3-> test3

[root@localhost test5] # cd test3

-bash: cd: test3: too many layers of symbolic connection

[root@localhost test5] #

[root@localhost test5] #

[root@localhost test5] # ll

Lrwxrwxrwx 1 root 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 a symbolic link "/ opt/soft/test/test5/test3" pointing to "/ opt/soft/test/test3"

[root@localhost test5] # ll

Lrwxrwxrwx 1 root 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--r-- 2 root root 80 12-07 16:36 log2013.log

[root@localhost test3] # touch log2014.log

[root@localhost test3] # ll

Total 4

-rw-r--r-- 2 root root 80 12-07 16:36 log2013.log

-rw-r--r-- 1 root root 0 12-07 17:05 log2014.log

[root@localhost test3] # cd..

[root@localhost test5] # cd..

Description:

1. Directories can only create soft links

two。 Directory creation link must use absolute path, relative path creation will not be successful, will prompt: too many layers of symbolic links such an error

3. Modifying files in the linked destination directory will synchronize changes in the source file directory.

There are two kinds of links, one is called Hard Link, the other is called symbolic link (Symbolic Link). When you establish a hard link, the linked file and the linked file must be on the same file system, and a hard link to a directory cannot be established. For symbolic links, this problem does not exist. By default, ln produces hard links.

Ln command

Use

Link the file.

Grammar

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 to a file in another directory specified in the TargetDirectory parameter. By default, the ln command creates hard links. If you need to use the ln command to create a symbolic link, indicate the-s flag.

A symbolic link is an indirect pointer to a file; its directory entry contains the file name to which it is linked. Symbolic links may span the file system and may point to directories.

If you are linking a file to a new name, only one file can be listed. If you link to a directory, you can list multiple files.

The TargetFile parameter is optional. If you do not specify a target file, 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 you do not use the-s flag, you cannot link files between file systems.

If TargetDirectory is already a symbolic link to a directory, the ln command treats the existing target as a file. This means that a command like ln-fs somepath/lname symdir does not follow the existing symdir symbolic link and instead creates a new symbolic link from somepath/lname to symdir.

Mark

-f causes the ln command to replace any existing destination paths. If the destination path already exists and the-f flag is not specified, the ln command does not create a new link, but writes a diagnostic message to standard error and continues to link the rest of the SourceFiles.

-n specifies that if the link is an existing file, then do not overwrite the contents of the file. The-f flag resets this flag. This is the default behavior.

-s causes the ln command to create symbolic links. The symbolic link contains the name of the file it links to. When you open a link, the reference file is used. The stat call to the symbolic link returns the linked target file; the lstat call must be completed to get the information about the link. You can use readlink calls to read the contents of symbolic links. Symbolic links may point to directories across the file system.

Note: when you specify the SourceFile parameter for the-s flag, you must use an absolute path. If the absolute path is not specified, unexpected results may occur when the SourceFile and TargetFile parameters are in different directories. The source file does not need to exist before you create a symbolic link.

Exit statu

This command returns the following exit values:

All specified files have been successfully linked.

An error occurred in > 0.

Example

Zero. To create another link (alias) to a file, enter:

Ln-f chap1 intro

This links chap1 to the new name, intro. If intro does not exist, the file name is created. If intro already exists, the file will be replaced with a link to chap1. Then the chap1 and intro filenames point to the same file. Changes to any 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 under another name.

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

Ln index manual

This links index to the new name, manual/index.

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

To link several files to names in another directory, enter:

Ln chap2 jim/chap3 / home/manual

This links chap2 to the new name / home/manual/chap2; and jim/chap3 to the new name / home/manual/chap3.

3. If you want to use pattern matching characters in the ln command, enter:

N manual/*.

This links all files in the manual directory to the current directory,. (click), give them the same name in the manual directory.

Note: you must enter a space between the asterisk and the period.

To create a symbolic link, type:

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

If you want to get the same result without specifying the TargetFile parameter, enter:

Ln-s / tmp/toc

At this point, I believe you have a deeper understanding of the "detailed explanation of the parameters used by the ln command of linux". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report