Sometimes it’s nice to have files accessible from more than one location on your computer, but to only actually have one copy of the file (it can save on drive space for really big files!). An example of where I have needed this most, actually, has been with the use of Dropbox on my laptop. Many times I’ve been working out of a folder somewhere on my computer and I’ve needed to make the files accessible to other computers in the research lab. I’ve had to physically copy and paste these directories to my Dropbox folder (or usb drive, before I discovered Dropbox) and then once all the edits had been made elsewhere, had to copy and paste them back to where they belong. If, however, I could place a link to the files in my Dropbox folder, both copy and paste steps could be avoided (as well as the simultaneous confusion of which files were updated, and in what order etc.).
In linux systems this “linking” process is called “symlinking” (softlinking) or “hardlinking.” The difference between the two is a little subtle and can be understood in that unix files have two parts the “file header” (which includes the name) and the “data” part. The header part contains the file name, permissions, etc, and also contains an inode number. This inode number is like the data part’s address on the hard drive…and points/associates the file name to the data it contains. One way to think about it is through this schematic:
You can imagine that linking to the file can be accomplished through one of two methods. One method is to simply have two filenames associated with the same inode. These two filenames (file header information) can reside in different physical locations on the hard disk and thus in two different locations in your folder tree of your computer. This process is called “hardlinking” and looks like this:
Hardlinking is great. If you delete filename1, filename2 still has the data stored in itself even though the file was originally created with filename1. Dropbox supports this linking feature. The only drawback to this kind of linking is that the link literally looks like and feels like the file in any file system–you can’t really tell the difference. So you better remember what was supposed to be the link! The filename doesn’t show up as a link because it has the same properties as a normal file… file header information (stored in the filename1, including the inode number), which links to the data.
Through command line, the way you create one of these hardlinks is through the following command:
ln file-to-be-linked.txt linking-file-name.txt
Now, the other way of linking files is a literal link: its a special unique file that has a file header portion with an inode link, but instead of that inode pointing straight to the file data inode, it points to the inode of its own file within which contains a “link” to the file you’re linking too. It’s much like a shortcut in Windows…and this is called soft-linking, or forming a symbolic link or “symlink.” It works in the following manner:
Obviously that “to.txt” file is the file header information that contains the inode number of where the data actually resides…so soft links are literally a special file that points to another file and the operating system treats it as such…its obvious that its a link and if you delete the symlink it just no longer points to the file anymore. And if you delete the original file and the symlink still links to it, the data is lost…it is not kept.
To access this kind of linking in the command line, you run the following command:
ln -s file-to-be-linked.txt name-of-link-to-file
Notice how I dropped the file extension off of the name of the link…you can do that because it is its own type of file that just points to the real thing. Dropbox also supports this kind of file linking. And it comes in handy particularly if you want the file to show up as a symlink.
Now you might be asking yourself why bother with the soft linking if hard linking works so well? The answer might be baffling: all directories in unix/linux systems are soft links that contain a name and point to a data file that contains all the file header information location of all the files associated with that directory. I mean, what are folders anyways? They aren’t “files” in that they don’t have data intrinsic to themselves. But they are “containers” that tell you what is associated with them. Brilliant? Yes. Immediately intuitive? No.
So a “directory” or “folder” has its own file header information which points to an inode number that contains all the addresses of all the files associated with itself. When you go to one of these addresses of the contained files you get pointed to the inode that contains all the data associated with that file. That’s how folders work–and if you want to use a symlink to point to a directory, you have to add a special flag to the linking command:
ln -sd directory-to-be-symlinked name-of-link
Of course, you can also hardlink a directory, then the code would just be:
ln -d directory-to-be-hardlinked name-of-link
I think I mostly use symlinking so I remember its a link and not the actual file, but I’m very appreciative that Dropbox supports this option. Makes changing files in different directories on my computer remotely very easy to do!