Unix File Links

When to use a file link

Suppose that you have a file of test data which you want several different programs (or even different users) to use. The programs and/or users reside in different directories. If the programs or users refer to the data file with a short file name such as data1, Unix assumes the file is in the current directory. There is more than one solution, of course.

What is a file link

A file link is a directory entry referring to a file. A hard link looks like the file's original directory entry. A hard link cannot be a directory name. A hard link cannot be created when the directory containing the link and the directory containing the file are in different file systems. (File systems are defined by the system manager. Two file systems may be on different disks, which may be in different buildings. You cannot tell by looking at the path whether something is or is not part of the same file system. To find out, you must use the XXX command.) A symbolic link contains only the name of the file. A symbolic link must be used if the link and the file linked to are in different file systems, or if the object pointed to is a directory. To create a symbolic link, use the -s flag on the command. An ls command which encounters a symbolic link shows

linkname->actualpathname in the file-name column.

Each file knows how many links are pointing to it. If the file is deleted from the "real" directory, it will not actually go away until all the directories which have hard links to it have deleted those links. Using a hard link name, you will still have access to the file. The deleted file will not be available, however, in the directories it is already deleted from, or through a symbolic link, even though the link to the file name is still listed when you do an ls. You cannot use a file in someone else's directory simply because you have set a link to point to it. The file must also be permitted to you.

Examples of creating links

% ln ~/datadir/data1 p1data in current directory, makes a hard link entry p1data pointing to the file data1 in the datadir directory of the home account.
% ln -s /users/cscprof/studata/test1 testdata1 in current directory, makes a symbolic link testdata1 to a data file on a professor's account.
% ln -s /users/cscprof/studata testdata in current directory, makes a symbolic link testdata to the entire studata directory. After this command, testdata/test1 in the current directory is equivalent to /users/cscprof/studata/test1
% ln /uses/yourdir/file2 mydir in the directory mydir (a subdirectory of the current directory), makes a hard link file2 to /users/yourdir/file2. now ./mydir/file2 = /users/yourdir/file2
% ln /users/yourdir/file2 ~/mydir in your home directory, the subdirectory mydir now has a hard link file2 to /users/yourdir/file2. ~/mydir/file2 = /users/yourdir/ file2

Creating and finding out about links In your directory In a C program

The stat and lstat functions need the following

#include
#include
char *path;
struct stat buf;

stat(hardlinkpath, buf) information as seen in your directory is put in buf.
stat(symlinkpath, buf) information about the file linked to is put in buf.
lstat(symlinkpath, buf) information on the link itself is put in buf.

The readlink command needs:

int cc, bufsiz:
char *path, * buf;

cc = readlink(linkpath, buf, bufsiz) read contents of a symbolic link.

The symlink command needs:

char *named *name2;
symlink(name1,name2); creates a symbolic link name1 to the file name2.


Previous Page
Index
Next Page