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 fold Command: Wrap Long Lines at a Fixed Width

Linux fold Command: Wrap Long Lines at a Fixed Width

Learn how to wrap Long Lines at a Fixed Width with the Linux fold command, including practical examples, key options, and important precautions.

Linux Administration Memo: Essential Commands and System Checks

Linux Administration Memo: Essential Commands and System Checks

A compact Linux administration reference for checking system information, processes, storage, networking, users, permissions, services, and logs.

Linux Tutorial / Inspect, Pause, and Stop Processes

Linux Tutorial / Inspect, Pause, and Stop Processes

Inspect Linux processes with ps, top, and pgrep, then safely pause, resume, and terminate a practice process while distinguishing shell jobs from services.

Linux Tutorial / Linux Terminal and Shell Basics: Command Syntax and Finding Help

Linux Tutorial / Linux Terminal and Shell Basics: Command Syntax and Finding Help

Learn the difference between a terminal and a shell, read command syntax and output, and use Bash help tools through a short hands-on exercise.

Linux apk Command: Manage Packages on Alpine Linux

Linux apk Command: Manage Packages on Alpine Linux

Learn how to manage Packages on Alpine Linux with the Linux apk command, including practical examples, key options, and important precautions.

Linux dpkg Command: Inspect and Install Local DEB Packages

Linux dpkg Command: Inspect and Install Local DEB Packages

Learn how to inspect and Install Local DEB Packages with the Linux dpkg command, including practical examples, key options, and important precautions.

Linux passwd Command: Change a User Password

Linux passwd Command: Change a User Password

Learn how to change a User Password with the Linux passwd 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 ssh-copy-id Command: Install a Public Key on a Remote Account

Linux ssh-copy-id Command: Install a Public Key on a Remote Account

Learn how to install a Public Key on a Remote Account with the Linux ssh-copy-id command, including practical examples, key options, and important precautions.

Linux fdisk Command: Inspect and Edit Disk Partitions

Linux fdisk Command: Inspect and Edit Disk Partitions

Learn how to inspect and Edit Disk Partitions with the Linux fdisk command, including practical examples, key options, and important precautions.