4

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.

2
  • 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 2, 2016 at 23:15
  • @DopeGhoti: You should make that an answer. Jul 2, 2016 at 23:34

2 Answers 2

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.

4
  • thanks. what does 'failing' mean?
    – Tim
    Jul 3, 2016 at 0:44
  • ell.stackexchange.com/a/95379
    – Tim
    Jul 3, 2016 at 1: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... Jul 3, 2016 at 1: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.
    – N.S.
    Feb 10, 2017 at 7:29
5

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.

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.