DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH PRINT BOOK
 
About files and directories

Creating links to files and directories

You may need to make a file accessible from more than one directory, and by more than one user, but still keep it as a single file. This is often the case when you need to share the data in a file with your colleagues. To prevent different versions of the file from proliferating, only one copy of the file exists, but links are created that allow you and your colleagues to access the file from your home directories or another convenient location.


NOTE: There is the danger of a file becoming corrupted if more than one person tries to edit it at the same time.

Creating a link to a file

To create a link to a file, use the ln(1) command, as follows:

ln filename linkname

For example, suppose you have a file called user_guide which is located in /u/workgrp/tasks/projects. To work on this file you would normally cd to that directory before opening the file. However, by creating a link to the file, you can access it from your current directory (without needing to enter the full path of the file). To do this, type the following:

   $ ln /u/workgrp/tasks/projects/user_guide my_guide
The my_guide argument identifies the link. Whenever you want to work on the file, which is now known to the system by two names (user_guide and my_guide), you can access it from the current directory by using my_guide as the filename.

You must have write permission on a directory before you can create a link that involves that directory or a file in that directory. You cannot create a hard link (the kind of link described above) to a directory or a file on a different filesystem. To create a link to a directory or a different filesystem, you must use a symbolic link. See ``Creating a link to a directory'' for details.

Links can be removed using rm. If a file has several links, it is not physically deleted until the final link is removed.

Finding out whether a file has hard links

To find out how many hard links there are to a file, use the ls -l command, as follows:

   $ ls -l
   -rw-r--r--   1 johnd   unixdoc    10586 Feb 25 12:26 1.start
   -rw-rw-r--   2 johnd   techpubs   61339 Feb 24 14:45 2.scosh
   -rw-rw-r--   1 johnd   techpubs   14741 Feb 25 11:18 3.dire
   -rw-rw-r--   3 johnd   techpubs   40419 Feb 25 15:57 4.files
You need to locate all the links to a file or directory if you want to delete it: as long as there are links, you cannot delete the file or directory.

In a long listing, the number of links are shown in the second column from the left (after the sets of permissions). For example, in the above example, the file 4.files has three links to it.

You can find out where the common links to a single file are located, in two steps. First, you need to identify the inode number of the file (see ``How the system manages files and directories'' for information on inodes). To do this, use the ls -i option, as follows:

   $ ls -i
   20350 basking_shark      3886 cod        2002 halibut
    3526 herring            10182 narwhal
The number before the filename is the file's inode number, for example, 2002 for the file called halibut.

To trace all the links to this file, you must find all the other files in the filesystem with this inode number. You can do this using the find -inum option, as follows:

   $ find / -inum 2002 -print
   /u/dave/tmp/halibut
   /u/charles/fish6
   /u/michael/project2/ichthyo14
In this case, there are three files with the same inode number. In order to delete this file from the filesystem, it would be necessary to run rm on all the links.

Alternatively, you could use an other form of the find command to remove the files directly:

$ find / -inum 2002 -exec rm {} \;

For more information see find(1).

Creating a link to a directory

It is often useful to change to another directory without typing its full pathname: symbolic links provide a useful shortcut to do this. A symbolic link differs from a hard link. It is a small file that contains a reference (by name) to a directory or file that already exists. Unlike normal links, symbolic links can cross filesystems and link to directories. (They are used extensively by the system.) Also unlike normal links, symbolic links are separate files; they cease to work if the file they point to is deleted or renamed, or if they are moved.

Many of the files found in /bin, /lib, and /usr are actually symbolic links that point to files (of the same name) stored below /var/opt. The directories these files are located in are called ``storage sections''. Storage sections are used because they make it easier to install system upgrades. Software subsystems (such as UUCP) consist of many files, which may be installed in several directories. However, all the files in a subsystem belong to a single storage section. By overwriting the contents of the (single) storage section directory, all the files in the subsystem can be updated simultaneously.

Symbolic links are identified in a directory listing by a ``->'', as follows:

   $ l
   drw-r--r--  1 johnd  unixdoc     29  Feb 27 15:56 mydata -> /u/work
   group/tasks/project/01
You can obtain a directory listing without symbolic links visible in it by specifying the -L (logical) option. This makes ls (or l, or any related program) list the directory, replacing the information about each symbolic link with the details for the file pointed to by the link:
   $ l -L
   drw-r--r--  1 johnd  unixdoc   10297  Feb 27 15:56 mydata
To create a symbolic link, use the ln -s option, as follows:
   ln -s directory symbolic_link
For example, suppose you work in /u/workgrp/tasks/projects and your home directory is /u/me. Your normal command to work on a file would be the following:
   $ cd /u/workgrp/tasks/projects
To reduce the typing required, enter the following command:
   $ ln -s /u/workgrp/tasks/projects mydata
This command creates a symbolic link called mydata in your current directory. From now on, mydata and /u/workgrp/tasks/projects refer to the same location, and you can relocate to /u/workgrp/tasks/projects by typing cd mydata instead of typing in the full pathname.

You must have write permission on a directory before you can create a link that involves that directory or a file in that directory.

See also ``Access control for files and directories''.

If you remove a symbolic link, only the link itself is removed. If you remove (or move) the directory or file to which the link points, the link will be left pointing to nothing.

Navigating symbolic links

Because the system uses symbolic links extensively, you may encounter problems in identifying your current working directory. For example, suppose you create a symbolic link to a directory, then change directory using the new link, as follows:

   $ ln -s /u/workgrp/tasks/projects mydata
   $ pwd
   /u/people/mike
   $ cd mydata
   $ pwd
   /u/people/mike/mydata
   $
In this example, you create a symbolic link called mydata, pointing to /u/workgrp/tasks/projects. However, if you change directory via the link mydata, you actually see yourself as being in /u/people/mike/mydata. This is the logical present working directory; that is, the path the user traversed to reach the directory. The directory also has a physical path, that is, the actual pathname of the current directory, relative to the top of the filesystem.

Now you are in /u/workgrp/tasks/projects. Suppose you create another symbolic link and change directory into it:

   $ ln -s /u/people people
   $ pwd
   /u/people/mike/mydata
   $ cd people
   $ pwd
   /u/people/mike/mydata/people
   $
When you change directory into people, you are following a link to /u/people. This is higher in the directory tree than your original starting point, but because you are traversing another symbolic link, your logical present working directory is another level down the tree.

If you then type cd .. (to go up a directory), where you end up depends on your shell. If you are running the Korn shell, the cd .. command goes up a level in your logical directory path: /u/people/mike/mydata becomes your present working directory. If you are running any other shell, the cd .. command goes up a level in your physical directory path: /u becomes the present working directory.

Despite the apparent complexity, it is possible to determine your physical working directory in a directory tree populated with symbolic links. There are two techniques, for Korn shell users and for others:


Korn shell users
Use the command pwd -P to identify your absolute current working directory. (The command pwd is built into the Korn shell. It normally returns the logical present working directory; the -P option makes pwd return the physical path to the current directory.

others
Use the command pwd(1). The non-Korn shells do not include a built-in pwd command. The external pwd command returns the physical path to the current directory by default.
In general, directory traversal commands built into the Korn shell accept two options, -P and -L. -P makes the command refer to the physical working directory, while -L makes the command refer to the logical working directory (that is, to the path taken through any symbolic links).

The equivalent commands, in any other shell, always apply to the physical working directory. . . . . . mount=no fsckflags=-y . .


© 2004 The SCO Group, Inc. All rights reserved.
UnixWare 7 Release 7.1.4 - 22 April 2004