05. Links

Topics

  • Links
  • Hard Links
    • When to Use Hard Links
    • Number of Hard Links
    • Check for Hard Links
  • Symbolic Links
    • Advantages/Disadvantages of Symbolic Links
    • Check for Symbolic Links

Links

  • Links are files that contain the address of another file.
    • For programmers, links are pointers or references to other files.
    • In Windows, links are called shortcuts
  • Links help users to easily access data that are in a different directory. Instead of typing a long path to get to someone’s file, you create a link to the file and put the link in your own directory.
  • Then you access the file by using the name of the link. When the shell sees the link name as an argument, it accesses the file for you.
  • 2 types of links: hard links and symbolic (or soft) links
    • Hard links contain the physical address (memory location) of another file
    • Symbolic links contain the path to another file

Hard Links

  • Any time you create a new file, you create the first hard link to the file. The filename you use is the hard link to the file. When you access the file by typing the filename, the hard link takes you to the physical address of the file.
  • To create subsequent hard links to a file: ln existingFile linkName
    • ln is for link
    • existingFile is the file you want to link to, and can contain a path
    • linkName is the name of the new link, and can contain a path
  • To remove a hard link: rm linkName where linkName can have a path

When to Use Hard Links

  • Advantages of using hard links:
    • If several people have hard links to the same file, then when one person changes the file, everyone sees the change because everyone is “looking” at the same file (same memory location)
    • When you have a hard link to a file, you have the physical address (direct access to the memory location) of the file. So if the owner of the file deletes the filename from his/her directory, you still have access to this file from your directory. A file is “gone” only when all hard links are removed
  • Disadvantages of using hard links:
    • As a regular user, you cannot create a hard link to a directory
    • Because hard links contain physical address of memory, you cannot create hard links over different file systems. If someone’s file is on a different hard disk or a different sector of the disk than where your directory is, you cannot create a hard link to his/her file
    • To overcome these limitations of hard links, you need to use symbolic links

Number of Hard Links

  • Every regular file has at least 1 hard link, created from when the file is created and given a name
  • Every directory has at least 2 hard links, the second link is to link up to the parent directory, in order to maintain the tree hierarchy
  • Every time someone uses ln to create a subsequent hard link, the number of hard links of the file increases by 1
  • Every time someone uses rm to remove a hard link, the number of hard links decreases by 1
  • The number of hard links of a file called filename is shown in the second column of ls –l filename

Check for Hard Links

Every file in the system has a unique ID called the inode number

To see the inode number for a file:

ls –i filename

where i is for inode and filename can contain a path

If a file has 3 hard links, each of the hard links will have the same inode number because they are all links to the same memory location (same file)

Therefore, to determine if 2 filenames are actually hard linked to the same file, check their inode numbers

Files that are hard linked together have the same inode number

Symbolic Links

  • Symbolic links contain the path from the link to the actual file
  • The file type link refers to symbolic links, not to hard links
  • Since hard links are direct access to memory locations, hard links are considered regular files
  • To create a symbolic link to a file:
    • ln –s existingPath linkName
    • existingPath is the path from the link to the destination file. This path will be stored in the link, so the link can help you access the file. If you create a symbolic link, it is best to use an absolute path to the destination file
    • linkName is the name of the link, and can have a path
  • Creating a symbolic link to a file will have no effect on the number of hard links the file has
  • To remove a symbolic link: rm linkName
    • where linkName can have a path

Advantages of symbolic links

  • Can link over file systems
  • Can link to directories

Disadvantage of symbolic links

  • Since the link contains the path to the actual file, if the file is removed or is moved to another location, the link will be broken. You will no longer be able to access the file through the link
  • For both symbolic links and hard links: you can create a link to a file only if the owner of the file allows you to access the file (file access is covered in the Permissions section)

Check for Symbolic Links

  • To check that a file is a link, there are 2 ways:
    • ls –l filename
  • the first character in the mode column is l and the filename will show where the link is pointing to
    • ls –F filename
    • the last character in the filename is @