multiple links with single ln command

Paul Chvostek paul+fbsd at it.ca
Wed Jun 28 00:07:19 UTC 2006


Hiya.

On Tue, Jun 27, 2006 at 02:14:22PM -0400, sara lidgey wrote:
> 
> I've read the man page for ln but can't find a way to do this.  I want to create multiple links to a single directory with one command.  Consider the following example.  I have a directory structure like this:
> test/a/
> test/b/
> test/c/
>  I want to create a symbolic link called "clink" in test/a/ and test/b/ which points to test/c/
> 
> The only way I know to do this is with two commands:
> ln -s test/c test/a/clink
> ln -s test/c test/b/clink
> 
> Can it be done with a single command?

No.  Well, it depends on what you consider a single command.  :)

Consider that your command line uses fileglob expansion to determine the
full command line *before* the command is run.  The notation you're
looking at is this:

	ln [-fhinsv] source_file ... target_dir

which means the `ln` command takes a left-hand-side (the source,
possibly multiple sources) and a right-hand-side (the target).

But you're asking to go the other way around, with a single source being
created in multiple targets.  If you have ALOT of these links to make,
or need to do this on an regular basis, I suggest making a small script
that does what you want; perhaps something like this:

  #!/bin/sh
  if [ $# -lt 2 ]; then
    echo "Usage: altln source_file target_dir ...
    exit 1
  fi
  source_file="$1"; shift
  for target_dir in $*; do
    ln -svi "$source_file" "$target_dir"
  done

... which you can run with a command line like:

# ls -F test/
a/      b/      c/
# altln ../c test/a test/b
# ls -l test/*/*
lrwxr-xr-x  1 root  wheel  4 Jun 27 17:28 test/a/c -> ../c
lrwxr-xr-x  1 root  wheel  4 Jun 27 17:28 test/b/c -> ../c
#

(Bear in mind that the symbolic link you create will be evaluated
relative to ITS location, not your cwd when you create the link.)

-- 
  Paul Chvostek                                             <paul at it.ca>
  it.canada                                            http://www.it.ca/



More information about the freebsd-questions mailing list