Professional Documents
Culture Documents
Links: Learning Objectives
Links: Learning Objectives
Links: Learning Objectives
Learning Objectives:
1. 2.
3.
To understand the basics of links & its usage To learn the construction / removal of different types of links To distinguish the differences between hard & soft links
Links
Table of Content
Links Symbolic Links Differences between Hard & Soft Links Biggest Difference between Hard & Soft Links Appending & Pattern Matching
Links (1)
A link is a pointer to a file. In fact, in UNIX all filenames are just links to a file. Most files only have one link.
-rw-r--r--rw-r--r-drwxr-xr-x 1 jbond 1 jbond 2 jbond cs cs cs 154 Feb 64 Feb 512 Feb 4 15:00 letter3 4 15:00 names 4 15:00 secret/
Additional links to a file allow the file to be shared. The ln command creates new links.
$ ln names NAMES $ ls -l total 8 -rw-r--r-2 jbond -rw-r--r-1 jbond -rw-r--r-2 jbond drwxr-xr-x 2 jbond cs cs cs cs 64 154 64 512 Feb Feb Feb Feb 6 4 4 4 18:36 15:00 15:00 15:00 NAMES letter3 names secret/
Links (2)
ln creates a new link, not a new file. The new link and the original filename are equivalent pointers to the file. The last argument is the link destination, and can be:
A pathname of an existing directory (a link with the same basename as the original file is created in the directory)
$ ln names secret
Links (3)
A link has two pieces of information
An inode number is an index into a system table that has all the information about the file (e.g., owner, size).
$ ln names NAMES jbond letter3 names
NAMES
file contents
007 Golden Eye Tomorrow Never Dies
system table inode: 42979 user: 4501 group: 1501 address: ...
Links (4)
You can use ls -i to see if two links point to the same inode:
$ ls -li total 8 42979 -rw-r--r-42976 -rw-r--r-42979 -rw-r--r-59980 drwxr-xr-x 3 1 3 2 jbond jbond jbond jbond cs cs cs cs 64 34 64 512 Feb Feb Feb Feb 6 4 4 4 18:36 15:00 15:00 17:10 NAMES letter3 names secret/
So, using rm actually only removes a link. When the last link to a file is removed, the operating system actually removes the file.
Symbolic Links
A symbolic link is a pointer to a pathname, not a pointer to the file itself.
A symbolic link is not equivalent to a hard link. The symbolic link has a different inode.
$ ln -s names snames $ ls -li total 10 42979 -rw-r--r-- 3 42976 -rw-r--r-- 1 42979 -rw-r--r-- 3 59980 drwxr-xr-x 2 42916 lrwxrwxrwx 1
6 4 4 4 8
Symbolic links are sometimes called soft links, and regular links are sometimes called hard links.
There is no way to tell how many symbolic links there are to a file.
$ echo 123 > first $ ln first second $ rm first $ cat second 123 $ echo 456 > first $ cat first 456 $ cat second 123
$ echo 123 > first $ ln -s first second $ rm first $ cat second cat: cannot open second $ echo 456 > first $ cat first 456 $ cat second 456