[ Next Article | Previous Article | Book Contents | Library Home | Legal | Search ]
System User's Guide: Operating System and Devices

Linking Files and Directories

Links are connections between a file name and an index node reference number (i-node), the internal representation of a file. Because directory entries contain file names paired with i-nodes, every directory entry is a link. The i-node number actually identifies the file, not the file name. By using links, any i-node or file can be known by many different names.

For example, i-node 798 contains a memo regarding June sales in the Omaha office. Presently, the directory entry for this memo is:

i-node Number File Name
798 memo

Because this information relates to information stored in the sales and omaha directories, linking is used to share the information where it is needed. Using the ln command, links are created to these directories. Now the file has three file names:

i-node Number File Name
798 memo
798 sales/june
798 omaha/junesales

When you use the pg or cat command to view the contents of any of the three file names, the same information is displayed. If you edit the contents of the i-node from any of the three file names, the contents of the data displayed by all of the file names will reflect any changes.

This section discusses:

Types of Links

Links are created with the ln command. There are two kinds of links:

hard link Allows access to the data of a file from a new file name. Hard links ensure the existence of a file. When the last hard link is removed, the i-node and its data are deleted. Hard links can be created only between files that are in the same file system.
symbolic link Allows access to data in other file systems from a new file name. The symbolic link is a special type of file that contains a path name. When a process encounters a symbolic link, the process may search that path. Symbolic links do not protect a file from deletion from the file system.
Note: The user who creates a file retains ownership of that file no matter how many links are created. Only the owner of the file or the root user can set the access mode for that file. However, changes can be made to the file from a linked file name with the proper access mode.

A file or directory exists as long as there is one hard link to the i-node for that file. In the long listing displayed by the ls -l command, the number of links to each file and subdirectory is given. All hard links are treated equally by the operating system regardless of which link was created first.

Linking Files (ln Command)

Linking files with the ln command is a convenient way to work with the same data in more than one place. Links are created by giving alternate names to the original file. The use of links allows a large file, such as a database or mailing list, to be shared by several users without making copies of that file. Not only do links save disk space, but changes made to one file are automatically reflected in all the linked files.

The ln command links the file designated in the SourceFile parameter to the file designated by the TargetFile parameter or to the same file name in another directory specified by the TargetDirectory parameter. By default, the ln command creates hard links. To use the ln command to create symbolic links, designate the -s flag.

If you are linking a file to a new name, you can list only one file. If you are linking to a directory, you can list more than one file.

The TargetFile parameter is optional. If you do not designate a target file, the ln command creates a new file in your current directory. The new file inherits the name of the file designated in the SourceFile parameter.

Note: You cannot link files across file systems without using the -s flag.

For example, to create another link to a file named chap1, enter:

ln -f chap1 intro

This links chap1 to the new name, intro. When the -f flag is used, the file name intro is created if it does not already exist. If intro does exist, the file is replaced by a link to chap1. Then both the chap1 and intro file names will refer to the same file. Any changes made to one also appear in the other.

For example, to link a file named index to the same name in another directory named manual, enter:

ln index manual

This links index to the new name, manual/index.

For example, to link several files to names in another directory, enter:

ln chap2 jim/chap3 /home/manual

This links chap2 to the new name /home/manual/chap2 and jim/chap3 to /home/manual/chap3.

For example, to use the ln command with pattern-matching characters, enter:

ln manual/* .

This links all files in the manual directory into the current directory, . (dot), giving them the same names they have in the manual directory.

Note: You must type a space between the asterisk and the period.

For example, to create a symbolic link, enter:

ln -s /tmp/toc toc

This creates the symbolic link, toc, in the current directory. The toc file points to the /tmp/toc file. If the /tmp/toc file exists, the cat toc command lists its contents.

To achieve identical results without designating the TargetFile parameter, enter:

ln -s /tmp/toc

See the ln command in the AIX Version 4.3 Commands Reference for the exact syntax.

Removing Linked Files

The rm or del command removes the link from the file name you indicate. When one of several hard-linked file names is deleted, the file is not completely deleted since it remains under the other name. When the last link to an i-node is removed, the data is removed as well. The i-node number is then available for reuse by the system.

See the del and rm commands in the AIX Version 4.3 Commands Reference for the exact syntax.

Related Information

Commands Overview

Processes Overview

Input and Output Redirection Overview

Shells Overview

File Systems Overview

Directory Overview

Files Overview

DOS Files

Printers, Print Jobs, and Queues

Backup Files and Storage Media

File and System Security


[ Next Article | Previous Article | Book Contents | Library Home | Legal | Search ]