Links (not the browser)
In UNIX-based systems, files have two parts: the filename part, and the data part. The data part has an affiliation with something called an inode, which contains the file’s data, permissions, etc. The filename part contains a name and the unique ID number of the inode. Two different links can be created to manipulate the data part, hard and soft. Hard links are like indicators that two or more files must be “synced”, or kept the same, no matter which one is modified. To create a link, we use the ln command.
ln
ln can create links to file’s data parts. Let’s try it out. Type these commands:
echo Hai! > dummy.txt
ls -lia
ln dummy.txt dummy-copy.txt
cat dummy-copy.txt
echo Bai! > dummy.txt
cat dummy-copy.txt
I’ll explain each step.
1. The echo command is told to write Hai! into a file called dummy.txt. The > parameter tells echo to write this into the file.
2. The ls command lists the files. The -lia parameter tells ls to show all files, to show their inode IDs, and to show their permissions.
3. ln without any parameters will create a hard link between dummy.txt and dummy-copy.txt, which was created by ln.
4. The cat program outputs the contents of dummy-copy.txt. Notice how they’re the same as dummy.txt’s.
5. echo writes Bai! into the first file, dummy.txt.
6. cat will show the contents of dummy-copy.txt again. They’re different!
This can be very useful. Maybe not now, but eventually it will be of good service.
Symlinks
Symlinks, or soft links, are a bit different from hard links. If a file is created with a soft link to another, if the parent file is deleted, the other disappears, whereas the hardlinked copy would have remained. Try this:
ln -s dummy-copy.txt softlink-dummy.txt
cat softlink-dummy.txt
ls -lia
rm dummy-copy.txt
ls -lia
1. This time, the -s parameter tells ln that you want to make a symlink with a new file, softlink-dummy.txt.
2. cat shows the contents of the softlinked copy.
3. ls -lia shows all files with their inode number, permissions, etc.
4. rm deletes the original file.
5. ls shows all of the files and folders. Notice how both the symlinked copy and the original are gone.
A symlinked copy will also have different permissions and a different inode number.
Links are used in all different things, including LFS and advanced projects like such. These links are also useful for keeping up to date backups. Try and see what you can do with these links.
Next chapter: Your Own Packages