Every file and directory in Linux contains more than just data—it also stores metadata, which describes the file’s properties such as size, type, permissions, ownership, and timestamps. Understanding and managing metadata is vital for system administration, troubleshooting, and security.
1. File Metadata Overview #
Definition: Metadata means “data about data.” In Linux, each file’s metadata is stored in an inode (index node). The inode contains all file information except its name.
Common File Metadata Includes:
| Metadata Type | Description |
|---|---|
| Inode Number | Unique identifier assigned by the file system |
| File Type | Regular file, directory, symbolic link, device, etc. |
| Permissions | Read, write, execute for user, group, and others |
| Ownership | User ID (UID) and Group ID (GID) |
| Size | File size in bytes |
| Timestamps | Access (atime), Modify (mtime), Change (ctime) |
| Links | Number of hard links pointing to the inode |
To view file metadata, Linux provides tools like stat and file.
2. The stat Command #
Purpose: The stat command displays detailed information about a file’s metadata, including size, permissions, ownership, and timestamps.
Syntax:
stat [options] filename
Example Output:
stat example.txt
Output Example:
File: example.txt Size: 1204 Blocks: 8 IO Block: 4096 regular file Device: 803h/2051d Inode: 131219 Links: 1 Access: (0644/-rw-r--r--) Uid: (1000/user) Gid: (1000/user) Access: 2025-11-03 10:45:02.000000000 +0500 Modify: 2025-11-02 14:22:10.000000000 +0500 Change: 2025-11-02 14:22:10.000000000 +0500
Explanation:
- Access: Last time file was read.
- Modify: Last time file content was changed.
- Change: Last time metadata or permissions changed.
- Links: Number of hard links to the file.
- Inode: Unique file identifier.
Usage in Administration:
- Checking file ownership and permissions.
- Monitoring when configuration files were last changed.
- Troubleshooting permission or access issues.
3. The file Command #
Purpose: The file command determines the type of a file by inspecting its content rather than its extension.
Syntax:
file filename
Examples:
- Identify a text file:
file /etc/passwdOutput:/etc/passwd: ASCII text - Identify a binary executable:
file /bin/lsOutput:/bin/ls: ELF 64-bit LSB executable - Identify a compressed file:
file backup.tar.gzOutput:backup.tar.gz: gzip compressed data
Usage in Administration:
Used for verifying file types, especially when dealing with unknown or mislabeled files.
4. Soft Links (Symbolic Links) #
Purpose:
A soft link (also called a symbolic link) is like a shortcut or pointer to another file or directory. It contains the path to the target file rather than its actual data.
Syntax:
ln -s target link_name
Example:
ln -s /var/log/syslog log_link
Now log_link acts as a shortcut to /var/log/syslog.
Key Characteristics:
- Has its own inode and file path.
- If the target file is deleted, the symbolic link becomes broken.
- Can link across file systems.
- Recognizable by the
ltype inls -l:lrwxrwxrwx 1 user user 12 Nov 3 11:00 log_link -> /var/log/syslog
Usage in Administration:
Useful for creating shortcuts to configuration files or linking shared libraries.
5. Hard Links #
Purpose: A hard link creates an additional name for an existing file. Both names point to the same inode (same data on disk).
Syntax:
ln target link_name
Example:
ln report.txt report_backup.txt
Now both files point to the same inode. Changes to one appear in the other.
Key Characteristics:
- Share the same inode number.
- File remains accessible as long as at least one hard link exists.
- Cannot link directories or cross file systems.
- Deleting one name does not delete the file’s data unless all links are removed.
Verification Example:
ls -li
Output:
131245 -rw-r--r-- 2 user user 1204 Nov 3 10:00 report.txt 131245 -rw-r--r-- 2 user user 1204 Nov 3 10:00 report_backup.txt
→ Both share the same inode (131245) and have 2 hard links.
6. Differences Between Soft and Hard Links #
| Feature | Hard Link | Soft (Symbolic) Link |
|---|---|---|
| Points To | Inode (actual data) | File path |
| Cross File Systems | ❌ No | ✅ Yes |
| Link to Directory | ❌ No | ✅ Yes |
| If Target Deleted | File still accessible | Link breaks |
| Inode Number | Same as target | Different |
| Creation Command | ln file link | ln -s file link |
7. Practical Use Cases #
| Task | Recommended Link Type |
|---|---|
| Create backup names for system files | Hard link |
| Create shortcuts in home directory | Soft link |
Link config files from /etc to /usr/local/etc | Soft link |
| Prevent accidental deletion of critical data | Hard link |