Jump to content

LPI Linux Certification/Create And Change Hard And Symbolic Links

From Wikibooks, open books for an open world

Detailed Objectives

[edit | edit source]

(LPIC‑1 Version 5.0)

Weight
2
Description
Candidates should be able to create and manage hard and symbolic links to a file.
Key Knowledge Areas
  • Create links.
  • Identify hard and/or softlinks.
  • Copying versus linking files.
  • Use links to support system administration tasks.
The following is a partial list of the used files, terms and utilities
  • ln
  • ls
[edit | edit source]

Use links when:

  • You want to create a pathname to a file.
  • Set a shorter or fixed pathname to a file.

To link one file to another, use ln:

ln [options] filename linkname
ln [options] filename linkdirectory

Common options are:

‐f, force
clobber existing link
‐s, symbolic link
The default links are hard links (ln without ‑s option). A hard link can only be created to an existing file on the same physical device, after creation no visible association can be displayed between a link name and a file name.
Symbolic links – also known as soft links – are like shortcuts in Windows®, in the sense that the file may be removed but the link will remain (although useless). Unlike in Windows®, however, a symbolic link can be created on a file that does not exist yet. The association between the link name and the file name can be viewed with the ls command.

Linking to a file

[edit | edit source]

The symbolic and hard link can be displayed with ls ‑l. Symbolic link are indicated with a dash, greater than, resembling a rightward arrow (→) with most monospace fonts: link_name‑>real_filename.

$ ls -l /dev/midi
lrwxrwxrwx   1   root   root        6    Feb 10 15:37   /dev/midi -> midi00

Hard links are indicated with the number of links counter (3 − 1 = 2 in this case).

$ ls -l readme
-rwxrwxrwx   3   yann   users       677  Feb 10 15:37   readme

When removing a link name, use rm. Only the link will be removed not the linked file.

Exercises

[edit | edit source]

Answer key

  1. Create directories etc and bin in your home directory.
  2. Copy all the files in recursive mode from /etc to your etc directory and do the same for /bin to bin.
  3. In your local etc directory rename all *.conf files to *.conf.bak.
  4. Create in your home directory a symbolic link called dir that points to your local bin/ls. Check if dir does execute ls.
  5. Remove the dir link. Is bin/ls still there?