Redirecting Symbolic Links to Directories
Suppose you are developing a website for a Linux system and you want to release it in various stages to the users. One of the ways to do this is to have a separate subdirectory for each version and to use a symbolic link (symlink) to point to the latest one. However, redirecting the symbolic link can be a bit tricky and my first attempts ended up with the link sitting inside one of the directories, instead of at the top level. The secret is to use the -n option to suppress dereferencing. Here is a simplified example illustrating how I got it to work properly:
$ mkdir dir1
$ ln -sfn dir1 latest
$ ls -l
total 4
drwxr-xr-x 2 tristram tristram 4096 2010-04-01 22:13 dir1
lrwxrwxrwx 1 tristram tristram 4 2010-04-01 22:13 latest -> dir1
$ mkdir dir2
$ ln -sfn dir2 latest
$ ls -l
total 4
drwxr-xr-x 2 tristram tristram 4096 2010-04-01 22:13 dir1
drwxr-xr-x 2 tristram tristram 4096 2010-04-01 22:13 dir2
lrwxrwxrwx 1 tristram tristram 4 2010-04-01 22:13 latest -> dir2
$
The -s and -f options select symbolic (as opposed to hard) links and force overwriting of the existing link. For more details see the Gnu Coreutils documentation for the ln command.
Reader Comments