Last modified: June 11, 2024
This article is written in: 🇺🇸
The Inode
An inode (short for "index node") is a fundamental concept in many filesystems, serving as a data structure that describes a file or a directory. Each inode contains crucial metadata about a file, but not the file's actual data.
Key Characteristics of Inodes:
- An inode stores essential metadata such as the file's owner, permissions, size, timestamps (creation, modification, and last accessed), and pointers to the file's data blocks.
- Every file or directory has a unique inode number within a given filesystem. This number helps the system efficiently manage and locate the file's data.
- To view the inode number and other details of files in a directory, use the
ls -li
command. The first column in the output displays the inode number.
Example:
$ ls -li
total 8
684867 -rw-r--r-- 1 user user 41 Mar 1 12:34 file1
684868 -rw-r--r-- 1 user user 41 Mar 1 12:34 file2
684869 -rw-r--r-- 1 user user 41 Mar 1 12:34 file3
Here, the inode numbers for file1
, file2
, and file3
are 684867
, 684868
, and 684869
.
- For more detailed inode information about a particular file, use the
stat
command:
$ stat file1
File: file1
Size: 41 Blocks: 8 IO Block: 4096 regular file
Device: 806h/2054d Inode: 684867 Links: 1
Access: (0644/-rw-rw-r--) Uid: ( 1000/ adam) Gid: ( 1000/ adam)
Hardlinks
A hardlink creates an additional reference to the existing inode of a file. It's essentially another name for an existing file on the same filesystem.
- Use the
ln
command to crea a hardlink:
ln existing_file hardlink_name
- Deleting a hardlink leaves the original file untouched. However, if you delete the source file, all its hardlinks will still point to its content, as they all reference the same inode.
Symlinks (Symbolic Links)
Symlinks are special pointers that reference the path to another file or directory.
-
Unlike hardlinks, symlinks can point to objects across different filesystems or even non-existent targets.
-
Use the
ln -s
command to create a symlink:
ln -s existing_file symlink_name
- To determine the target of a symlink, use the
readlink -f
command:
readlink -f symlink_name
- Deleting the symlink doesn't affect the target, but if the target file or directory is removed, the symlink becomes a "dangling link", pointing to a non-existent location.
Key Differences Between Hardlinks and Symlinks
Feature | Hardlink | Symlink |
Points across different filesystems | No | Yes |
Affected by changes to its target's attributes | Yes (Shares same inode) | No (Points to a path, not an inode) |
Points to non-existent files | No | Yes (Can create "dangling links") |
Reference | Inode of the target | Path to the target |
Challenges
I. Hard Link Exploration
- Create a text file named
myfile.txt
in a directory. - Inside another directory, create a hard link to
myfile.txt
namedmyhardlink
. - Delete
myhardlink
. - What happened to the original
myfile.txt
? Is it still accessible?
II. Inode Investigation
- Create a text file named
inodefile.txt
. - Make a symlink to
inodefile.txt
in the same directory and name itsymlink_to_inodefile
. - Display the inode number for both
inodefile.txt
andsymlink_to_inodefile
using thels -li
command. - Compare the inode numbers. Are they the same or different?
III. Library Links Search
- Navigate to the
/lib
folder. - Use the
ls
command to list all the files and identify which ones are links. Can you differentiate between hard links and symlinks? - Hint: Hard links will have a link count greater than 1 in the second column, while symlinks will be highlighted differently (often in cyan) and show the path they link to.
IV. Dangling Symlinks
- Create a text file named
original.txt
. - Create a symlink to
original.txt
nameddangling_symlink
. - Delete
original.txt
. - What happens when you try to access
dangling_symlink
? Why?
V. Can a filesystem run out of inodes even if there's still disk space available? Research and explain.
VI. Try creating a hard link to a directory. What happens and why?
VII. Multiple Hard Links
- Create a text file named
multi.txt
. - Make three hard links to this file in different locations or directories.
- Modify the content of
multi.txt
. - Check the content of all three hard links. What do you observe?
VIII. Use the ls
command with a flag that indicates the type of file (file, directory, symlink, etc.) for each item in the /etc
directory. Which flag should you use, and what are the indicators for each type?
IX. Changing Symlink Targets
- Create two text files,
fileA.txt
andfileB.txt
. - Create a symlink named
mylink
pointing tofileA.txt
. - Without deleting
mylink
, make it point tofileB.txt
. How would you do this?
X. How much space does an inode typically consume on a filesystem? Research and provide your findings.