Table of Content

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 soft links
  • Copying versus linking files
  • Use links to support system administration tasks

Terms and Utilities:

ln
ls

link

A hard link is a directory entry that points to an inode, while a soft link or symboliclink is a directory entry that points to an inode that provides the name of another directory entry.

root@dclab-u1504s:/home/oldhorse# mkdir -p sub1/sub2
root@dclab-u1504s:/home/oldhorse# touch sub1/file1
root@dclab-u1504s:/home/oldhorse# touch sub2/file2
root@dclab-u1504s:/home/oldhorse# ln sub1/file1 sub1/file3
root@dclab-u1504s:/home/oldhorse# ln sub1/file1 sub1/sub2/file3sub
root@dclab-u1504s:/home/oldhorse# echo "something" > sub1/file1
root@dclab-u1504s:/home/oldhorse# echo "else" >> sub1/file3
root@dclab-u1504s:/home/oldhorse# cat sub1/sub2/file3sub 
something
else

ln [-s]    

root@dclab-u1504s:/home/oldhorse# ls -R sub1
sub1:
file1  file2  file3  sub2

sub1/sub2:
file3sub
root@dclab-u1504s:/home/oldhorse# ls -Rl sub1
sub1:
total 12
-rw-r--r-- 3 root root   14 Oct 17 07:37 file1
-rw-r--r-- 1 root root    0 Oct 17 07:36 file2
-rw-r--r-- 3 root root   14 Oct 17 07:37 file3
drwxr-xr-x 2 root root 4096 Oct 17 07:37 sub2

sub1/sub2:
total 4
-rw-r--r-- 3 root root 14 Oct 17 07:37 file3sub
root@dclab-u1504s:/home/oldhorse# ln -s /home/oldhorse/sub1/file1 /home/oldhorse/sub1/file4
root@dclab-u1504s:/home/oldhorse# cd sub1
root@dclab-u1504s:/home/oldhorse/sub1# ln -s file1 file5
root@dclab-u1504s:/home/oldhorse/sub1# ls -ltr
total 12
-rw-r--r-- 1 root root    0 Oct 17 07:36 file2
drwxr-xr-x 2 root root 4096 Oct 17 07:37 sub2
-rw-r--r-- 3 root root   14 Oct 17 07:37 file3
-rw-r--r-- 3 root root   14 Oct 17 07:37 file1
lrwxrwxrwx 1 root root   25 Oct 17 07:42 file4 -> /home/oldhorse/sub1/file1
lrwxrwxrwx 1 root root    5 Oct 17 07:42 file5 -> file1
root@dclab-u1504s:/home/oldhorse/sub1# echo "softlink line" >> file4
root@dclab-u1504s:/home/oldhorse/sub1# cat file4
something
else
softlink line
root@dclab-u1504s:/home/oldhorse/sub1# cat file5
something
else
softlink line

root@dclab-u1504s:/home/oldhorse# ls -lR sub1
sub1:
total 12
-rw-r--r-- 3 root root   28 Oct 17 07:43 file1
-rw-r--r-- 1 root root    0 Oct 17 07:36 file2
-rw-r--r-- 3 root root   28 Oct 17 07:43 file3      # hardlink due to inode 3 > 1
lrwxrwxrwx 1 root root   25 Oct 17 07:42 file4 -> /home/oldhorse/sub1/file1
lrwxrwxrwx 1 root root    5 Oct 17 07:42 file5 -> file1
drwxr-xr-x 2 root root 4096 Oct 17 07:37 sub2

sub1/sub2:
total 4
-rw-r--r-- 3 root root 28 Oct 17 07:43 file3sub   # hardlink due to inode 3 > 1

link inode

root@dclab-u1504s:/home/oldhorse# ls -ilR sub1
sub1:
total 12
3276814 -rw-r--r-- 3 root root   28 Oct 17 07:43 file1
3276815 -rw-r--r-- 1 root root    0 Oct 17 07:36 file2
3276814 -rw-r--r-- 3 root root   28 Oct 17 07:43 file3
3276816 lrwxrwxrwx 1 root root   25 Oct 17 07:42 file4 -> /home/oldhorse/sub1/file1
3276817 lrwxrwxrwx 1 root root    5 Oct 17 07:42 file5 -> file1
3276813 drwxr-xr-x 2 root root 4096 Oct 17 07:37 sub2

sub1/sub2:
total 4
3276814 -rw-r--r-- 3 root root 28 Oct 17 07:43 file3sub

find softlink

root@dclab-u1504s:/home/oldhorse# find sub1 -type l
sub1/file4
sub1/file5

defer softlink

root@dclab-u1504s:/home/oldhorse# ls -lH sub1
total 12
-rw-r--r-- 3 root root   28 Oct 17 07:43 file1
-rw-r--r-- 1 root root    0 Oct 17 07:36 file2
-rw-r--r-- 3 root root   28 Oct 17 07:43 file3
lrwxrwxrwx 1 root root   25 Oct 17 07:42 file4 -> /home/oldhorse/sub1/file1
lrwxrwxrwx 1 root root    5 Oct 17 07:42 file5 -> file1
drwxr-xr-x 2 root root 4096 Oct 17 07:37 sub2
root@dclab-u1504s:/home/oldhorse# ls -lL sub1
total 20
-rw-r--r-- 3 root root   28 Oct 17 07:43 file1
-rw-r--r-- 1 root root    0 Oct 17 07:36 file2
-rw-r--r-- 3 root root   28 Oct 17 07:43 file3
-rw-r--r-- 3 root root   28 Oct 17 07:43 file4
-rw-r--r-- 3 root root   28 Oct 17 07:43 file5
drwxr-xr-x 2 root root 4096 Oct 17 07:37 sub2

find hardlink

root@dclab-u1504s:/home/oldhorse# find sub1 -samefile sub1/file1
sub1/sub2/file3sub
sub1/file1
sub1/file3

find softlink

root@dclab-u1504s:/home/oldhorse# find sub1 -lname "*file1"
sub1/file4
sub1/file5

root@dclab-u1504s:/home/oldhorse# find /usr/lib/ -lname "*libstdc++*"
/usr/lib/x86_64-linux-gnu/libstdc++.so.6
/usr/lib/gcc/x86_64-linux-gnu/4.9/libstdc++.so

Quiz questions

1. How does the output of ls identify a hard link?
2. How can you use ls to identify which two files are hard links to each other?
3. Why does an empty directory show contain 2 links?
4. Which flag for ln will request it to overwrite an existing file or link?
5. What happens when you delete a file to which a symbolic link points?
6. What happens when you delete a file to which a hard link points?
Answers to quiz questions
1. The second column contains a number other than 1.
2. ls -i
shows inode numbers. If the inode numbers are the same, the files are the same.
3. The link count for the directory is the number of inodes in it – one for “.” and one for “..”.
4. -f
5. The symbolic link is unchanged, but points to nowhere.
6. The other part of the hard link remains the same.