Linux administration

Create and Change Hard and Symbolic Links

Nguyen Hai Chau
Vietnam National University

Symbolic Links

  • A symbolic link (or symlink) is a pseudo-file which behaves as an alternative name for some other file or directory
  • The 'contents' of the symlink are the real name pointed to
  • When you try to use a file name including a symlink, the kernel replaces the symlink component with its 'contents' and starts again
  • Symlinks allow you to keep a file (or directory) in one place, but pretend it lives in another
    • For example, to ensure that an obsolete name continues to work for older software
    • Or to spread data from a single filesystem hierarchy over multiple disk partitions

Examining and Creating Symbolic Links

  • ls -l shows where a symbolic link points to:
$ ls -l /usr/tmp
lrwxrwxrwx 1 root root 30 Sep 26 2000 /usr/tmp -> /var/tmp
  • ls can also be made to list symlinks in a different colour to other files, or to suffix their names with @
  • A symlink is created with the ln -s command
  • Its syntax is similar to cp - the original name comes first, then the name you want to create:
$ ln -s real-file file-link
$ ln -s real-dir dir-link
$ ls -l file-link dir-link
lrwxrwxrwx 1 bob bob 9 Jan 11 15:22 file-link -> real-file
lrwxrwxrwx 1 bob bob 8 Jan 11 15:22 dir-link -> real-dir

Hard Links

  • Where symlinks refer to other files by name, a hard link refers to another file by inode number
    • An inode is the data structure that describes a file on disk
    • It contains information about the file, including its type (file/directory/device), modification time, permissions, etc.
  • A directory entry contains a name and an inode number
    • So a file's name is not considered to be part of the file itself
  • You get a hard link when different directory entries on a filesystem refer to the same inode number

Symlinks and Hard Links Illustrated

  • A symbolic link refers to filename, which in turn refers to an inode:

  • A hard link is a normal directory entry, referring directly to an inode:

Comparing Symlinks and Hard Links

Symlink Hard link
Symlinks are distinctly different from normal files, so we can distinguish a symlink from the original it points to Multiple hard-link style names for the same file are indistinguishable; the term 'hard link' is merely conventional
Symlinks can point to any type of file (normal file, directory, device file, symlink, etc.) Hard links work by inode number, so they can only work within a single filesystem
Symlinks refer to names, so they can point to files on other filesystems Hard links may not point to a directory (or, on some non-Linux systems, to a symlink)
Conversely, if you rename or delete the original file pointed to by a symlink, the symlink gets broken Renaming or deleting the 'original' file pointed to by a hard link has no effect on the hard link
Symlinks may take up additional disk space (to store the name pointed to) Hard links only need as much disk space as a directory entry

Examining and Creating Hard Links

  • Use the ln command to create a hard link
  • Don't use the -s option when creating hard links
  • As when creating symlinks, the order of the arguments to ln mimics cp:
$ ls -l *.dtd
-rw-r--r-   1 anna  anna    11170   Dec 9   14:11   module.dtd
$ ln module.dtd chapter.dtd
$ ls -l *.dtd
-rw-r--r-   2 anna  anna    11170   Dec 9   14:11   module.dtd
-rw-r--r-   2 anna  anna    11170   Dec 9   14:11   chapter.dtd
  • Notice that the link count in the listing increases to 2
  • The two names are now indistinguishable
    • Deleting or renaming one doesn't affect the other

Preserving Links

  • Commands that operate on files often take options to specify whether links are followed
  • The tar command notices when two files it's archiving are hard links to each other, and stores that fact correctly
  • By default tar also stores symlinks in archives
    • Use the -h option (--dereference) to instead store the file pointed to
  • The cp command by default ignores both hard links and symlinks
    • Use the -d option (--no-dereference) to preserve all links
    • Use the -R option (--recursive) when copying recursively to ensure that symlinks are preserved
    • The -a option (--archive) implies both -d and -R

Finding Symbolic Links to a File

  • The find command has a -lname option which searches for symbolic links containing some text:
$ find / -lname '*file' -printf '%p -> %l\n'
  • This command prints the names and destinations of all symbolic links whose destination ends in file
  • Be aware that running find over the entire filesystem is very disk-intensive!

Finding Hard Links to a File

  • Hard links can be found by searching for directory entries with a given inode number
  • First, identify the filesystem and inode number of the file you're interested in:
$ df module.dtd
Filesystem  1k-blocks   Used    Available   Use%    Mounted on
/dev/sdb3   13647416    5241196 7712972     40% /home
$ ls -i module.dtd
245713 module.dtd
  • Then use find's -inum option to look for directory entries in that filesystem with that inode number:
$ find /home -xdev -inum 245713
  • The -xdev option prevents find from recursing down into other filesystems

Exercise 1

  • a. Make a temporary directory and change into it.
  • b. Make some test files as follows:
$ echo "oranges and lemons" > fruit
$ echo spuds > veg
  • c. Make a symbolic link called starch to the veg file.
  • d. Make a hard link called citrus to the appropriate file, and check that it has the same inode number.
  • e. Delete the original fruit file and check that citrus still contains the text.
  • f. Delete the original veg file and try to look at the contents of starch. Use ls to check the symlink.

Exercise 2

  • a. Try to see what the following loop does, and then create some .htm files and try it:
$ for htm in *.htm; do
> ln -s $htm ${htm}l;
> done
  • b. Make a symlink called dir to a directory (such as /etc).
  • c. Try the following commands to display the link and compare the results:
$ ls -l dir
$ ls -l dir/