Softpanorama
(slightly skeptical) Open Source Software Educational Society

May the source be with you, but remember the KISS principle ;-)

Softpanorama Search

Hard and soft links, ln command and usage of links for simplifying navigation in Unix

News Recommended Links Recommended articles Reference Unix filesystems
Advanced Unix filesystem navigation pushd/popd/dirs NCD clones mv command cp command
Solaris UFS Horror Stories Unix History Humor Etc

Hard and soft links are a great features of Unix. It is a reference (link) in a directory to a file stored in another directory. Links can be of two types: hard and soft.  In case of soft links you can reference not only a file but a directory essentially providing alternative path to the file and subdirectories in it.  The latter is extremely important for simplifying complex filesystem navigation. About usage of symbolic links for filesystem navigation see:

There might be multiple links to a file. Links eliminate redundancy because you do not need to store multiple copies of a file.

Links are of two types: hard and soft (also known as symbolic).

To create a symbolic link, you must use the -s option with the ln command. Files that are soft linked contain an l symbol at the first bit of the access permission bits displayed by the ls -l command, whereas those that are hard linked do not contain the l symbol. A directory is symbolically linked to a file. However, it cannot be hard linked.

It is obvious that no file exists with a link count less than one.  Relative pathnames . or .. are nothing but links for the current directory and its parent directory.  These are present in every directory: any directory stores the two links ., .. and the Inode numbers of the files. They can be listed by the ls -lia option. A directory must have a minimum of two links. The number of links increases as the number of sub-directories increase. Whenever you issue a command to list the file attributes, it refers to the Inode block with the Inode number and the corresponding data is retrieved.

ln command is used to make a link between files. It's format resembles the format of cp command. As we already know a link allows a filename in one directory to point to a file in another directory. Once a link has been made the linkname (alias for a file) can be used, in most circumstances, as if it were a normal file. As a link takes up much less disc space than the file which it is pointing to this it can be used to save space if you need to store duplicate files in many dfferent directories.  It is also useful for simplifying navigation especially in obscure software packages with crazy directory structures like Tivoli. For example you can create a link to the specified target directory from some specialized directory (for example specially created root directory /q -- qucklinks)

 

To create a symbolic link in Unix, at the Unix prompt, enter the following command:

  ln -s source_file myfile

Replace source_file with the name of the existing file for which you want to create the symbolic link (this file can be any existing file or directory across the file systems). Replace myfile with the name of the symbolic link. The ln command then creates the symbolic link. After you've made the symbolic link, you can perform an operation on or execute myfile, just as you could with the source_file. You can use normal file management commands (e.g., cp, rm) on the symbolic link.

Note: If you delete the source file or move it to a different location, your symbolic file will not function properly. You should either delete or move it. If you try to use it for other purposes (e.g., if you try to edit or execute it), the system will send a "file nonexistent" message.

To find out more about symbolic links, you can view the man pages for the ln command. To do this, at the Unix prompt, enter the following command:

  man ln

Mandatory arguments to long options are mandatory for short options too.

This example shows copying three files from a directory into the current working directory.

    [2]%cp ~team/IntroProgs/MoreUltimateAnswer/more*
    [3]%ls -l more*
    -rw-rw-r--   1 mrblobby  mrblobby    632 Sep 21 18:12 moreultimateanswer.adb
    -rw-rw-r--   1 mrblobby  mrblobby   1218 Sep 21 18:19 moreultimatepack.adb
    -rw-rw-r--   1 mrblobby  mrblobby    784 Sep 21 18:16 moreultimatepack.ads

