xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

Linux shell command ln All In One

Linux shell command ln All In One

硬连接 vs 软连接

hard link
symbolic link

https://www.gnu.org/software/coreutils/ln

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.

在第一种形式中,使用名称 LINK_NAME 创建指向 TARGET 的链接。
在第二种形式中,在当前目录中创建指向 TARGET 的链接。
在第 3 和第 4 种形式中,创建指向 DIRECTORY 中每个 TARGET 的链接。
默认创建硬链接,符号链接使用 --symbolic
默认情况下,每个目的地(新链接的名称)不应该已经存在。
创建硬链接时,每个 TARGET 都必须存在。
符号链接可以包含任意文本;
如果稍后解析,相对链接将根据其父目录进行解释。

多头期权的强制参数对于空头期权也是强制性的。

image

ln

Debian / Ubuntu / Raspberry Pi

$ man ln > ./man-docs/ln.md
$ cat ./man-docs/ln.md

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

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 TAR‐
       GET in the current directory.  In the 3rd and 4th forms, create links to each TARGET in DIRECTORY.   Cre‐
       ate  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 direc‐
       tory.

       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 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
              create symbolic 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  ©  2020  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 8.32                               September 2020                                            LN(1)


macOS

$ man ln
LN(1)                       General Commands Manual                      LN(1)

NAME
     ln, link – link files

SYNOPSIS
     ln [-L | -P | -s [-F]] [-f | -iw] [-hnv] source_file [target_file]
     ln [-L | -P | -s [-F]] [-f | -iw] [-hnv] source_file ... target_dir
     link source_file target_file

DESCRIPTION
     The ln utility creates a new directory entry (linked file) for the file
     name specified by target_file.  The target_file will be created with the
     same file modes as the source_file.  It is useful for maintaining
     multiple copies of a file in many places at once without using up storage
     for the “copies”; instead, a link “points” to the original copy.  There
     are two types of links; hard links and symbolic links.  How a link
     “points” to a file is one of the differences between a hard and symbolic
     link.

     The options are as follows:

     -F    If the target file already exists and is a directory, then remove
           it so that the link may occur.  The -F option should be used with
           either -f or -i options.  If neither -f nor -i is specified, -f is
           implied.  The -F option is a no-op unless -s is specified.

     -L    When creating a hard link to a symbolic link, create a hard link to
           the target of the symbolic link.  This is the default.  This option
           cancels the -P option.

     -P    When creating a hard link to a symbolic link, create a hard link to
           the symbolic link itself.  This option cancels the -L option.

     -f    If the target file already exists, then unlink it so that the link
           may occur.  (The -f option overrides any previous -i and -w
           options.)

     -h    If the target_file or target_dir is a symbolic link, do not follow
           it.  This is most useful with the -f option, to replace a symlink
           which may point to a directory.

     -i    Cause ln to write a prompt to standard error if the target file
           exists.  If the response from the standard input begins with the
           character ‘y’ or ‘Y’, then unlink the target file so that the link
           may occur.  Otherwise, do not attempt the link.  (The -i option
           overrides any previous -f options.)

     -n    Same as -h, for compatibility with other ln implementations.

     -s    Create a symbolic link.

     -v    Cause ln to be verbose, showing files as they are processed.

     -w    Warn if the source of a symbolic link does not currently exist.

     By default, ln makes hard links.  A hard link to a file is
     indistinguishable from the original directory entry; any changes to a
     file are effectively independent of the name used to reference the file.
     Directories may not be hardlinked, and hard links may not span file
     systems.

     A symbolic link contains the name of the file to which it is linked.  The
     referenced file is used when an open(2) operation is performed on the
     link.  A stat(2) on a symbolic link will return the linked-to file; an
     lstat(2) must be done to obtain information about the link.  The
     readlink(2) call may be used to read the contents of a symbolic link.
     Symbolic links may span file systems and may refer to directories.

     Given one or two arguments, ln creates a link to an existing file
     source_file.  If target_file is given, the link has that name;
     target_file may also be a directory in which to place the link; otherwise
     it is placed in the current directory.  If only the directory is
     specified, the link will be made to the last component of source_file.

     Given more than two arguments, ln makes links in target_dir to all the
     named source files.  The links made will have the same name as the files
     being linked to.

     When the utility is called as link, exactly two arguments must be
     supplied, neither of which may specify a directory.  No options may be
     supplied in this simple mode of operation, which performs a link(2)
     operation using the two passed arguments.

EXAMPLES
     Create a symbolic link named /home/src and point it to /usr/src:

           # ln -s /usr/src /home/src

     Hard link /usr/local/bin/fooprog to file /usr/local/bin/fooprog-1.0:

           # ln /usr/local/bin/fooprog-1.0 /usr/local/bin/fooprog

     As an exercise, try the following commands:

           # ls -i /bin/[
           11553 /bin/[
           # ls -i /bin/test
           11553 /bin/test

     Note that both files have the same inode; that is, /bin/[ is essentially
     an alias for the test(1) command.  This hard link exists so test(1) may
     be invoked from shell scripts, for example, using the if [ ] construct.

     In the next example, the second call to ln removes the original foo and
     creates a replacement pointing to baz:

           # mkdir bar baz
           # ln -s bar foo
           # ln -shf baz foo

     Without the -h option, this would instead leave foo pointing to bar and
     inside foo create a new symlink baz pointing to itself.  This results
     from directory-walking.

     An easy rule to remember is that the argument order for ln is the same as
     for cp(1): The first argument needs to exist, the second one is created.

COMPATIBILITY
     The -h, -i, -n, -v and -w options are non-standard and their use in
     scripts is not recommended.  They are provided solely for compatibility
     with other ln implementations.

     The -F option is a FreeBSD extension and should not be used in portable
     scripts.

SEE ALSO
     link(2), lstat(2), readlink(2), stat(2), symlink(2), symlink(7)

STANDARDS
     The ln utility conforms to IEEE Std 1003.2-1992 (“POSIX.2”).

     The simplified link command conforms to Version 2 of the Single UNIX
     Specification (“SUSv2”).

HISTORY
     An ln command appeared in Version 1 AT&T UNIX.

macOS 13.4                       May 10, 2021                       macOS 13.4

demos

(🐞 反爬虫测试!打击盗版⚠️)如果你看到这个信息, 说明这是一篇剽窃的文章,请访问 https://www.cnblogs.com/xgqfrms/ 查看原创文章!

refs

image

https://www.imooc.com/video/7948



©xgqfrms 2012-2021

www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!

原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!


posted @ 2023-06-04 21:27  xgqfrms  阅读(24)  评论(0编辑  收藏  举报