The last thing to cover for basic Linux usage is links - a way to refer to other files and/or directories in the Linux file system. These are useful if we want to have the same file (and edits we  accessible from multiple places

In your c2s2 folder, create a directory titled links . In the links directory:

  • Create a text file titled hardlink_orig.txt, which contains the text "This is some text we can link to!"
  • Copy hardlink_orig.txt to another file titled softlink_orig.txt 
  • Create a directory named subdir, which in turn should contain another directory titled subsubdir

In Linux, we have two types of links - the first we'll explore is hard links. When we create a hard link to a file, it acts like the exact same file that it's pointing to; a hard link points to the same place in memory. This means that if you edit what the hard link is pointing to, you edit the actual file (and vice versa). 

The way they do this is through the concept of inodes (short for index nodes). Inodes are somewhat beyond the scope of the tutorial (see here and maybe here for more info), but you can think of them as metadata that identifies files in the Linux system. When we create a hardlink to a file, the hardlink shares the same inode number (think of as reference to a place in memory) as the original file in the system, meaning they look at the same piece of memory. Because of this, if we delete either the original file or the hardlink, the other will remain and function correctly; Linux doesn't free up the memory, as there is still a place in the directory structure that references the inode.

If we want to create a hardlink to a file, we use the ln command (for link). Specifically, we run it as:

ln <target_file> <name_of_hardlink>

For example, let's create a hardlink named hardlink_link.txt that links to hardlink_orig.txt:

ln hardlink_orig.txt hardlink_link.txt

Now, let's look at the status of our directory:

ls # You should now see the hard link!

There's no real indication here that we've created a hard link, or anything other than the original file. However, we can see that this is a hardlink if we look at the inode numbers that each file points to. This can be done with the i flag for ls (here, I'm also using the 1 flag, to make sure that all listings are printed on a separate line):

ls -i1

Here on the left, we can see that hardlink_orig.txt and hardlink_link.txt share the same inode number, indicating that they reference the same file in memory. However, at this point, there is no way to tell apart the original file versus the hardlink - the hardlink acts just like the original file!

Make some changes to the contents of hardlink_orig.txt. Once you save it, open up hardlink_link.txt, and verify that the changes are also reflected there (as it's the same place in memory!) Once you've done that, delete hardlink_orig.txt and verify that you can still read/write hardlink_link.txt 

In summary, hardlinks:

  • Can only link to files
  • Act just like the file they're linked to (have the same memory)
  • Are identical to what they links to
  • Deletion of one won't affect the other

The other type of link that we can create is a soft link, also known as a symbolic link or symlink. These can link to files or directories, and act instead like a reference to the file or directory, not the actual thing (think of them like aliases from before, except in the file system). They will function just like the actual file in that you can read/write/execute them, but only because they're synonymous to the file they link to (any reference to them will be replaced with a reference to the file or directory they link to).

Because they're only a reference, if you delete the original file or directory, the soft link will not work - it's lost what it references! (This is known as a hanging link, and there's not much you can do with them; you can either replace the file/directory at its previous location (in which case the link will make sense again), or just delete the link) Because of this, the soft link is distinct from what it links to; they cannot be treated exactly the same.

We can create soft links similar to hard links, just with passing in the s flag to ln. For example, let's create a softlink named softlink_link.txt  that links to softlink_orig.txt:

ln -s softlink_orig.txt softlink_link.txt

We can also create a softlink in the links directory named dir_link that links directly to subsubdir:

ln -s dir_link subdir/subsubdir

We can examine what happened to our file system with ls:

ls # You should now see soft links!

Note that our new soft links are in a different color; we have colors enabled for ls (part of the course setup script), which can help convey extra information such as this. We can get even more information, like before, with the l flag:

ls -l

Note that for our soft links:

  • After the name of the link in the listing, it shows you what the soft link links to
  • In the first character of our first column, where we usually have - for regular files and d for directories, we now have l, indicating that the item is a (soft) link

Make some changes to the contents of softlink_orig.txt. Once you save it, open up softlink_link.txt, and verify that the changes are also reflected there. In many code editors, such as VSCode, it can also tell you the path to what you're editing, and you'll find that when you open softlink_link.txt, you're actually editing softlink_orig.txt, as the file that was linked to was automatically substituted in. Once you've done that, delete softlink_orig.txt; what happens when you try to open softlink_link.txt now? 

We can also see how this affects directories. See what happens when you try to cd into dir_link:

cd dir_link
pwd

We can see that we've automatically navigated to the subsubdir directory, as it replaced dir_link in our cd command. Furthermore, if you try to cd .., you will find yourself in subdir, the parent of subsubdir (and not going back to the parent of dir_link )

In summary, softlinks:

  • Can link to files or directories
  • Act like references to what they're linked to
  • Are NOT identical to what they link to
  • Deletion of the original will affect the link (making it a hanging link)

With that, you've got the basics of operating within Linux under your belt - congratulations! If you want, you can now move on to Book 2, where we'll cover some other advanced concepts.

  • No labels