Linux Tutorial / File Types and Metadata: ls, file, stat, and readlink

A file's name or extension alone does not establish its type in Linux. ls -l shows its file-system type and basic attributes, file examines content to infer a format, and stat displays metadata such as size, permissions, timestamps, and inode. This lesson explains what each result does—and does not—tell you.

What a Linux file type means

Linux file-system entries include ordinary files, directories, symbolic links, and devices. The first character of an ls -l output row identifies the entry's type. An extension such as .txt or .jpg is merely part of the name; it is not this file-system type indicator.

First character Type Example
- Regular file Document or executable file
d Directory Path containing other entries
l Symbolic link Entry pointing to another path
c, b Character or block device Device entries under /dev
p, s FIFO (named pipe) or socket Entries used for interprocess communication

Specify a target with ls -ld when you want to inspect that entry itself. Running ls -l on a directory can list its contents instead of showing a row for the directory.

ls -ld /etc /etc/passwd

The /etc row normally starts with d, and the /etc/passwd row with -. The remaining columns include permissions, link count, owner, group, size, modification time, and name. Actual values and display details vary by system.

Use file to infer a content format

file examines file-system information and characteristics of a file's content, then gives a human-readable description. Even without an extension, /etc/passwd may be recognized as text.

file /etc/passwd

Output might resemble the following; the wording varies with encoding and implementation.

/etc/passwd: ASCII text

This is a classification based on inspection, not an infallible certification of a file's contents. The detail reported for text encoding or other formats depends on detection rules. For a symbolic link, whether file inspects the link or its target also depends on options and environment; compare file -h link with file -L link.

Use stat to inspect metadata

stat reports information maintained by the file system, not the file's text or other content. It can show size, permissions, owner, inode number, link count, and several timestamps. An inode is a file-system structure used to manage an entry's metadata; it is distinct from the entry's name.

stat /etc/passwd

For a shorter output, GNU stat supports -c with a format string. Here %F is the file type, %s its size in bytes, and %n its name.

stat -c '%F | %s bytes | %n' /etc/passwd

The result is one line like the example below. 1234 is an illustrative size, not a measurement of your file; the actual size and display language can differ.

regular file | 1234 bytes | /etc/passwd

The first field identifies a regular file, the middle field gives its size in bytes, and the last field is the path examined. In the full stat output, Access, Modify, Change, and Birth timestamps have different meanings. In particular, do not read Change as the creation time: it concerns metadata changes. Birth may be unavailable on a particular file system or through a particular tool.

When you encounter a symbolic link

A symbolic link is a separate entry that refers to another path. ls -l commonly shows it as link -> target. readlink link prints the path string stored in the link. If that string is relative, interpret it from the directory containing the link, not your shell's current directory.

The link's metadata differs from the target file's metadata. GNU stat normally examines the link itself; use -L to follow it to the target. Following a broken link can fail. The links lesson covers creating links for practice.

Which command should you choose?

  • Type and basic attributes in a listing: ls -l, or ls -ld to inspect the named entry itself.
  • Likely content format: file.
  • Detailed size, timestamps, inode, and other attributes: stat.
  • Path string stored in a symbolic link: readlink.

For more examples, read the guides to file, stat, and readlink.

Official documentation

Check the file(1) manual, the GNU stat documentation, and the GNU readlink documentation for options and exceptions.

More in This Category
Linux shutdown Command: Schedule a Shutdown or Restart

Linux shutdown Command: Schedule a Shutdown or Restart

Learn how to schedule a Shutdown or Restart with the Linux shutdown command, including practical examples, key options, and important precautions.

Linux dig Command: Query DNS Records in Detail

Linux dig Command: Query DNS Records in Detail

Learn how to query DNS Records in Detail with the Linux dig command, including practical examples, key options, and important precautions.

Linux Tutorial / File Permissions and Safe sudo Use

Linux Tutorial / File Permissions and Safe sudo Use

Learn to read file and directory permissions, practice chmod safely, and understand when to use sudo and how to diagnose access errors.

Linux grep Command: Search Text and Filter Command Output

Linux grep Command: Search Text and Filter Command Output

Learn how to use the Linux grep command to search files for text or regular expressions and filter command output, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux df Command: Check Filesystem Free Space

Linux df Command: Check Filesystem Free Space

Learn how to check Filesystem Free Space with the Linux df command, including practical examples, key options, and important precautions.

Linux which Command: Find Executables in PATH

Linux which Command: Find Executables in PATH

Learn how Linux which searches PATH for executables, shows all matching paths, differs from type and command -v, and should be used carefully in scripts.

Linux ssh-keygen Command: Generate SSH Keys and Inspect Fingerprints

Linux ssh-keygen Command: Generate SSH Keys and Inspect Fingerprints

Learn how to generate SSH Keys and Inspect Fingerprints with the Linux ssh-keygen command, including practical examples, key options, and important precautions.

Linux ls Command: List Files and Directories

Linux ls Command: List Files and Directories

Learn how to use the Linux ls command to list files and directories with permissions, sizes, timestamps, and sorting options, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux whoami Command: Show the Effective User Name

Linux whoami Command: Show the Effective User Name

Learn how to show the Effective User Name with the Linux whoami command, including practical examples, key options, and important precautions.

A History of Linux: From a 1991 Hobby Project to Global Infrastructure

A History of Linux: From a 1991 Hobby Project to Global Infrastructure

Trace the history of Linux from Unix and the GNU Project through the first Linux kernel, distributions, enterprise adoption, cloud computing, containers, and modern infrastructure.