Presentation is loading. Please wait.

Presentation is loading. Please wait.

Links Learning Objectives: 1. To understand the basics of links & its usage 2. To learn the construction / removal of different types of links 3. To distinguish.

Similar presentations


Presentation on theme: "Links Learning Objectives: 1. To understand the basics of links & its usage 2. To learn the construction / removal of different types of links 3. To distinguish."— Presentation transcript:

1 Links Learning Objectives: 1. To understand the basics of links & its usage 2. To learn the construction / removal of different types of links 3. To distinguish the differences between hard & soft links

2 COMP111 Lecture 4 / Slide 2 Links Table of Content  Links  Symbolic Links  Differences between Hard & Soft Links  Biggest Difference between Hard & Soft Links  Appending & Pattern Matching

3 COMP111 Lecture 4 / Slide 3 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-- 1 jbond cs 154 Feb 4 15:00 letter3 -rw-r--r-- 1 jbond cs 64 Feb 4 15:00 names drwxr-xr-x 2 jbond cs 512 Feb 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 cs 64 Feb 6 18:36 NAMES -rw-r--r-- 1 jbond cs 154 Feb 4 15:00 letter3 -rw-r--r-- 2 jbond cs 64 Feb 4 15:00 names drwxr-xr-x 2 jbond cs 512 Feb 4 15:00 secret/

4 COMP111 Lecture 4 / Slide 4 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 a new regular file $ ln names NAMES  A pathname of an existing directory (a link with the same basename as the original file is created in the directory) $ ln names secret  No second argument (same as giving a second argument of “.”) $ ln /bin/cat

5 COMP111 Lecture 4 / Slide 5 Links (3)  A link has two pieces of information  A name  An inode number  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 NAMESnamesletter3 007 Golden Eye Tomorrow Never Dies inode: 42979 user: 4501 group: 1501 address:... system table file contents

6 COMP111 Lecture 4 / Slide 6 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-- 3 jbond cs 64 Feb 6 18:36 NAMES 42976 -rw-r--r-- 1 jbond cs 34 Feb 4 15:00 letter3 42979 -rw-r--r-- 3 jbond cs 64 Feb 4 15:00 names 59980 drwxr-xr-x 2 jbond cs 512 Feb 4 17:10 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.

7 COMP111 Lecture 4 / Slide 7 Symbolic Links  A symbolic link is a pointer to a pathname, not a pointer to the file itself.  ln -s original target creates a symbolic link.  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 jbond cs 64 Feb 6 18:36 NAMES 42976 -rw-r--r-- 1 jbond cs 34 Feb 4 15:00 letter3 42979 -rw-r--r-- 3 jbond cs 64 Feb 4 15:00 names 59980 drwxr-xr-x 2 jbond cs 512 Feb 4 17:10 secret/ 42916 lrwxrwxrwx 1 jbond cs 5 Feb 8 17:09 snames -> names  Symbolic links are sometimes called soft links, and “regular” links are sometimes called hard links.

8 COMP111 Lecture 4 / Slide 8 Differences Between Hard and Soft Links (1)  You can’t make a hard link to a directory, but you can make a symbolic link to a directory. $ ln secret secrethlink ln: secret is a directory $ ln -s secret secretslink $ ls -li total 12 42979 -rw-r--r-- 3 jbond cs 64 Feb 6 18:36 NAMES 42976 -rw-r--r-- 1 jbond cs 34 Feb 4 15:00 letter3 42979 -rw-r--r-- 3 jbond cs 64 Feb 4 15:00 names 59980 drwxr-xr-x 2 jbond cs 512 Feb 4 17:10 secret/ 42917 lrwxrwxrwx 1 jbond cs 6 Feb 8 17:21 secretslink -> secret/ 42916 lrwxrwxrwx 1 jbond cs 5 Feb 8 17:09 snames -> names $ cd secretslink $ pwd /homes/jbond/secret

9 COMP111 Lecture 4 / Slide 9 Differences Between Hard and Soft Links (2)  You can also make symbolic links across file systems. $ pwd /homes/jbond/secret $ ls -l /tmp total 26 -rw-rw-r-- 1 root sys 13636 Feb 2 01:41 ps_data $ ln /tmp/ps_data ps_data ln: ps_data is on a different file system $ ln -s /tmp/ps_data ps_data $ ls -li total 4 59944 -rw-r--r-- 1 jbond cs 154 Feb 4 16:38 letter1 59597 lrwxrwxrwx 1 jbond cs 12 Feb 8 17:39 ps_data -> /tmp/ps_data  There is no way to tell how many symbolic links there are to a file.

10 COMP111 Lecture 4 / Slide 10 Biggest Difference Between Hard and Soft Links  The most important difference between hard and symbolic links occur when a link is removed.  For a hard link: $ echo 123 > first $ ln first second $ rm first $ cat second 123 $ echo 456 > first $ cat first 456 $ cat second 123 n For a symbolic link: $ echo 123 > first $ ln -s first second $ rm first $ cat second cat: cannot open second $ echo 456 > first $ cat first 456 $ cat second 456

11 COMP111 Lecture 4 / Slide 11 Difference Between Symbolic and Hard Links secret hardlink1 symlink1 hardlink2 1 2 3 >cat hardlink1  1 2 3 [return]  ln hardlink1 hardlink2  ln –s hardlink2 symlink1  rm hardlink2  cat > hardlink2  4 5 6 [return]  cat symlink1  result? 4 5 6

12 COMP111 Lecture 4 / Slide 12 Links in Directories  ras.cs.ust.hk:qyang:63> ls -l secret  total 16  drwx------ 2 qyang 96 Jun 18 10:32 mysecret  -rwxrwxrwx 1 qyang 56 Sep 10 2001 names  -rw------- 1 qyang 4 Jun 18 11:41 names2  ras.cs.ust.hk:qyang:64>  Why does mysecret has a link number 2?  It contains two links  One to itself: “.”  One to its parent: “..”


Download ppt "Links Learning Objectives: 1. To understand the basics of links & its usage 2. To learn the construction / removal of different types of links 3. To distinguish."

Similar presentations


Ads by Google