1 ULI101 Introduction to UNIX/Linux and the Internet Filesystem Links Seneca College of Applied Technology ©2005 Raymodn.chan for ULI101 2 What is a filesystem Link? A link is a pointer to a file. This pointer associates a file name with a number called an i-node number An i-node is the control structure for a file (on a UNIX/Linux filesystem) If two file names have the same i-node number, they are links to the same file ©2005 Raymodn.chan for ULI101 3 Display a file's i-node number ● Use the command “ls -i” to print i-node number of each file: [ray@localhost week8]$ ls -i 32764 lab3a.html 37745 lab3b.html 37740 lab3.zip ● Use the command “ls -il” for long listing [ray@localhost week8]$ ls -il total 40 32764 -rw-r--r-- 1 ray ray 1097 Sep 13 08:53 lab3a.html 37745 -rw-r--r-- 1 ray ray 6582 Sep 13 08:53 lab3b.html 37740 -rw-rw-r-- 1 ray ray 6218 Sep 14 00:05 lab3.zip i-node ©2005 Raymodn.chan for ULI101 4 Two kinds of links Hard Links and Soft or Symbolic Links ©2005 Raymodn.chan for ULI101 5 Hard Links ● Hard link is a reference to the physical data on a filesystem ● All named files are hard links ● More than one name can be associated with the same physical data ● Hard link can only refer to data that exists on the same file system ● ©2005 Raymodn.chan for ULI101 6 Create Hard Links ● When you use “vi” to create a new file, you create the first hard link (vi myfile) ● You can also use the “touch” command to create the first hard link (touch myfile) ● To Create the 2nd, 3rd and etc. hard links, use the command: ln myfile link-name ©2005 Raymodn.chan for ULI101 7 Display Hard Links info ● Create a new file called “myfile” ● Run the command “ls -il” to display the i-node number and link counter 38753 -rw-rw-r-^ |-- inode # ©2005 Raymodn.chan for ULI101 1 uli uli 29 Oct 29 08:47 myfile ^ |-- link counter (one link) 8 Display Hard Link Info ● Create a 2nd link to the same data: ln myfile mylink ● Run the command “ls -il”: 38753 -rw-rw-r-- 2 uli uli 29 Oct 29 08:47 myfile 38753 -rw-rw-r-- 2 uli uli 29 Oct 29 08:47 mylink ^ |-- inode # ©2005 Raymodn.chan for ULI101 ^ |--link counter (2 links) 9 rd Add the 3 Link ● Create a 3rd link to the same data: ln myfile newlink ● Run the command “ls -il”: 38753 -rw-rw-r-- 3 uli uli 29 Oct 29 08:47 myfile 38753 -rw-rw-r-- 3 uli uli 29 Oct 29 08:47 mylink 38753 -rw-rw-r-- 3 uli uli 29 Oct 29 08:47 newlink ^ |-- inode # ©2005 Raymodn.chan for ULI101 ^ |--link counter (3 links) 10 Removing a link When a file has more than one link, you can remove any one link and still be able to access the file through the remaining links. [uli@seneca links] 38753 -rw-rw-r-- 3 38753 -rw-rw-r-- 3 38753 -rw-rw-r-- 3 [uli@seneca links] [uli@seneca links] 38753 -rw-rw-r-- 2 38753 -rw-rw-r-- 2 [uli@seneca links] [uli@seneca links] 38753 -rw-rw-r-- 1 ©2005 Raymodn.chan for ULI101 ls -li uli uli uli uli uli uli rm mylink ls -li uli uli uli uli rm myfile ls -li uli uli 29 Oct 29 08:47 myfile 29 Oct 29 08:47 mylink 29 Oct 29 08:47 newlink 29 Oct 29 08:47 myfile 29 Oct 29 08:47 newlink 29 Oct 29 08:47 newlink 11 Removing all links ● Remove all the links [uli@seneca links] 38753 -rw-rw-r-- 2 38753 -rw-rw-r-- 2 [uli@seneca links] This is my file. [uli@seneca links] This is my file. [uli@seneca links] [uli@seneca links] 38753 -rw-rw-r-- 1 [uli@seneca links] This is my file. [uli@seneca links] [uli@seneca links] ©2005 Raymodn.chan for ULI101 ls -li uli uli uli uli cat myfile 29 Oct 29 08:47 myfile 29 Oct 29 08:47 newlink cat newlink rm myfile ls -li uli uli 29 Oct 29 08:47 newlink cat newlink rm newlink ls -li 12 Advanced use of links Directory Cross-classify links File Courses uli101 iso110 ica101 to_do to_do to_do to_do uli101 iso110 ica101 Links ©2005 Raymodn.chan for ULI101 13 Create Cross-classify files [uli@seneca uli] cd courses [uli@seneca courses] mkdir uli101 ios110 ica101 [uli@seneca courses] vi uli101/to_do ... Add to do list to each file [uli@seneca courses] vi ios110/to_do ... under each course's [uli@seneca courses] vi ica101/to_do ... directory [uli@seneca courses] ls -liR 38767 drwxrwxr-x 2 uli uli 72 Oct 29 11:15 ica101 38768 drwxrwxr-x 2 uli uli 72 Oct 29 11:16 ios110 38762 drwxrwxr-x 2 uli uli 72 Oct 29 11:14 uli101 ./ica101: 44309 -rw-rw-r-- 1 uli uli 45 Oct 29 11:15 to_do ./ios110: 44407 -rw-rw-r-- 1 uli uli 49 Oct 29 11:16 to_do ./uli101: 38772 -rw-rw-r-- 1 uli uli 51 Oct 29 11:14 to_do ©2005 Raymodn.chan for ULI101 14 Create Cross-classify links [uli@seneca courses] ls -i 38767 ica101 38768 ios110 38762 uli101 [uli@seneca courses] mkdir to_do 38767 ica101 38768 ios110 38769 to_do 38762 uli101 Create links in the to_do directory to the to_do file in each course's directory [uli@seneca courses] ln ica101/to_do to_do/ica101 [uli@seneca courses] ln ios110/to_do to_do/ios110 [uli@seneca courses] ln uli101/to_do to_do/uli101 ©2005 Raymodn.chan for ULI101 15 Display links to the same file ● Pairs of files in different directories point to the same physical file: [uli@seneca courses] ls -li ica101/to_do to_do/ica101 44309 -rw-rw-r-- 2 uli uli 45 Oct 29 11:15 ica101/to_do 44309 -rw-rw-r-- 2 uli uli 45 Oct 29 11:15 to_do/ica101 [uli@seneca courses] ls -li ios110/to_do to_do/ios110 44407 -rw-rw-r-- 2 ray ray 49 Oct 29 11:16 ios110/to_do 44407 -rw-rw-r-- 2 ray ray 49 Oct 29 11:16 to_do/ios110 [uli@seneca courses] ls -li uli101/to_do to_do/uli101 38772 -rw-rw-r-- 2 ray ray 51 Oct 29 11:14 to_do/uli101 38772 -rw-rw-r-- 2 ray ray 51 Oct 29 11:14 uli101/to_do ©2005 Raymodn.chan for ULI101 16 View To_do list files [uli@seneca courses] cat ica101/to_do Test 3 - Hands-on Database test Nov 10, 2005 [uli@seneca courses] cat iso110/to_do Shell Programming assignment due on Nov 14, 2005 [uli@seneca courses] cat uli101/to_do assignment 1 due Nov 4, 2005 lab 8 due Nov 4, 2005 [uli@seneca courses] cat ica101/to_do ios110/to_do uli101/to_do Test 3 - Hands-on Database test Nov 10, 2005 Shell Programming assignment due on Nov 14, 2005 assignment 1 due Nov 4, 2005 lab 8 due Nov 4, 2005 [uli@seneca courses] cat to_do/* ?? what would be the output ?? ©2005 Raymodn.chan for ULI101 17 Recap ● Use “ln” to create hard links ● Files with the same i-node number refer to the same physical file ● Use “ls -i” to display the file's i-node number and link counter ● Use “rm filename” to remove a hard link ● A physical file will be deleted from the file system when its link counter drops to zero, ©2005 Raymodn.chan for ULI101 18 Symbolic Links Also Know As (a.k.a.): Soft links or Symlinks ● Symbolic link is an indirect pointer to a file – a pointer to the hard link to the file ● You can create a symbolic link to a directory ● A symbolic link can point to a file on a different filesystem ● A symbolic link can point to a nonexistent file ©2005 Raymodn.chan for ULI101 19 Create Symbolic Links ● To create a symboic link to the file “myfile”, use ln -s myfile symlink or ln --symbolic myfile symlink [uli@seneca courses] ls -li myfile 44418 -rw-rw-r-- 1 uli uli 49 Oct 29 14:33 myfile [uli@seneca courses] ln -s myfile symlink [uli@seneca courses] ls -li myfile symlink 44418 -rw-rw-r-- 1 uli uli 49 Oct 29 14:33 myfile 44410 lrwxrwxrwx 1 uli Link uli counter 6 Oct 29 14:33 symlink -> myfile (1 link) File type – symboic link Different i-node number ©2005 Raymodn.chan for ULI101 20 Properties of Symbolic Link files ● The i-node number is different from the pointed to file ● The link counter of the new symbolic link file is “1” ● Symbolic link file does not affect the link counter of the pointed to file ● The type field of symblic file contains the letter “l” ● The symbolic link file and the pointed to file have different status information (e.g. file size, last modification time etc.) ©2005 Raymodn.chan for ULI101 21 Create Symbolic Link Directory ● The syntax is the same as linking to a file ln -s target_directory link_directory ln --symbolic target_directory link_directory [uli@seneca week8]$ ls -li 38766 drwxrwxr-x 7 uli uli 168 Oct 29 13:32 courses [uli@seneca week8]$ ln courses mydir ln: `courses': hard link not allowed for directory [uli@seneca week8]$ ln -s courses mydir [uli@seneca week8]$ ls -li 38766 drwxrwxr-x 7 uli uli 168 Oct 29 13:32 courses 44417 lrwxrwxrwx 1 uli uli 7 Oct 29 15:41 mydir -> courses ©2005 Raymodn.chan for ULI101 22 Directory Listing ● To display the contents in a directory, we normally use the command “ls -l directory_name” ● Compare the following to commands [uli@seneca week8]$ ls -l mydir lrwxrwxrwx 1 uli uli 7 Oct 29 15:41 mydir -> courses [uli@seneca drwxrwxr-x drwxrwxr-x drwxrwxr-x drwxrwxr-x ©2005 Raymodn.chan for ULI101 week8]$ ls -l courses 2 uli uli 72 Oct 29 11:15 2 uli uli 72 Oct 29 11:16 2 uli uli 120 Oct 29 11:20 2 uli uli 72 Oct 29 11:14 ica101 ios110 to_do uli101 23 Directory Contents of Symbolic Links ● Must use the “-L” option in order to display directory contents of a symbolic link Compare the following commands: [uli@seneca lrwxrwxrwx [uli@seneca drwxrwxr-x drwxrwxr-x drwxrwxr-x drwxrwxr-x Directory Status week8]$ ls -l mydir 1 uli uli 7 Oct 29 15:41 mydir -> courses week8]$ ls -lL mydir 2 uli uli 72 Oct 29 11:15 ica101 2 uli uli 72 Oct 29 11:16 ios110 2 uli uli 120 Oct 29 11:20 to_do 2 uli uli 72 Oct 29 11:14 uli101 Directory Contents ©2005 Raymodn.chan for ULI101 24 Delete link to a directory To delete a link to a directory, simply use the “rm” command: [uli@seneca week8]$ ls -l drwxrwxr-x 7 uli uli 168 Oct 29 13:32 courses lrwxrwxrwx 1 uli uli 7 Oct 29 15:41 mydir -> courses [uli@seneca week8]$ rm mydir [uli@seneca week8]$ ls -l drwxrwxr-x 7 uli uli 168 Oct 29 13:32 courses ©2005 Raymodn.chan for ULI101 25 Properties of Symbolic Link Directory ● The Symbolic link to a directory has a file type of “l” (the first letter of the permission field). ● The permissions on the link are set to “rwx” for all. ● chmod on the link applies to the actual directory (or file), the permissions on the link stay the same ● Can point to a nonexistent directory ©2005 Raymodn.chan for ULI101 26 Reference ● Hard Links – ● Symbolic Links – ● http://en.wikipedia.org/wiki/Hard_link http://en.wikipedia.org/wiki/Symbolic_link I-nodes – http://www.cse.unsw.edu.au/~neilb/oss/linux-commentary/vfs-7.html – http://en.wikipedia.org/wiki/Inode ©2005 Raymodn.chan for ULI101 27 Questions and Answers ● What is a filesystem link? ● What is an i-node? ● How do you display the i-node of a file/directory? ● How many kinds of filesystem links are there? ● What is a hard link? ● What is a soft link? ● Can you create a hard link to a directory? ©2005 Raymodn.chan for ULI101 28 More Q & A ● How do you create the first hard link to a file? ● How do you create the 2nd (and up) hard link to a file? ● What is the purpose of a link counter? ● Where can you get the link counter of a file? ● How do you create a soft link to a file? ● Does a soft link file has the same i-node number ©2005 Raymodn.chan for ULI101 29 More Q & A ● How do you remove a hard link? ● How do you delete a physical file with multiple links? ● How do you create a symbolic link to a file? ● How do you create a symbolic link to a directory? ● Could you create a symbolic to a file which does not exist? ©2005 Raymodn.chan for ULI101 30 More Q & A ● How do you display the contents of a regular directory? ● How do you display the contents of a symbolic link to a regular directory? ● How do you display the status information of a regular directory? ● More Q & A ? ©2005 Raymodn.chan for ULI101