Alias Physical Files

By Danny Kalev

Associating more than one name to the same physical file is a useful

feature. For instance, you may need to refer to the same physical file

from different directories. This week I will explain how to create file

links using the ln command.

The ln command defines a new name - commonly called a link - for an

existing file. In the following example, the user defines a new link

called "january" for the file reports01_01:

$ln reports01_01 january

The ln command takes two parameters, the first being the original

filenames and the second the new associated name. If you list the

current directory's files, you will see a new item added -- the link

january:

$ls

reports01_01 january

Remember though, only one physical file resides in the directory.

Adding a link doesn't create a new file, it merely creates an alias.

It's possible to associate more than one link per file. The following

example illustrates associating a new link with the file reports01_01:

$ln reports01_01 inventory

As expected, the ls command now shows three different files in the

current directory, two of which are the previously defined links:

$ls

reports01_01 january inventory

To find out the number of links associated with a file, type the ls

command with the -l option. This option provides additional information

about a file such as permissions, the number of associated links, the

owner, the size, and modification date. However, this option still

doesn't reveal the names linked to the same file. To see the associated

links, use the -i option. This option displays the file's inode - a

unique identification number Linux assigns to each physical file. If

two filenames have the same inode number, then they are linked to the

same physical file. For example:

$ls -i january inventory

4577 january 4577 inventory

To delete a file, you have to remove its entire associated links. So

long as one associated link remains, the file isn't deleted; even if

the original filename has been removed, the physical file remains

intact. In the following example, the user employs the rm command to

remove the file reports01_01. However, since this file has two other

associated links, it still can be accessed through them:

$rm reports01_01

$cat january

bulbs 12

mice 56

...

About the author(s)

-------------------

Danny Kalev is a system analyst and software engineer with more than 10

years of experience, specializing in C++ and object-oriented analysis

and design on various platforms including VMS, DOS, Windows, Unix, and

Linux. His technical interests involve code optimization, networking,

and distributed computing. He is also a member of the ANSI C++

standardization committee and the author of ANSI/ISO C++ Professional

Programmer's Handbook (Que, 1999). Contact him at linuxnl@excite.com.

1