Skip to main content

Understanding Inodes and File System Internals

1. Understanding Inode Structure

Field Description Size (bytes)
inode number Unique identifier for the inode within the filesystem 4 or 8 bytes
file type Indicates whether it's a regular file, directory, symlink, etc. 2 bits
permissions Read, write, execute permissions for owner, group, others 9 bits
link count Number of hard links pointing to this inode 2 bytes
owner ID (UID) User ID of the file owner 4 bytes
group ID (GID) Group ID of the file owner 4 bytes
size in bytes Size of the file in bytes 8 bytes
block count Number of 512-byte blocks allocated to the file 8 bytes
access time (atime) Time of last access 8 bytes
modify time (mtime) Time of last modification 8 bytes
change time (ctime) Time of last metadata change 8 bytes
birth time (btime) Time of creation (not available on all systems) 8 bytes
block pointers Pointers to data blocks (typically 12 direct + 1 indirect + 1 double indirect) 15 * 4 or 8 bytes

Example: Inspecting Inode Details

ls -li /path/to/file
Note: The inode structure varies by filesystem type (ext2, ext3, ext4, XFS, etc.). Some fields like btime are not supported on older filesystems.

2. Viewing Inode Information

Command Description Example Output
ls -i Lists files with their inode numbers
123456 file1.txt
789012 file2.log
stat Displays detailed file or filesystem status including inode info
File: /home/user/file1.txt
  Size: 1024            Blocks: 2          IO Block: 4096   regular file
Inode: 123456        Links: 1
Access: (0644/-rw-r--r--)  Uid: ( 1000/   user)   Gid: ( 1000/   user)
Access: 2025-04-01 10:00:00.000000000 +0000
Modify: 2025-04-01 10:00:00.000000000 +0000
Change: 2025-04-01 10:00:00.000000000 +0000
find -inum Searches for files by inode number /home/user/file1.txt

Example: Using stat to view inode details

stat /etc/passwd
Note: Inode numbers are unique within a filesystem but not across different filesystems.

3. Checking Inode Usage

Command Description Example Output
df -i Display inode usage for all mounted filesystems
Filesystem     Inodes IUsed   IFree IUse% Mounted on
/dev/sda1       100000 50000   50000   50% /
tmpfs           200000  1000   199000    1% /tmp
ls -li List files with inode numbers (useful for checking individual inodes) 123456 -rw-r--r-- 1 user group 1024 Jan 1 12:00 file.txt
find / -inum <inode_number> Find files by inode number /home/user/file.txt

Example: Find files using a specific inode number

find / -inum 123456
Note: Inode usage can be checked with df -i, which shows the number of inodes used and available on each filesystem.
Aspect Hard Links Inodes
Definition A directory entry that points to the same inode as another file Metadata structure storing file attributes and pointers to data blocks
Count Multiple directory entries can point to the same inode Each file has one unique inode
File Size Does not affect file size Represents actual file size and metadata
Permissions Same permissions as original file Permissions are separate from hard link permissions
Deletion Original file remains accessible until all hard links are removed Removed only when no more hard links exist
Symbolic Links Cannot be created to directories (unless root) Not applicable to inodes
touch file1.txt
ln file1.txt file2.txt
ls -li
Note: Hard links cannot span across different filesystems.

5. Finding Files by Inode Number

Command Description Example
find Searches for files and directories by inode number using the -inum option. find /path/to/search -inum 12345
ls -i Lists files with their inode numbers in the current directory. ls -i
stat Displays detailed information about a file, including its inode number. stat filename

Example: Find file by inode number in a directory

find /home/user -inum 12345
Note: Inode numbers are unique within a filesystem but not across different filesystems.

6. Understanding File Descriptors

Descriptor Type Description Default Value
stdin Standard input stream 0
stdout Standard output stream 1
stderr Standard error stream 2

Example: Opening a File Descriptor

fd=3
exec 3<< /etc/passwd
cat &3
exec 3<&-

Example: Redirecting File Descriptors

ls -l /etc/passwd 2>&1 >output.txt
Note: File descriptors are integers used to reference open files or I/O streams. They are managed by the kernel and can be duplicated, redirected, or closed.

7. Viewing Open File Descriptors

Command Description Notes
lsof Lists open files and their associated processes Most common tool for viewing file descriptors
/proc/<pid>/fd/ Directory containing symbolic links to open files Each link represents an open file descriptor
ls -l /proc/<pid>/fd/ Lists file descriptors for a specific process Shows file paths and permissions

Example: Using lsof to view open files

lsof -p 1234

Example: Inspecting file descriptors in /proc

ls -l /proc/1234/fd/
Note: The /proc/<pid>/fd/ directory provides a snapshot of all open file descriptors for the specified process ID.

8. Understanding Sparse Files

Sparse files are files that contain large sections of null bytes (zeros), but do not actually allocate disk space for these sections. Instead, the filesystem only allocates space for actual data, making sparse files efficient in terms of disk usage.

Property Description Notes
File Size vs Disk Usage Actual size of file on disk vs logical file size Logical size can be much larger than physical space used
Creation Method Using tools like dd or fallocate Can be created with zero-filled regions
Filesystem Support Most modern Linux filesystems (ext4, XFS) Not supported on FAT or older filesystems

Example: Creating a Sparse File

dd if=/dev/zero of=sparse_file bs=1M count=0 seek=1024

Example: Checking File Size and Disk Usage

ls -l sparse_file
du -h sparse_file
Note: The ls -l command shows the logical file size, while du -h shows actual disk usage.

Key Points About Sparse Files

  • Sparse files do not consume disk space for zero regions.
  • They are useful for large files with many empty sections (e.g., databases, virtual machines).
  • Operations like cp or dd may not preserve sparseness.

9. Managing Extended Attributes

Attribute Name Value Type Description Commands
user.comment String User-defined comment for a file setfattr -n user.comment -v "Important file" filename
security.selinux String SELinux security context getfattr -n security.selinux filename
trusted.* Binary Trusted namespace for system use setfattr -n trusted.user_data -v "secret" filename

Example: Setting and Retrieving Extended Attributes

setfattr -n user.author -v "John Doe" /path/to/file
getfattr -n user.author /path/to/file
Note: Extended attributes are supported on filesystems like ext2, ext3, ext4, and XFS.
Warning: Writing to trusted.* attributes requires root privileges.

10. Understanding File System Journaling

Aspect Description Notes
Journaling Logging of file system changes before they are committed to the main file system Protects against data corruption during unexpected shutdowns or crashes
Journal Location Stored in a separate area of the file system or on a dedicated partition Can be located in a separate area to avoid conflicts with the main file system
Journal Types Transaction-based logging, metadata-only journaling, and data journaling Transaction-based logging ensures atomicity of operations
Journaling Benefits Reduces recovery time, improves data consistency, and prevents file system corruption Improves reliability of file systems like ext3, ext4, and XFS
Journaling Overhead Minimal performance impact in most cases Can cause slight delays during write operations due to logging

Example: Enabling Journaling on ext4

sudo mkfs.ext4 -J /dev/sdb1
Note: Journaling is enabled by default in ext4 and XFS file systems.
Warning: Disabling journaling can lead to data loss or corruption if the system crashes unexpectedly.

Key Points About Journaling

  • Journaling ensures that file system changes are logged before being committed.
  • It helps in maintaining data integrity and reducing recovery time after a crash.
  • Journaling is supported by most modern file systems including ext3, ext4, and XFS.

Was this article helpful?