The three files take a total of 2634 bytes. The equivalent ln commands would be:

    [2]%ln -s ~team/IntroProgs/MoreUltimateAnswer/moreultimateanswer.adb .
    [3]%ln -s ~team/IntroProgs/MoreUltimateAnswer/moreultimatepack.adb .
    [4]%ln -s ~team/IntroProgs/MoreUltimateAnswer/moreultimatepack.adb .
    [5]%ls -l
    lrwxrwxrwx   1  mrblobby  mrblobby     35 Sep 22 08:50 moreultimateanswer.adb ->
                     /users/team/IntroProgs/MorUltimateAnswer/moreultimateanswer.adb
    lrwxrwxrwx   1  mrblobby  mrblobby     37 Sep 22 08:49 moreultimatepack.adb ->                       /users/team/IntroProgs/MorUltimateAnswer/moreultimatepack.adb
    lrwxrwxrwx   1   mrblobby  mrblobby    37 Sep 22 08:50 moreultimatepack.ads ->
                     /users/team/IntroProgs/MorUltimateAnswer/moreultimatepack.ads

The ln command now indicates in the first character on the line that this entry is a link. The filename is followed by an arrow ( -> ) and the full path name of the file which it is pointing to. The total size of the three links is 109 bytes. Although mrblooby is shown as owning these files, he only owns the links, and these permissions do not relate to the file itself.

The -s option allows a symbolic link to be made, rather than a phsical link. This allows a link to be made between two file systems. As a symbolic link can be made whenever a non-symbolic link can be made, but a non-symbolic link link cannot always be made when a symbolic link can be made, a symbolic link should always be specified.

The usege of the link command is.

   ln -s ActualFilename LinkFileName

Where -s indicates a symbolic link. ActualFilename is the name of the file which is to be linked to and LinkFileName is the name by which the file should be known.


ln will complain if:
Notes:
  • This is a Spartan WHYFF (We Help You For Free) site written by people for whom English is not a native language. Some amount of grammar and spelling errors should be expected.
  • The site contain some broken links as it develops like a living tree... Please try to use Google, Open directory, etc. to find a replacement link (see HOWTO search the WEB for details). We would appreciate if you can mail us a correct link.
Google Search
Open directory

Research Index

Old News ;-)

[Jul 07, 2007] Easing SUSE Server administration from the command line

How can hard links and symbolic links make administration of SLES 10 easier?

Van Vugt: Hard and symbolic links allow you to put information at the places where you would need it, without losing the option of management in one place. Think of the tmp directory, which is used for temporary files. This directory occurs on many locations in your file system. In general, all those different tmp directories are just links to one single /tmp directory. This allows you to put all temporary files on one centralized location, and to create a special volume for that, which prevents your server from running out of disk space.

Also, links can help you to deal with a program. One example is older graphical utilities that try to locate the X-server in /usr/X11. On a good day, that default location for the X-server changed and nowadays, /usr/X11R6 is used. Imagine that you have such an old application that still tries to locate program files in /usr/X11. It would be able to find those program files if you created a symbolic link with the name X11 that refers to the new X11R6 directory.

docs.sun.com Solaris 10 Release Notes

New ln Utility Requires -f Option

The behavior of /usr/bin/ln has changed to adhere to all of the standards from SVID3 through XCU6. If you use the ln command without the -f option to link to an existing target file, the link is not established. Instead, a diagnostic message is written to standard error, and the command proceeds to link any remaining source files. Finally, the ln command exits with an error value.

For example, if file b exists, the syntax ln a b generates the following message:

ln: b: File exists

This behavior change affects existing shell scripts or programs that include the ln command without the -f option. Scripts that used to work might now fail in Solaris 10 OS.

Workaround: Use the -f option with the ln command. If you have existing scripts that execute the link utility, make sure to modify these scripts to comply with the command's new behavior.

Recommended Links


In case of broken links please try to use Google search. If you find the page please notify us about new location
Google     

Internal

Solaris File System Structure

External

Reference

docs.sun.com Solaris 10 Release Notes New ln Utility Requires -f Option

