Jump to content

Ict-innovation/LPI/104.6

From Wikibooks, open books for an open world
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

104.6 Create and change hard and symbolic links

[edit | edit source]

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.



When we copy a file with the cp command, a duplicate of that file is created, sometimes however we want to provide an link to an existing file but want that path to point to the exact same file as the original. In this case we will use symbolic links.

Symbolic Links

A soft link to a file or a directory is a special file type that simply contains the name of the file that it “points to”.

# ln -s mytext.txt myext.sym

Soft links can be created across filesystems. By running ls -l we can identify whether a directory entry is a soft link or just an ordinary file from the output. A symbolic link is shown as follows when we run the ls -l command

mytext.txt -> mytext.sym. Notice that the reference count is 1 for both files.

-rw------- 1 root root 223 Sep 29 09:10 mytext.txt

lrwxrwxrwx 1 root root 9 Sep 29 09:10 mytext.sym -> mytext.txt


To find all symbolic links to a file you can use the find command for example

find / -lname mytext.txt

will find all symbolic links to the file mytext.txt.


Hard Links

A hard link is an additional name for the same inode and as such the reference count of the file increases by one for every new hard link.

# ln mytextfile.txt mytextfile.link

In the listing notice that the reference count is 2 and that both files have the same size. In fact they are identical.

-rw------- 2 mark mark 223 Sep 26 09:06 mytextfile.txt

-rw------- 2 mark mark 223 Sep 26 09:06 mytextfile.link

Hard links can only be created within the same filesystem. Using ls, a hard link can be identified by the reference count shown in the output, as in the above example. Another way of finding files with hard links is to obtain the file's inode number and then run the find command with the inode number as a parameter. To find the inode of a file run the command:

# ls -i mytextfile.txt


8652338 mytextfile.txt

This will output the inode number of the file, next run the find command as follows:

# find / -inum 8652338



Used files, terms and utilities:

  • ln


Previous Chapter | Next Chapter