Symbolic Links


Table of Contents

symbolic links 
cmd:lndir 
cmd:ln 
removing/finding broken links? 
removing/finding broken links? 
Hard link can have different names: 
Deal with multi-level of links to a shell script 
Symptom 
Reason 
Solution 
Creating a hard link to a directory…. 
how to find a users *true* home directory with a shell script? 
how to find a users *true* home directory with a shell script? 
How to change timestamp on symbolic link? 
How to change timestamp on symbolic link? 

symbolic links 

cmd:lndir 

SYNOPSIS

lndir [ -silent ] [ -ignorelinks ] [ -withrevinfo ] fromdir [ todir ]

Caution: as opposed to cp, lndir will not create a dir, ie, all dir/files from the 'fromdir' will be created under pwd unless the 'todir' is specified.

Also, lndir will not create the 'todir', you have to create it first if it does not exist.

cmd:ln 

Usage 

file 
ln -s index.htm index.html
dir 
ln -s ~/ndl/libwww ~/install/libwww

— works fine for this case, but not for www well, it depends on how apache is configured. <<:2000.08.03 Thu:>>

link certain files to other dir 
iitrc:~/ndl/libwww$ ls *me | xargs1 ln {} ~/www/tools/libwww

— has to do this way, for files out side of www, hard link *N*: use hard link IFF for www!

link certain files from other dir 
ls ~+1/* | xargs -n 1 ln -s
ls ~+1/* | doeach ln -s @_
[Note]

'doeach.pl ln -s ./@_ ~+1/' doesn't work! ln just takes whatever from the command line and create the file as is. No hidden processing.

link a bunch of files from other dirs current one, relative 
cd /opt/bin
ln -s ../pkg1/bin/* .
make same type of link across different dirs, relative 
Obj: All sub dirs that contains file ++index.html,
    create a relative sym link to it named index.html
ff . ++index.html | sed 's|/++|/|'  | xargs1 ln -sf ++index.html
$ dir ./news/e21/usCommentary/2001/0329/index.html
 ./news/e21/usCommentary/2001/0329/index.html -> ++index.html
[Note]

file ++index.html need not exist in current dir!

link a bunch of files from one dir to another!, abs 
ln -s /usr/local/teTeX/bin/sparc-solaris/* /usr/local/bin
ln -s /usr/local/teTeX/man/man1/* /usr/local/man/man1
ln -s /usr/local/teTeX/man/man5/* /usr/local/man/man5
ln -s /usr/local/teTeX/info/*info* /usr/local/info
link a bunch of files from one dir to another!, relative 
ln -s some_dir/* .
[Note]

link from somewhere else to current dir:

/usr/shared# ln -s teTeX/bin/tie bin
/usr/shared# dir bin/tie
lrwxrwxrwx   1 root     other          13 Aug  3 18:12 bin/tie -> teTeX/bin/tie

— nono

/usr/shared/bin# ln -s ../teTeX/bin/tie .
/usr/shared/bin# dir tie
lrwxrwxrwx   1 root     other          16 Aug  3 18:14 tie -> ../teTeX/bin/tie*
/usr/shared/bin# ln -s ../teTeX/bin/* .

Test 

1<<, >>
host:~/www>ln index.htm index.html
host:~/www>dir
-rw-r--r--   2 034897s cstudent      174 Feb 17 14:56 index.htm
-rw-r--r--   2 034897s cstudent      174 Feb 17 14:56 index.html

— two "same" files, with "link count" of 2

2<<, >>
host:~/www>ln -s index.htm index.html
ln: index.html: File exists
host:~/www>rm index.html
host:~/www>dir
-rw-r--r--   1 034897s cstudent      174 Feb 17 14:56 index.htm

— index.htm still remains after rm index.html, "link count" down to 1

3<<, >>
host:~/www>ln -s index.htm index.html
host:~/www>dir
-rw-r--r--   1 034897s cstudent      174 Feb 17 14:56 index.htm
lrwxrwxrwx   1 034897s cstudent        9 Feb 17 15:03 index.html -> index.htm

— two files, one is symbolic links, "link count" are still 1

4<<, >>
host:~/ndl>ln get-1.5.3.tar.gz ~/www/tools
host:~/ndl>dir ~/www/tools
total 463
-rw-r--r--   2 034897s cstudent   446966 Feb 17 20:53 get-1.5.3.tar.gz

*N*: can't do a symbolic link this time, web server refuse to get files out of www dir. For links from web to home file only, have to use hard links!

help 

NAME

ln - make links between files

SYNOPSIS

ln [options] source [dest]
ln [options] source... directory
It  is  an error  if the last argument is not a directory and more than
two files are given.  It makes hard links  by  default.   By default, it
does not remove existing files.
-s, --symbolic
     Make symbolic links instead of hard links.  This option
     produces  an  error message on systems that do not sup-
     port symbolic links.