Skybinary

View Categories

1.2.3 Understanding File Metadata and Links in Linux

2 min read

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 TypeDescription
Inode NumberUnique identifier assigned by the file system
File TypeRegular file, directory, symbolic link, device, etc.
PermissionsRead, write, execute for user, group, and others
OwnershipUser ID (UID) and Group ID (GID)
SizeFile size in bytes
TimestampsAccess (atime), Modify (mtime), Change (ctime)
LinksNumber 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/passwd Output: /etc/passwd: ASCII text
  • Identify a binary executable: file /bin/ls Output: /bin/ls: ELF 64-bit LSB executable
  • Identify a compressed file: file backup.tar.gz Output: 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 l type in ls -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 #

FeatureHard LinkSoft (Symbolic) Link
Points ToInode (actual data)File path
Cross File Systems❌ No✅ Yes
Link to Directory❌ No✅ Yes
If Target DeletedFile still accessibleLink breaks
Inode NumberSame as targetDifferent
Creation Commandln file linkln -s file link

7. Practical Use Cases #

TaskRecommended Link Type
Create backup names for system filesHard link
Create shortcuts in home directorySoft link
Link config files from /etc to /usr/local/etcSoft link
Prevent accidental deletion of critical dataHard link

Powered by BetterDocs

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to Top