Exploring INODES in Linux
What is Inode Link to heading
An inode is a data structure used by filesystems in Unix-like operating systems (Linux, macOS, etc.) to store information about a file or a directory. The inode does not contain the filename or the file content but rather metadata such as:
- File type (regular file, directory, symlink, etc.)
- Permissions (read, write, execute)
- Ownership (user ID and group ID)
- Timestamps (last access, last modification, last inode change)
- File size
- Pointers to the data blocks (where the file content is stored on disk)
Every file and directory has a unique inode associated with it in the filesystem. When you access a file, the operating system looks up the inode to get the metadata and pointers to the file’s data blocks.
# Let's create a file and check Inode number
user@server ~/k/c/w/z/inode> echo -e "Content 1" > testfile.txt
user@server ~/k/c/w/z/inode> ls -i
5249599 testfile.txt
What hapens when change file contents Link to heading
The data in the file is modified, and the changes are written to the data blocks pointed to by the inode. Inode remains the same but data like timestamp nad datasize changes. Let’s take a look at an example.
# Modifying the contents of testfile.txt
user@server ~/k/c/w/z/inode> echo -e "\nTest Content 2" >> testfile.txt
user@server ~/k/c/w/z/inode> ls -i
5249599 testfile.txt
What hapens when copy files Link to heading
When you copy a file, you essentially create a new file with a new inode. The original file and the copied file will have different inodes, even though the content is identical. The metadata, such as timestamps and file size, are copied over, but the inode numbers and data block pointers will be different. Here’s am example:
user@server ~/k/c/w/z/inode> cp testfile.txt testfilecopy.txt
user@server ~/k/c/w/z/inode> ls -i
5249619 testfilecopy.txt 5249599 testfile.txt
What hapens when when moving files Link to heading
When you move a file within the same filesystem (e.g., using mv command in Linux), you are only changing the filename or directory location. The inode remains the same, and no new inode is created. The file’s data and metadata remain intact, but the path to access the file changes.
user@server ~/k/c/w/z/inode> mv testfile.txt testfile_moved.txt
user@server ~/k/c/w/z/inode> ls -i
5249619 testfilecopy.txt 5249599 testfile_moved.txt
What is Softlink Link to heading
A soft link (also called a symbolic link or symlink) is a special type of file that points to another file or directory. Unlike hard links, soft links can link to files or directories across different filesystems. A symlink stores the path to the target file, not the actual content. Key characteristics of soft links:
- Can link to files or directories.
- Can span across different filesystems.
- If the target file is deleted or moved, the symlink becomes a “dangling” symlink (i.e., it no longer points to a valid file).
Create Softlink Link to heading
user@server ~/k/c/w/z/inode> cat testfile.txt
Content 1
Test Content 2
# Create a Softlink
user@server ~/k/c/w/z/inode> ln -s testfile.txt symlink.txt
user@server ~/k/c/w/z/inode> ls -il
total 4
5249619 lrwxrwxrwx 1 user user 12 Nov 28 00:35 symlink.txt -> testfile.txt
5249599 -rw-rw-r-- 1 user user 26 Nov 28 00:25 testfile.txt
user@server ~/k/c/w/z/inode> cat symlink.txt
Content 1
Test Content 2
Change Content Link to heading
Changing the content of the target file will affect the file that the symlink points to. However, changing the symlink itself will not affect the target file.
user@server ~/k/c/w/z/inode> ls
symlink.txt@ testfile.txt
user@server ~/k/c/w/z/inode> echo -e "Add another Content3" >> testfile.txt
user@server ~/k/c/w/z/inode> cat symlink.txt
Content 1
Test Content 2
Add another Content3
user@server ~/k/c/w/z/inode> echo -e "Add content4 in symlink file" >> symlink.txt
user@server ~/k/c/w/z/inode> cat testfile.txt
Content 1
Test Content 2
Add another Content3
Add content4 in symlink file
user@server ~/k/c/w/z/inode> cat symlink.txt
Content 1
Test Content 2
Add another Content3
Add content4 in symlink file
user@server ~/k/c/w/z/inode> ls -i
5249619 symlink.txt@ 5249599 testfile.txt
Delete Softlink Link to heading
When you delete a soft link (e.g., using rm), only the symlink is deleted. The target file remains intact because a symlink is just a pointer, not the actual file content.
user@server ~/k/c/w/z/inode> rm symlink.txt
user@server ~/k/c/w/z/inode> ls -i
5249599 testfile.txt
user@server ~/k/c/w/z/inode> cat testfile.txt
Content 1
Test Content 2
Add another Content3
Add content4 in symlink file
What is Hardlink Link to heading
A hard link is another name for an existing file. Unlike a soft link, a hard link points directly to the inode of the file, so both the original file and the hard link are essentially equal. When you create a hard link:
- The inode of the original file is shared with the new hard link.
- Both files share the same content and metadata, and deleting one file does not affect the other.
- Hard links cannot span different filesystems and cannot be created for directories (except for the special . and .. entries). Example
user@server ~/k/c/w/z/inode> ls -i
5249599 testfile.txt
user@server ~/k/c/w/z/inode> ln testfile.txt hardlink.txt
user@server ~/k/c/w/z/inode> ls -i
5249599 hardlink.txt 5249599 testfile.txt
Change Content Link to heading
Since both the original file and the hard link point to the same inode, changing the content of one will affect the other. Let’s look at an example
user@server ~/k/c/w/z/inode> echo -e "Add content5 in hardlink file" >> testfile.txt
user@server ~/k/c/w/z/inode> ls -i
5249599 hardlink.txt 5249599 testfile.txt
user@server ~/k/c/w/z/inode> cat testfile.txt
Content 1
Test Content 2
Add another Content3
Add content4 in symlink file
Add content5 in hardlink
user@server ~/k/c/w/z/inode> cat hardlink.txt
Content 1
Test Content 2
Add another Content3
Add content4 in symlink file
Add content5 in hardlink file
Summary Link to heading
- Inode: A data structure that stores metadata and pointers to the data blocks of a file. It does not contain the filename or the file content.
- Soft Link (Symlink): A pointer to another file or directory, stored as a path. Can span different filesystems but can become “dangling” if the target is deleted.
- Hard Link: Another reference to the same inode and data blocks. Changes made to one hard link affect all hard links that share the same inode. They cannot span filesystems.