stat Command in Linux

Introduction to the Linux stat Command

This tutorial will guide you through using the Linux stat command. You'll learn how to retrieve comprehensive information about files and directories. This includes essential metadata like permissions, ownership details, and various timestamps. We will cover the function and syntax of the stat command, accompanied by practical examples that demonstrate analyzing file and directory attributes. This includes learning how to access file metadata, interpret file permissions and ownership, and utilize available options to tailor the stat command's output for system administration tasks.

Understanding the Purpose and Syntax of the stat Command for System Administrators

This section will explain the purpose and syntax of the stat command within a Linux environment. The stat command is an invaluable tool for any systemadmin, used to display detailed information about files or directories. This includes critical metadata such as permissions, ownership, timestamps and other vital system information.

To utilize the stat command, execute the following in your terminal:

stat [options] <file_or_directory>

Frequently used options for the stat command include:

  • -c or --format=<format>: This allows you to define a custom output format string.
  • -L or --dereference: By following symbolic links, this option displays information about the target file or directory.
  • -f or --file-system: Instead of the file itself, this displays information about the file system.

Below is an example of how to use the stat command to obtain information regarding a specific file:

stat ~/project/example.txt

Example output:

  File: '/home/labex/project/example.txt'
  Size: 0         	Blocks: 0          IO Block: 4096   regular empty file
Device: 801h/2049d	Inode: 131074      Links: 1
Access: (0644/-rw-r--r--)  Uid: (1000/labex)   Gid: (1000/labex)
Access: 2023-04-11 12:34:56.789012345 +0000
Modify: 2023-04-11 12:34:56.789012345 +0000
Change: 2023-04-11 12:34:56.789012345 +0000
 Birth: -

The output above provides detailed information concerning the file including its size, file permissions, ownership details and timestamps.

Retrieving File Metadata Using the stat Command in Linux

In this step, you will discover how to use the stat command to retrieve detailed metadata about specific files and directories, which is crucial for any systemadmin managing a Linux system.

First, let's create a new file within the ~/project directory:

touch ~/project/example.txt

Now, let's use the stat command to retrieve the metadata for this newly created file:

stat ~/project/example.txt

Example output:

  File: '/home/labex/project/example.txt'
  Size: 0         	Blocks: 0          IO Block: 4096   regular empty file
Device: 801h/2049d	Inode: 131075      Links: 1
Access: (0644/-rw-r--r--)  Uid: (1000/labex)   Gid: (1000/labex)
Access: 2023-04-11 12:34:56.789012345 +0000
Modify: 2023-04-11 12:34:56.789012345 +0000
Change: 2023-04-11 12:34:56.789012345 +0000
 Birth: -

The output delivers a large amount of information about the file, including:

  • The complete file path and file name
  • The total file size and associated block size
  • The specific device and inode numbers
  • The defined file permissions, assigned owner, and the assigned group
  • The file access time, modification time, and change timestamps

You are also able to use the -c or --format option to customize how the stat command's output is formatted. As an example, you can display only the file size and last modification time using the command below:

stat -c '%s %y' ~/project/example.txt

Example output:

0 2023-04-11 12:34:56.789012345 +0000

This lets you extract only the specific metadata fields that are most useful to you.

Analyzing File Permissions and Ownership using the stat Command

This section will explain how to analyze file permissions and ownership using the stat command.

Let's begin by creating a new file within the ~/project directory. This is a common task for any systemadmin.

touch ~/project/example.txt

Now, use the stat command to inspect the file's permissions and ownership information:

stat -c '%A %u %G' ~/project/example.txt

Example output:

-rw-r--r-- 1000 1000

The following output is displayed:

  • The file permissions: -rw-r--r-- (read and write for the file owner, read-only access for the group and other users)
  • The User ID (UID) of the file owner: 1000 (which corresponds to the labex user within the system)
  • The Group ID (GID) of the file's assigned group: 1000 (this also corresponds to the labex group)

Using the long-form options will also return similar information:

stat --format='%A %U %G' ~/project/example.txt

Example output:

-rw-r--r-- labex labex

Here the same information is shown, but using the actual user and group names instead of the numeric IDs.

Understanding file permissions and ownership is crucial for controlling file and directory access. The stat command is a fast and easy way to inspect this specific metadata in a Linux system.

Summary

In this tutorial, you have learned the purpose and syntax of the Linux stat command, which is a valuable tool to display detailed file and directory information including metadata such as permissions, ownership and timestamps. This tutorial also explained how to use the stat command to retrieve file metadata and analyze file permissions and ownership information. The key concepts covered in this tutorial include understanding the basic stat command usage, how to interpret command output, and leveraging various options to customize the displayed information.

400+ Linux Commands