The behavior of /usr/bin/ln has changed to adhere to all of the standards from SVID3 through XCU6. If you use the ln command without the -f option to link to an existing target file, the link is not established. Instead, a diagnostic message is written to standard error, and the command proceeds to link any remaining source files. Finally, the ln command exits with an error value.

For example, if file b exists, the syntax ln a b generates the following message:

ln: b: File exists

This behavior change affects existing shell scripts or programs that include the ln command without the -f option. Scripts that used to work might now fail in Solaris 10 OS.

Workaround: Use the -f option with the ln command. If you have existing scripts that execute the link utility, make sure to modify these scripts to comply with the command's new behavior.

Solaris 10

Solaris 9 man pages for ln

Recommended Articles

 TTTT Links (see also other tips at  Tuesday Tiny Techie Tips )

ln(1) creates a "link" to a file. To know what that means, you need to know a little bit about how the UNIX filesystem works. (note, I'm doing this off the top of my head without references, so if you want to go write your own UNIX filesystem, you might want to double check my misconceptions).

The UNIX File System

Most UNIX machines store their files on magnetic disk drives. A disk drive is a device that can store information by making electrical imprints on a magnetic surface. One or more heads skim close to the spinning magnetic plate, and can detect, or change, the magnetic state of a given spot on the disk. The drives use disk controllers to position the head at the correct place at the correct time to read from, or write to, the magnetic surface of the plate. It is often possible to partition a single disk drive into more than one logical storage area. This section describes how the UNIX operating system deals with a raw storage device like a disk drive, and how it manages to make organized use of the space.

How the UNIX file system works

Every item in a UNIX file system can de defined as belonging to one of four possible types:

Ordinary files

Ordinary files can contain text, data, or program information. An ordinary file cannot contain another file, or directory. An ordinary file can be thought of as a one-dimensional array of bytes.

Directories

In a previous section, we described directories as containers that can hold files, and other directories. A directory is actually implemented as a file that has one line for each item contained within the directory. Each line in a directory file contains only the name of the item, and a numerical reference to the location of the item. The reference is called an i-number, and is an index to a table known as the i-list. The i-list is a complete list of all the storage space available to the file system.

Special files

Special files represent input/output (i/o) devices, like a tty (terminal), a disk drive, or a printer. Because UNIX treats such devices as files, a degree of compatibility can be achieved between device i/o, and ordinary file i/o, allowing for the more efficient use of software. Special files can be either character special files, that deal with streams of characters, or block special files, that operate on larger blocks of data. Typical block sizes are 512 bytes, 1024 bytes, and 2048 bytes.

Links

A link is a pointer to another file. Remember that a directory is nothing more than a list of the names and i-numbers of files. A directory entry can be a hard link, in which the i-number points directly to another file. A hard link to a file is indistinguishable from the file itself. When a hard link is made, then the i-numbers of two different directory file entries point to the same inode. For that reason, hard links cannot span across file systems. A soft link (or symbolic link) provides an indirect pointer to a file. A soft link is implemented as a directory file entry containing a pathname. Soft links are distinguishable from files, and can span across file systems. Not all versions of UNIX support soft links.

The I-List

When we speak of a UNIX file system, we are actually referring to an area of physical memory represented by a single i-list. A UNIX machine may be connected to several file systems, each with its own i-list. One of those i-lists points to a special storage area, known as the root file system. The root file system contains the files for the operating system itself, and must be available at all times. Other file systems are removable. Removable file systems can be attached, or mounted, to the root file system. Typically, an empty directory is created on the root file system as a mount point, and a removable file system is attached there. When you issue a cd command to access the files and directories of a mounted removable file system, your file operations will be controlled through the i-list of the removable file system.

The purpose of the i-list is to provide the operating system with a map into the memory of some physical storage device. The map is continually being revised, as the files are created and removed, and as they shrink and grow in size. Thus, the mechanism of mapping must be very flexible to accomodate drastic changes in the number and size of files. The i-list is stored in a known location, on the same memory storage device that it maps.

Each entry in an i-list is called an i-node. An i-node is a complex structure that provides the necessary flexibility to track the changing file system. The i-nodes contain the information necessary to get information from the storage device, which typically communicates in fixed-size disk blocks. An i-node contains 10 direct pointers, which point to disk blocks on the storage device. In addition, each i-node also contains one indirect pointer, one double indirect pointer, and one triple indirect pointer. The indirect pointer points to a block of direct pointers. The double indirect pointer points to a block of indirect pointers, and the triple indirect pointer points to a block of double indirect pointers. By structuring the pointers in a geometric fashion, a single i-node can represent a very large file.

It now makes a little more sense to view a UNIX directory as a list of i-numbers, each i-number referencing a specific i-node on a specific i-list. The operating system traces its way through a file path by following the i-nodes until it reaches the direct pointers that contain the actual location of the file on the storage device.

The file system table

Each file system that is mounted on a UNIX machine is accessed through its own block special file. The information on each of the block special files is kept in a system database called the file system table, and is usually located in /etc/fstab. It includes information about the name of the device, the directory name under which it will be mounted, and the read and write privileges for the device. It is possible to mount a file system as "read-only," to prevent users from changing anything.

File system quotas

Although not originally part of the UNIX filesystem, quotas quickly became a widely-used tool. Quotas allow the system administrator to place limits on the amount of space the users can allocate. Quotas usually place restrictions on the amount of space, and the number of files, that a user can take. The limit can be a soft limit, where only a warning is generated, or a hard limit, where no further operations that create files will be allowed.

The command

quota

will let you know if you're over your soft limit. Adding the -v option will provide statistics about your disk usage.

File system related commands

Here are some commands related to file system usage, and other topics discussed in this section:

bdf

On HP-UX systems, reports file system usage statistics

 

df

On HP-UX systems, reports on free disk blocks, and i-nodes

 

du

Summarizes disk usage in a specified directory hierarchy

 

ln

Creates a hard link (default), or a soft link (with -s option)

 

mount, umount

Attaches, or detaches, a file system (super user only)

 

mkfs

Constructs a new file system (super user only)

 

fsck

Evaluates the integrity of a file system (super user only)

A brief tour of the UNIX filesystem

The actual locations and names of certain system configuration files will differ under different inplementations of UNIX. Here are some examples of important files and directories under version 9 of the HP-UX operating system:

/hp-ux

The kernel program

 

/dev/

Where special files are kept

 

/bin/

Executable system utilities, like sh, cp, rm

 

/etc/

System configuration files and databases

 

/lib/

Operating system and programming libraries

 

/tmp/

System scratch files (all users can write here)

 

/lost+found/

Where the file system checker puts detached files

 

/usr/bin/

Additional user commands

 

/usr/include/

Standard system header files

 

/usr/lib/

More programming and system call libraries

 

/usr/local/

Typically a place where local utilities go

 

/usr/man

The manual pages are kept here

Other places to look for useful stuff

If you get an account on an unfamiliar UNIX system, take a tour of the directories listed above, and familiarize yourself with their contents. Another way to find out what is available is to look at the contents of your PATH environment variable:

echo $PATH

You can use the ls command to list the contents of each directory in your path, and the man command to get help on unfamiliar utilities. A good systems administrator will ensure that manual pages are provided for the utilities installed on the system.

 

 


Copyright © 1996-2009 by Dr. Nikolai Bezroukov. www.softpanorama.org was created as a service to the UN Sustainable Development Networking Programme (SDNP) in the author free time. Submit comments This document is an industrial compilation designed and created exclusively for educational use and is placed under the copyright of the Open Content License(OPL). Site uses AdSense so you need to be aware of Google privacy policy. Original materials copyright belong to respective owners. Quotes are made for educational purposes only in compliance with the fair use doctrine.

Disclaimer:

Last modified: February 07, 2010