Linux stat Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to use the Linux stat command to retrieve detailed information about files and directories, including their metadata such as permissions, ownership, and timestamps. The lab covers the purpose and syntax of the stat command, as well as practical examples of how to use it to analyze file and directory properties. You will learn how to retrieve file metadata, understand file permissions and ownership, and explore the various options available for customizing the output of the stat command.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) linux/BasicFileOperationsGroup -.-> linux/cat("`File Concatenating`") linux/BasicFileOperationsGroup -.-> linux/ls("`Content Listing`") linux/BasicFileOperationsGroup -.-> linux/touch("`File Creating/Updating`") linux/BasicFileOperationsGroup -.-> linux/chmod("`Permission Modifying`") subgraph Lab Skills linux/cat -.-> lab-422932{{"`Linux stat Command with Practical Examples`"}} linux/ls -.-> lab-422932{{"`Linux stat Command with Practical Examples`"}} linux/touch -.-> lab-422932{{"`Linux stat Command with Practical Examples`"}} linux/chmod -.-> lab-422932{{"`Linux stat Command with Practical Examples`"}} end

Understand the Purpose and Syntax of the stat Command

In this step, you will learn about the purpose and syntax of the stat command in Linux. The stat command is used to display detailed information about a file or directory, including its metadata such as permissions, ownership, timestamps, and more.

To use the stat command, simply run the following command in the terminal:

stat [options] <file_or_directory>

The most commonly used options for the stat command are:

  • -c or --format=<format>: Specify the output format using a custom format string.
  • -L or --dereference: Follow symbolic links and display information about the target file or directory.
  • -f or --file-system: Display information about the file system instead of the file itself.

Here's an example of using the stat command to get information about a 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: -

This output provides detailed information about the file, including its size, permissions, ownership, and timestamps.

Retrieve File Metadata Using the stat Command

In this step, you will learn how to use the stat command to retrieve detailed metadata about files and directories.

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

touch ~/project/example.txt

Now, let's use the stat command to retrieve the metadata for this 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: -

This output provides a wealth of information about the file, including:

  • The file path and name
  • The file size and block size
  • The device and inode numbers
  • The file permissions, owner, and group
  • The access, modification, and change timestamps

You can also use the -c or --format option to customize the output format of the stat command. For example, to display only the file size and modification time, you can use:

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

Example output:

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

This allows you to extract specific metadata fields that are most relevant to your needs.

Analyze File Permissions and Ownership with stat

In this step, you will learn how to use the stat command to analyze file permissions and ownership.

Let's start by creating a new file in the ~/project directory:

touch ~/project/example.txt

Now, let's use the stat command to examine the file's permissions and ownership:

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

Example output:

-rw-r--r-- 1000 1000

This output shows:

  • The file permissions: -rw-r--r-- (read-write for the owner, read-only for the group and others)
  • The user ID (UID) of the file owner: 1000 (which corresponds to the labex user)
  • The group ID (GID) of the file's group: 1000 (which corresponds to the labex group)

You can also use the long-form options to get the same information:

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

Example output:

-rw-r--r-- labex labex

This shows the same information, but with the user and group names instead of the numeric IDs.

Understanding file permissions and ownership is crucial for managing access to files and directories in a Linux system. The stat command provides an easy way to quickly inspect this metadata.

Summary

In this lab, you learned about the purpose and syntax of the Linux stat command, which is used to display detailed information about files and directories, including their metadata such as permissions, ownership, and timestamps. You also learned how to use the stat command to retrieve file metadata and analyze file permissions and ownership. The key points covered in this lab include understanding the basic usage of the stat command, interpreting the output, and leveraging the command's various options to customize the information displayed.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like