user225643
user225643

Reputation: 3701

Is it possible to create relative symlinks from a script?

This question follows on the heals of this one: Symlinks not working when link is made in another directory?

Say I have a directory containing a project, and a script that creates symbolic links from one part of the project directory to another part. Is it possible for those symbolic links to be relative (not absolute), even if the location of the script is somewhere else entirely?

I'm interested in doing this so that the project directory can be moved without breaking absolute links inside it.

Upvotes: 3

Views: 2496

Answers (2)

Christopher Neylan
Christopher Neylan

Reputation: 8292

EDIT:

@MДΓΓБДLL points out correctly that what you want to do is built into the functionality of ln. The first argument to ln will be the target value used when the link is created, wherever it is created. For example:

$ cd /
$ ln -s .. ~/testln
$ cd ~
$ ls -la testln
lrwxrwxrwx 1 xxxxx xxxxx 2 Dec 22 09:29 testln -> ..

Upvotes: 1

user3850
user3850

Reputation:

There is nothing special about running commands from a script. You just do it.

$ mkdir originals
$ echo "weird" > originals/original.txt
$ mkdir copies
$ ln -s ../originals/original.txt copies/copy.txt
$ cat copies/copy.txt
weird

Note that you don't have to change the working directory before creating relative links. The symlink will be relative to the link, not relative to the current directory.

Upvotes: 3

Related Questions