国际访客建议访问 Primers 编程伙伴 国际版站点 > Bash 教程 > ln 以获得更好的体验。

# Bash 的 ln 命令

ln [OPTION] SRC DEST

!subtitle:功能

创建文件的硬链接或软链接(符号链接)。

!subtitle:类型

可执行文件(/usr/bin/ln),属于 coreutils

!subtitle:参数

  • OPTION 选项:

    • --backup[=CONTROL] - 选择怎样备份被覆盖的目标文件

    • -b - 备份被覆盖的目标文件

    • -d, -F, --directory - 允许超级用户尝试创建目录的硬链接

    • -f, --force - 强制操作,目标存在且无法打开时会尝试将其删除

    • -i, --interactive - 覆盖前提示

    • -L, --logical - 源文件是符号链接时,链接该链接的源文件,而不是该链接自身

    • -n, --no-dereference - 目标是指向目录的符号链接时将其视作普通文件进行覆盖

    • -P, --physical - 源文件是符号链接时,直接创建指向该链接的硬链接

    • -r, --relative - 将源文件的路径转换为参照符号链接所在目录的相对路径(与 -s 一起使用)

    • -s, --symbolic - 创建符号链接,而不是硬链接

    • -S, --suffix=SUFFIX - 使用 SUFFIX 作为备份文件的后缀;默认后缀为 ~

    • -t, --target-directory=DIRECTORY - 指定要创建链接的目录

    • -T, --no-target-directory - 将 DEST 视为文件(如果 DEST 是目录,则进行覆盖而非复制到它内部)

    • -v, --verbose - 打印详细信息

    • --help - 显示帮助

    • --version - 显示版本

  • SRC - 源文件

  • DEST - 目标文件(硬链接)

# 示例

!subtitle:绝对路径

$ ln -s /opt/xxxx/cmd /usr/bin/        # 将 /opt/xxxx/cmd 链接到 /usr/bin/cmd
$ ls -l /usr/bin/cmd
lrwx-wxr-x 1 root root 5 Nov 17 12:12 /usr/bin/cmd -> /opt/xxxx/cmd
  • /usr/bin/cmd 是链接到 /opt/xxxx/cmd 的软连接

!subtitle:相对路径

$ ln -sr ./cmd ./dir        # 将 ./cmd 链接到 ./dir/cmd
$ ls -l ./dir/cmd
lrwx-wxr-x 1 root root 5 Nov 17 12:12 ./dir/cmd -> ../cmd
  • ./dir/cmd 是链接到 ../cmd 的软连接

$ ln -s ./cmd ./dir        # 将 ./cmd 链接到 ./dir/cmd
$ ls -l ./dir/cmd
lrwx-wxr-x 1 root root 5 Nov 17 12:12 ./dir/cmd -> ./cmd
  • ./dir/cmd 是链接到 ./cmd 的软连接

软连接在解析时相对路径参照自身所在目录,因此无法找到源文件,./cmd 指向自身导致无限递归错误。

# 相关命令

命令 说明
link 创建文件的硬链接
readlink 获取符号链接的源文件

# 推荐阅读

# 手册

LN(1)                            User Commands                           LN(1)

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  (note:
              will  probably fail due to system restrictions, even for the su‐
              peruser)

       -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  SIM‐
       PLE_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  con‐
       trols 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  ©  2023  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'

GNU coreutils 9.4                 April 2024                             LN(1)
本文 更新于: 2025-11-27 09:38:15 创建于: 2025-11-27 09:38:15