Create and view symbolic or hard links

Author: Fred Crowson openbsd@crowsons.net OpenBSD

Reviewer: name contact BSD flavour

Reviewer: name contact BSD flavour


Concept

Know the difference between symbolic and hard links as well as how to create, view and remove both types of links. In addition, be able to temporarily resolve a low disk space issue using a symbolic link.

Introduction

A link allows several filenames to refer to a single file on disk. There are two types of links: hard links (((links!hard))) and symbolic links(((links!symbolic))). A hard link associates two or more filenames with the same inode. Thus hard links allow different directory entries to refer to the same disk data blocks.

Symbolic, or soft links as they are sometimes known as, are pointer files that name another file elsewhere in the file system. As symbolic links point to another pathname in the filesystem they can span physical devices, as they point to a pathname not actual disk location of the file.

Both hard and symbolic links are created with the ln(((ln))) command.

Changes made to either the hard linked file or the original will effect both of them since they share the same disk data blocks. ls(((ls))) marks symbolic links with an 'l'. Using the '-i' option to ls will list the inodes of each file which will identify the hard linked files:

""20912 -rw-r--r-- 2 fred wheel 24 Jan 31 14:00 hardlink ""20912 -rw-r--r-- 2 fred wheel 24 Jan 31 14:00 index ""20914 lrwxr-xr-x 1 fred wheel 5 Jan 31 13:58 softlink -> index

rm(((rm))) will remove both hard and symbolic links, however if the symbolic link is deleted it does not affect the file referenced by the link. Similarly, in the above example deleting index will not affect hardlink even though it is the same file.

The stat(1)(((stat))) utility displays file status. Using stat -F filename will highlight symbolic links with a trailing '@' and show the file to which it is linked:

""$ stat -F softlink
""lrwxr-xr-x 1 fred wheel 5 Jan 31 13:58:17 2007 softlink@ -> index

The power of stat lies in its ability to format the output, as illustrated in the example taken from the man page: ""$ stat -f "%N: %HT%SY" *
""hardlink: Regular File ""index: Regular File ""softlink: Symbolic Link -> index

Examples

Create an entry in the current directory called hardlink with the same inode as index:

""$ ln index hardlink ""$ ls -la ""total 8 ""drwxr-xr-x 2 fred wheel 512 Jan 31 12:33 . ""drwxr-xr-x 29 fred wheel 2048 Jan 31 12:33 .. ""-rw-r--r-- 2 fred wheel 0 Jan 31 12:33 hardlink ""-rw-r--r-- 2 fred wheel 0 Jan 31 12:33 index

Create a symbolic link to index in the current directory:

""$ ln -s index softlink ""$ ls -la ""total 8 ""drwxr-xr-x 2 fred wheel 512 Jan 31 12:35 . ""drwxr-xr-x 29 fred wheel 2048 Jan 31 12:33 .. ""-rw-r--r-- 2 fred wheel 0 Jan 31 12:33 hardlink ""-rw-r--r-- 2 fred wheel 0 Jan 31 12:33 index ""lrwxr-xr-x 1 fred wheel 5 Jan 31 12:35 softlink -> index

Practice Exercises

More information

ln(1), ls(1), rm(1), stat(1)