xref: /freebsd/bin/ln/ln.1 (revision 90aea514c6249118e880d75972d063362f4bf492)
19ddb49cbSWarner Losh.\"-
24b88c807SRodney W. Grimes.\" Copyright (c) 1980, 1990, 1993
34b88c807SRodney W. Grimes.\"	The Regents of the University of California.  All rights reserved.
44b88c807SRodney W. Grimes.\"
54b88c807SRodney W. Grimes.\" This code is derived from software contributed to Berkeley by
64b88c807SRodney W. Grimes.\" the Institute of Electrical and Electronics Engineers, Inc.
74b88c807SRodney W. Grimes.\"
84b88c807SRodney W. Grimes.\" Redistribution and use in source and binary forms, with or without
94b88c807SRodney W. Grimes.\" modification, are permitted provided that the following conditions
104b88c807SRodney W. Grimes.\" are met:
114b88c807SRodney W. Grimes.\" 1. Redistributions of source code must retain the above copyright
124b88c807SRodney W. Grimes.\"    notice, this list of conditions and the following disclaimer.
134b88c807SRodney W. Grimes.\" 2. Redistributions in binary form must reproduce the above copyright
144b88c807SRodney W. Grimes.\"    notice, this list of conditions and the following disclaimer in the
154b88c807SRodney W. Grimes.\"    documentation and/or other materials provided with the distribution.
16fbbd9655SWarner Losh.\" 3. Neither the name of the University nor the names of its contributors
174b88c807SRodney W. Grimes.\"    may be used to endorse or promote products derived from this software
184b88c807SRodney W. Grimes.\"    without specific prior written permission.
194b88c807SRodney W. Grimes.\"
204b88c807SRodney W. Grimes.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
214b88c807SRodney W. Grimes.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
224b88c807SRodney W. Grimes.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
234b88c807SRodney W. Grimes.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
244b88c807SRodney W. Grimes.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
254b88c807SRodney W. Grimes.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
264b88c807SRodney W. Grimes.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
274b88c807SRodney W. Grimes.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
284b88c807SRodney W. Grimes.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
294b88c807SRodney W. Grimes.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
304b88c807SRodney W. Grimes.\" SUCH DAMAGE.
314b88c807SRodney W. Grimes.\"
32*b01fe519SEnji Cooper.Dd June 12, 2017
334b88c807SRodney W. Grimes.Dt LN 1
34caa2db3bSRuslan Ermilov.Os
354b88c807SRodney W. Grimes.Sh NAME
36d71e172aSSheldon Hearn.Nm ln ,
37d71e172aSSheldon Hearn.Nm link
388b9e2a36STom Rhodes.Nd link files
394b88c807SRodney W. Grimes.Sh SYNOPSIS
40be8b1497SRuslan Ermilov.Nm
4180a0e9b5SJilles Tjoelker.Op Fl L | Fl P | Fl s Op Fl F
423595f21aSGiorgos Keramidas.Op Fl f | iw
4380906944SRuslan Ermilov.Op Fl hnv
444b88c807SRodney W. Grimes.Ar source_file
459c24ed85STim J. Robbins.Op Ar target_file
46be8b1497SRuslan Ermilov.Nm
4780a0e9b5SJilles Tjoelker.Op Fl L | Fl P | Fl s Op Fl F
483595f21aSGiorgos Keramidas.Op Fl f | iw
4980906944SRuslan Ermilov.Op Fl hnv
504b88c807SRodney W. Grimes.Ar source_file ...
519c24ed85STim J. Robbins.Ar target_dir
52d71e172aSSheldon Hearn.Nm link
53d71e172aSSheldon Hearn.Ar source_file Ar target_file
544b88c807SRodney W. Grimes.Sh DESCRIPTION
554b88c807SRodney W. GrimesThe
562a170595SPhilippe Charnier.Nm
578b9e2a36STom Rhodesutility creates a new directory entry (linked file) for the file name
588b9e2a36STom Rhodesspecified by
598b9e2a36STom Rhodes.Ar target_file .
608b9e2a36STom RhodesThe
618b9e2a36STom Rhodes.Ar target_file
628b9e2a36STom Rhodeswill be created with the same file modes as the
638b9e2a36STom Rhodes.Ar source_file .
644b88c807SRodney W. GrimesIt is useful for maintaining multiple copies of a file in many places
654b88c807SRodney W. Grimesat once without using up storage for the
664b88c807SRodney W. Grimes.Dq copies ;
674b88c807SRodney W. Grimesinstead, a link
684b88c807SRodney W. Grimes.Dq points
694b88c807SRodney W. Grimesto the original copy.
704b88c807SRodney W. GrimesThere are two types of links; hard links and symbolic links.
714b88c807SRodney W. GrimesHow a link
724b88c807SRodney W. Grimes.Dq points
73790b5553SKris Kennawayto a file is one of the differences between a hard and symbolic link.
744b88c807SRodney W. Grimes.Pp
754b88c807SRodney W. GrimesThe options are as follows:
764b88c807SRodney W. Grimes.Bl -tag -width flag
775e09de40SGleb Smirnoff.It Fl F
785e09de40SGleb SmirnoffIf the target file already exists and is a directory, then remove it
795e09de40SGleb Smirnoffso that the link may occur.
805e09de40SGleb SmirnoffThe
815e09de40SGleb Smirnoff.Fl F
825e09de40SGleb Smirnoffoption should be used with either
835e09de40SGleb Smirnoff.Fl f
845e09de40SGleb Smirnoffor
855e09de40SGleb Smirnoff.Fl i
865e09de40SGleb Smirnoffoptions.
87*b01fe519SEnji CooperIf neither
88*b01fe519SEnji Cooper.Fl f
89*b01fe519SEnji Coopernor
90*b01fe519SEnji Cooper.Fl i
91*b01fe519SEnji Cooperis specified,
925e09de40SGleb Smirnoff.Fl f
935e09de40SGleb Smirnoffis implied.
945e09de40SGleb SmirnoffThe
955e09de40SGleb Smirnoff.Fl F
965e09de40SGleb Smirnoffoption is a no-op unless
975e09de40SGleb Smirnoff.Fl s
98*b01fe519SEnji Cooperis specified.
9980a0e9b5SJilles Tjoelker.It Fl L
10080a0e9b5SJilles TjoelkerWhen creating a hard link to a symbolic link,
10180a0e9b5SJilles Tjoelkercreate a hard link to the target of the symbolic link.
10280a0e9b5SJilles TjoelkerThis is the default.
10380a0e9b5SJilles TjoelkerThis option cancels the
10480a0e9b5SJilles Tjoelker.Fl P
10580a0e9b5SJilles Tjoelkeroption.
10680a0e9b5SJilles Tjoelker.It Fl P
10780a0e9b5SJilles TjoelkerWhen creating a hard link to a symbolic link,
10880a0e9b5SJilles Tjoelkercreate a hard link to the symbolic link itself.
10980a0e9b5SJilles TjoelkerThis option cancels the
11080a0e9b5SJilles Tjoelker.Fl L
11180a0e9b5SJilles Tjoelkeroption.
11280a0e9b5SJilles Tjoelker.It Fl f
11380a0e9b5SJilles TjoelkerIf the target file already exists,
11480a0e9b5SJilles Tjoelkerthen unlink it so that the link may occur.
11580a0e9b5SJilles Tjoelker(The
11680a0e9b5SJilles Tjoelker.Fl f
11780a0e9b5SJilles Tjoelkeroption overrides any previous
11880a0e9b5SJilles Tjoelker.Fl i
11980a0e9b5SJilles Tjoelkerand
12080a0e9b5SJilles Tjoelker.Fl w
12180a0e9b5SJilles Tjoelkeroptions.)
122c9e7c667SMaxim Sobolev.It Fl h
123c9e7c667SMaxim SobolevIf the
124c9e7c667SMaxim Sobolev.Ar target_file
125c9e7c667SMaxim Sobolevor
126c9e7c667SMaxim Sobolev.Ar target_dir
127eccea571SRuslan Ermilovis a symbolic link, do not follow it.
128eccea571SRuslan ErmilovThis is most useful with the
129c9e7c667SMaxim Sobolev.Fl f
130c9e7c667SMaxim Sobolevoption, to replace a symlink which may point to a directory.
1317fcd5ae0SSheldon Hearn.It Fl i
1328ba8e2efSSheldon HearnCause
1338ba8e2efSSheldon Hearn.Nm
1348ba8e2efSSheldon Hearnto write a prompt to standard error if the target file exists.
1358ba8e2efSSheldon HearnIf the response from the standard input begins with the character
1368ba8e2efSSheldon Hearn.Sq Li y
1378ba8e2efSSheldon Hearnor
1388ba8e2efSSheldon Hearn.Sq Li Y ,
1398ba8e2efSSheldon Hearnthen unlink the target file so that the link may occur.
1408ba8e2efSSheldon HearnOtherwise, do not attempt the link.
1418ba8e2efSSheldon Hearn(The
1427fcd5ae0SSheldon Hearn.Fl i
1437fcd5ae0SSheldon Hearnoption overrides any previous
1447fcd5ae0SSheldon Hearn.Fl f
1458ba8e2efSSheldon Hearnoptions.)
146c9e7c667SMaxim Sobolev.It Fl n
147c9e7c667SMaxim SobolevSame as
148c9e7c667SMaxim Sobolev.Fl h ,
149c9e7c667SMaxim Sobolevfor compatibility with other
150c9e7c667SMaxim Sobolev.Nm
151c9e7c667SMaxim Sobolevimplementations.
1524b88c807SRodney W. Grimes.It Fl s
1534b88c807SRodney W. GrimesCreate a symbolic link.
154b598b073SDavid E. O'Brien.It Fl v
155b598b073SDavid E. O'BrienCause
156b598b073SDavid E. O'Brien.Nm
157b598b073SDavid E. O'Briento be verbose, showing files as they are processed.
1583595f21aSGiorgos Keramidas.It Fl w
1593595f21aSGiorgos KeramidasWarn if the source of a symbolic link does not currently exist.
1604b88c807SRodney W. Grimes.El
1614b88c807SRodney W. Grimes.Pp
162790b5553SKris KennawayBy default,
1632a170595SPhilippe Charnier.Nm
1644b88c807SRodney W. Grimesmakes
1654b88c807SRodney W. Grimes.Em hard
1664b88c807SRodney W. Grimeslinks.
1674b88c807SRodney W. GrimesA hard link to a file is indistinguishable from the original directory entry;
168790b5553SKris Kennawayany changes to a file are effectively independent of the name used to reference
1694b88c807SRodney W. Grimesthe file.
1708b9e2a36STom RhodesDirectories may not be hardlinked, and hard links may not span file systems.
1714b88c807SRodney W. Grimes.Pp
1724b88c807SRodney W. GrimesA symbolic link contains the name of the file to
173eccea571SRuslan Ermilovwhich it is linked.
174eccea571SRuslan ErmilovThe referenced file is used when an
1754b88c807SRodney W. Grimes.Xr open 2
1764b88c807SRodney W. Grimesoperation is performed on the link.
1774b88c807SRodney W. GrimesA
1784b88c807SRodney W. Grimes.Xr stat 2
1794b88c807SRodney W. Grimeson a symbolic link will return the linked-to file; an
1804b88c807SRodney W. Grimes.Xr lstat 2
1814b88c807SRodney W. Grimesmust be done to obtain information about the link.
1824b88c807SRodney W. GrimesThe
1834b88c807SRodney W. Grimes.Xr readlink 2
1844b88c807SRodney W. Grimescall may be used to read the contents of a symbolic link.
1854b88c807SRodney W. GrimesSymbolic links may span file systems and may refer to directories.
1864b88c807SRodney W. Grimes.Pp
1874b88c807SRodney W. GrimesGiven one or two arguments,
1882a170595SPhilippe Charnier.Nm
1894b88c807SRodney W. Grimescreates a link to an existing file
1904b88c807SRodney W. Grimes.Ar source_file .
1914b88c807SRodney W. GrimesIf
1924b88c807SRodney W. Grimes.Ar target_file
1934b88c807SRodney W. Grimesis given, the link has that name;
1944b88c807SRodney W. Grimes.Ar target_file
1954b88c807SRodney W. Grimesmay also be a directory in which to place the link;
1964b88c807SRodney W. Grimesotherwise it is placed in the current directory.
1974b88c807SRodney W. GrimesIf only the directory is specified, the link will be made
1984b88c807SRodney W. Grimesto the last component of
1994b88c807SRodney W. Grimes.Ar source_file .
2004b88c807SRodney W. Grimes.Pp
2014b88c807SRodney W. GrimesGiven more than two arguments,
2022a170595SPhilippe Charnier.Nm
2034b88c807SRodney W. Grimesmakes links in
2044b88c807SRodney W. Grimes.Ar target_dir
2054b88c807SRodney W. Grimesto all the named source files.
2064b88c807SRodney W. GrimesThe links made will have the same name as the files being linked to.
207d71e172aSSheldon Hearn.Pp
208d71e172aSSheldon HearnWhen the utility is called as
209d71e172aSSheldon Hearn.Nm link ,
210d71e172aSSheldon Hearnexactly two arguments must be supplied,
211d71e172aSSheldon Hearnneither of which may specify a directory.
212d71e172aSSheldon HearnNo options may be supplied in this simple mode of operation,
2139a73cc73SSheldon Hearnwhich performs a
214d71e172aSSheldon Hearn.Xr link 2
215d71e172aSSheldon Hearnoperation using the two passed arguments.
2168850c7d7SJoel Dahl.Sh EXAMPLES
2178850c7d7SJoel DahlCreate a symbolic link named
2188850c7d7SJoel Dahl.Pa /home/src
2198850c7d7SJoel Dahland point it to
2208850c7d7SJoel Dahl.Pa /usr/src :
2218850c7d7SJoel Dahl.Pp
2228850c7d7SJoel Dahl.Dl # ln -s /usr/src /home/src
2238850c7d7SJoel Dahl.Pp
2248850c7d7SJoel DahlHard link
2258850c7d7SJoel Dahl.Pa /usr/local/bin/fooprog
2268850c7d7SJoel Dahlto file
2278850c7d7SJoel Dahl.Pa /usr/local/bin/fooprog-1.0 :
2288850c7d7SJoel Dahl.Pp
2298850c7d7SJoel Dahl.Dl # ln /usr/local/bin/fooprog-1.0 /usr/local/bin/fooprog
2308850c7d7SJoel Dahl.Pp
2318850c7d7SJoel DahlAs an exercise, try the following commands:
2328850c7d7SJoel Dahl.Bd -literal -offset indent
2338850c7d7SJoel Dahl# ls -i /bin/[
2348850c7d7SJoel Dahl11553 /bin/[
2358850c7d7SJoel Dahl# ls -i /bin/test
2368850c7d7SJoel Dahl11553 /bin/test
2378850c7d7SJoel Dahl.Ed
2388850c7d7SJoel Dahl.Pp
2398850c7d7SJoel DahlNote that both files have the same inode; that is,
2408850c7d7SJoel Dahl.Pa /bin/[
2418850c7d7SJoel Dahlis essentially an alias for the
2428850c7d7SJoel Dahl.Xr test 1
2438850c7d7SJoel Dahlcommand.
2448850c7d7SJoel DahlThis hard link exists so
2458850c7d7SJoel Dahl.Xr test 1
2468850c7d7SJoel Dahlmay be invoked from shell scripts, for example, using the
2478850c7d7SJoel Dahl.Li "if [ ]"
2488850c7d7SJoel Dahlconstruct.
2498850c7d7SJoel Dahl.Pp
2508850c7d7SJoel DahlIn the next example, the second call to
2518850c7d7SJoel Dahl.Nm
2528850c7d7SJoel Dahlremoves the original
2538850c7d7SJoel Dahl.Pa foo
2548850c7d7SJoel Dahland creates a replacement pointing to
2558850c7d7SJoel Dahl.Pa baz :
2568850c7d7SJoel Dahl.Bd -literal -offset indent
2578850c7d7SJoel Dahl# mkdir bar baz
2588850c7d7SJoel Dahl# ln -s bar foo
2598850c7d7SJoel Dahl# ln -shf baz foo
2608850c7d7SJoel Dahl.Ed
2618850c7d7SJoel Dahl.Pp
2628850c7d7SJoel DahlWithout the
2638850c7d7SJoel Dahl.Fl h
2648850c7d7SJoel Dahloption, this would instead leave
2658850c7d7SJoel Dahl.Pa foo
2668850c7d7SJoel Dahlpointing to
2678850c7d7SJoel Dahl.Pa bar
2688850c7d7SJoel Dahland inside
2698850c7d7SJoel Dahl.Pa foo
2708850c7d7SJoel Dahlcreate a new symlink
2718850c7d7SJoel Dahl.Pa baz
2728850c7d7SJoel Dahlpointing to itself.
2738850c7d7SJoel DahlThis results from directory-walking.
2748850c7d7SJoel Dahl.Pp
2758850c7d7SJoel DahlAn easy rule to remember is that the argument order for
2768850c7d7SJoel Dahl.Nm
2778850c7d7SJoel Dahlis the same as for
2788850c7d7SJoel Dahl.Xr cp 1 :
2798850c7d7SJoel DahlThe first argument needs to exist, the second one is created.
280b598b073SDavid E. O'Brien.Sh COMPATIBILITY
281b598b073SDavid E. O'BrienThe
2821d7f44efSMaxim Sobolev.Fl h ,
2831d7f44efSMaxim Sobolev.Fl i ,
2843595f21aSGiorgos Keramidas.Fl n ,
2858ba8e2efSSheldon Hearn.Fl v
2863595f21aSGiorgos Keramidasand
2873595f21aSGiorgos Keramidas.Fl w
2887fcd5ae0SSheldon Hearnoptions are non-standard and their use in scripts is not recommended.
2891d7f44efSMaxim SobolevThey are provided solely for compatibility with other
2901d7f44efSMaxim Sobolev.Nm
29141f6d284SRuslan Ermilovimplementations.
2925e09de40SGleb Smirnoff.Pp
2935e09de40SGleb SmirnoffThe
2945e09de40SGleb Smirnoff.Fl F
295955e0a6bSGlen Barberoption is a
2965e09de40SGleb Smirnoff.Fx
297a53bb70bSJoel Dahlextension and should not be used in portable scripts.
2986fca4c7cSRuslan Ermilov.Sh SEE ALSO
2996fca4c7cSRuslan Ermilov.Xr link 2 ,
3006fca4c7cSRuslan Ermilov.Xr lstat 2 ,
3016fca4c7cSRuslan Ermilov.Xr readlink 2 ,
3026fca4c7cSRuslan Ermilov.Xr stat 2 ,
3036fca4c7cSRuslan Ermilov.Xr symlink 2 ,
3046fca4c7cSRuslan Ermilov.Xr symlink 7
305c9e7c667SMaxim Sobolev.Sh STANDARDS
306c9e7c667SMaxim SobolevThe
307c9e7c667SMaxim Sobolev.Nm
308c9e7c667SMaxim Sobolevutility conforms to
309ad2eaa7eSTim J. Robbins.St -p1003.2-92 .
310c9e7c667SMaxim Sobolev.Pp
311c9e7c667SMaxim SobolevThe simplified
312c9e7c667SMaxim Sobolev.Nm link
313c9e7c667SMaxim Sobolevcommand conforms to
314c9e7c667SMaxim Sobolev.St -susv2 .
3154b88c807SRodney W. Grimes.Sh HISTORY
316790b5553SKris KennawayAn
3172a170595SPhilippe Charnier.Nm
3184b88c807SRodney W. Grimescommand appeared in
319b8923d4cSWolfram Schneider.At v1 .
320