回到手册索引

命令用 ln(link)命令用于在 Linux 系统中创建文件或目录的链接,分为硬链接和符号链接(软链接),常用于共享文件或为文件创建快捷方式

常用用法示例

  1. 创建硬链

    1
    ln file1.txt file1_hard

    硬链接与源文件共享相同的 inode,删除源文件不影响硬链接的访问

  2. 创建符号链接(软链接)

    1
    ln -s file1.txt file1_soft

    符号链接独立于源文件,删除源文件会导致软链接失效

  3. 创建目录的符号链

    1
    ln -s /var/log logs

    通过 ls -l 可查看链接指向:lrwxrwxrwx 1 user … logs -> /var/log

  4. 强制覆盖现有链接

    1
    ln -sf file2.txt file1_soft

    -f 参数用于覆盖已存在的链接文件

  5. 批量创建链接到指定目录

    1
    ln -s /path/to/files/*.conf /etc/

    适用于批量设置配置文件链接

  6. 显示链接创建过程的详细信息

    1
    2
    ln -sv file1.txt link_file
    'link_file' -> 'file1.txt'

    -v 参数会输出操作详情

  7. 避免误操作覆盖文件(交互模式)

    1
    2
    ln -si file1.txt existing_link
    ln: replace 'existing_link'? y

    -i 参数在覆盖前询问用户确认

  8. 创建相对路径的符号链接

    1
    ln -sr ../dir1/file.txt ./link_file

    -r 参数使链接路径为相对路径而非绝对路径

常用参数选项

  • -s, –symbolic
    创建符号链接而非硬链接
  • -f, –force
    强制覆盖已存在的目标文件
  • -i, –interactive
    覆盖前询问用户确认
  • -v, –verbose
    显示操作详细信息
  • -n, –no-dereference
    若目标是符号链接,将其视为普通文件而非引用其指向的内容
  • -r, –relative
    创建基于当前目录的相对路径符号链接
  • -b, –backup
    覆盖文件前创建备份(备份文件名默认添~ 后缀)
  • –backup=[控制参数]
    自定义备份方式(–backup=numbered 生成带数字的备份)

原厂文档

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
NAME

ln - make links between files

SYNOPSIS

ln [OPTION]... [-T] TARGET LINK_NAME
ln [OPTION]... TARGET
ln [OPTION]... TARGET... DIRECTORY
ln [OPTION]... -t DIRECTORY TARGET...

DESCRIPTION

In the 1st form, create a link to TARGET with the name LINK_NAME.
In the 2nd form, create a link to TARGET in the current directory.
In the 3rd and 4th forms, create links to each TARGET in
DIRECTORY. Create hard links by default, symbolic links with
--symbolic. By default, each destination (name of new link)
should not already exist. When creating hard links, each TARGET
must exist. Symbolic links can hold arbitrary text; if later
resolved, a relative link is interpreted in relation to its parent
directory.

Mandatory arguments to long options are mandatory for short
options too.

--backup[=CONTROL]
make a backup of each existing destination file

-b like --backup but does not accept an argument

-d, -F, --directory
allow the superuser to attempt to hard link directories
(this will probably fail due to system restrictions, even
for the superuser)

-f, --force
remove existing destination files

-i, --interactive
prompt whether to remove destinations

-L, --logical
dereference TARGETs that are symbolic links

-n, --no-dereference
treat LINK_NAME as a normal file if it is a symbolic link
to a directory

-P, --physical
make hard links directly to symbolic links

-r, --relative
with -s, create links relative to link location

-s, --symbolic
make symbolic links instead of hard links

-S, --suffix=SUFFIX
override the usual backup suffix

-t, --target-directory=DIRECTORY
specify the DIRECTORY in which to create the links

-T, --no-target-directory
treat LINK_NAME as a normal file always

-v, --verbose
print name of each linked file

--help display this help and exit

--version
output version information and exit

The backup suffix is '~', unless set with --suffix or
SIMPLE_BACKUP_SUFFIX. The version control method may be selected
via the --backup option or through the VERSION_CONTROL environment
variable. Here are the values:

none, off
never make backups (even if --backup is given)

numbered, t
make numbered backups

existing, nil
numbered if numbered backups exist, simple otherwise

simple, never
always make simple backups

Using -s ignores -L and -P. Otherwise, the last option specified
controls behavior when a TARGET is a symbolic link, defaulting to
-P.

AUTHOR

Written by Mike Parker and David MacKenzie.

REPORTING BUGS

GNU coreutils online help:
<https://www.gnu.org/software/coreutils/>
Report any translation bugs to
<https://translationproject.org/team/>

COPYRIGHT

Copyright © 2025 Free Software Foundation, Inc. License GPLv3+:
GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

SEE ALSO

link(2), symlink(2)

Full documentation <https://www.gnu.org/software/coreutils/ln>
or available locally via: info '(coreutils) ln invocation'

COLOPHON

This page is part of the coreutils (basic file, shell and text
manipulation utilities) project. Information about the project
can be found at ⟨http://www.gnu.org/software/coreutils/ If you
have a bug report for this manual page, see
⟨http://www.gnu.org/software/coreutils/ This page was obtained
from the tarball coreutils-9.6.tar.xz fetched from
⟨http://ftp.gnu.org/gnu/coreutils/on 2024-02-02. If you
discover any rendering problems in this HTML version of the page,
or you believe there is a better or more up-to-date source for the
page, or you have corrections or improvements to the information
in this COLOPHON (which is not part of the original manual page),
send a mail to man-pages@man7.org