5

From coreutils' manual, for ln

ln makes links between files. By default, it makes hard links; with the -s option, it makes symbolic (or soft) links. Synopses:

ln [option]... [-T] target linkname
ln [option]... target
ln [option]... target... directory
ln [option]... -t directory target...

• If two file names are given, ln creates a link to the first file from the second.

• If one target is given, ln creates a link to that file in the current directory.

If the --target-directory (-t) option is given, or failing that if the last file is a directory and the --no-target-directory (-T) option is not given, ln creates a link to each target file in the specified directory, using the targets’ names.

What does the part in bold mean?

in particular what do the followings mean

  • "failing that if the last file is a directory"
  • "using the targets’ names"?

Thanks.

Tim
  • 101,790
  • 1
    It means that if you to ln /path/to/files/* /path/to/some/directory/ or ln -t /path/to/some/directory/ /path/to/files/*, a link to each of the files matching /path/to/files/*will be created in /path/to/some/directory/ with identical names to the originals. – DopeGhoti Jul 02 '16 at 23:15
  • @DopeGhoti: You should make that an answer. – Julie Pelletier Jul 02 '16 at 23:34

2 Answers2

8

It means that if you to ln /path/to/files/* /path/to/some/directory/ or ln -t /path/to/some/directory/ /path/to/files/*, a link to each of the files matching /path/to/files/* will be created in /path/to/some/directory/ with identical names to the originals.

DopeGhoti
  • 76,081
  • thanks. what does 'failing' mean? – Tim Jul 03 '16 at 00:44
  • http://ell.stackexchange.com/a/95379 – Tim Jul 03 '16 at 01:27
  • 2
    Essentially it's 'else'. If the -t option is given, or failing that if... means If the -t option is given, else if... – David Conrad Jul 03 '16 at 01:37
  • 1
    As a word of warning, related to the use of relative path names, you can't go to '/path/to/files' and type 'ln * /path/to/some/directory/' b/c that will just give you a self loop in the target directory of every filename to itself. – Motorhead Feb 10 '17 at 07:29
6

I think http://linux.die.net/man/1/ln

makes it clearer. You have these forms:

  1. ln [OPTION]... [-T] TARGET LINK_NAME (1st form)
  2. ln [OPTION]... TARGET (2nd form)
  3. ln [OPTION]... TARGET... DIRECTORY (3rd form)
  4. ln [OPTION]... -t DIRECTORY TARGET... (4th form)

and

-t, --target-directory=DIRECTORY
specify the DIRECTORY in which to create the links
-T, --no-target-directory
treat LINK_NAME as a normal file

-T says that if you pass two arguments, you absolutely mean form 1 (second arg is the link_name), not 3 -- in other words, you want to make sure you haven't accidentally passed a directory as the second argument.

Example:

mkdir output
ln -s /etc/passwd output #OK passwd link goes into output/passwd

vs

mkdir output
ln -sT /etc/passwd output #FAIL; output is already taken and is a directory

Link -t, is the opposite of -T. It expects whatever comes after it to be a directory to put links into, which allows you to do things like:

list_link_targets | xargs -d '\n' ln -s -t directory

which would be much more challenging to do with the 3rd form.

Petr Skocik
  • 28,816
  • thanks. regarding your example before the last, http://unix.stackexchange.com/questions/293521/does-ln-sf-overwrites-existing-files-which-are-only-symbolic-links – Tim Jul 03 '16 at 00:58