Connect with us

Website Tutorials

The Linux Stat Command: Your Key to Advanced File Information

Published

on

The Linux Stat Command: Your Key to Advanced File Information

On Linux systems, everything things is made of up files and folders. The stat command holds the key to unraveling a wealth of information about your files and directories like permissions, ownership, size, or timestamps. Join us on a journey through the depths of file metadata as we delve into the versatile capabilities of the stat command. From basic usage to advanced features, this powerhouse utility empowers Linux enthusiasts, sysadmins, and curious minds to gain unparalleled insights into their file systems.

Introduction:

The stat command is part of the GNU coreutils package. Linux is often used as a generic term to refer to any system that uses the Linux kernel. Though may disagree with referring to the whole stack as just Linux, instead they feel it is more appropriate to use GNU/Linux. This is because, without the applications on top of the kernel, Linux would be a shell of what it is. GNU provides many of the base programs, applications, and libraries that when bundled with the Linux kernel do we get something resembling the the dynamic environment we interact with daily.

Now, armed with this context, let’s explore the stat command in detail. As we unravel its purpose and delve into its functionalities, we’ll uncover how this command becomes an indispensable tool for users navigating the intricacies of file metadata on a Linux system. From unraveling basic file attributes to wielding advanced features, the stat command invites us into the core of Linux’s file-centric universe.

Basic Usage

At its core, the stat command is a versatile tool designed to provide quick and essential information about files and directories. Its basic usage can significantly enhance your ability to navigate and understand the structure of your Linux system.

The basic syntax of the stat command is straightforward. Simply type stat followed by the path to the file or directory you want to inspect. For example:

Advertisement
stat /path/to/file

It is also possible to run the stat command on more than one file using wildcards.

stat /path/to/files/*

Understanding The Basic Output

Here is the basic Output from the stat command on a file called example.txt.
Note: the stat command defaults may vary slightly between different distributions.

File: example.txt
Size: 169486
Blocks: 336
IO Block: 4096 regular file
Device: 0,39
Inode: 1371398
Links: 1
Access: (0644/-rw-r--r--)
Uid: ( 1000/cj)
Gid: ( 1000/cj)
Access: 2024-01-03 14:30:11.220415381 -0500
Modify: 2023-12-18 14:59:33.070992691 -0500
Change: 2023-12-19 14:00:46.540768169 -0500
Birth: 2023-12-18 14:59:33.066992608 -0500

File Information (Lines 1-2):

  • File: The name of the file or directory being analyzed.
  • Size: The size of the file generally will be in bytes.
  • Blocks: The number of disk blocks allocated for the file.
  • IO Block: The block size used for disk I/O operations.
  • Type: Indicates whether the item is a regular file, directory, symbolic link, etc.

Device and Inode Information (Line 3):

  • Device: The device identifier you might encounter either decimal or hexadecimal representations for the device numbers
  • Inode: The inode number, a unique identifier for the file or directory.
  • Links: The number of hard links to the file.

Access Control (Line 4):

  • Access: Displays the file permissions in octal notation and the corresponding human-readable form.
  • Uid: The user ID of the file owner and corresponding username.
  • Gid: The group ID of the group owner and corresponding group name.

Timestamps (Lines 5-8)

  • Access: The last time the file was accessed.
  • Modify: The last time the file content was modified.
  • Change: The last time the file metadata was changed (e.g., permissions).
  • Birth: The creation time of the file. Note: This may be unavailable on some file system

Options And Flags

Let’s look at the options and flags or the stat command.

-L: Tells stat to follow links
-f: display file system status instead of file status
–cached=MODE: specify how to use cached attributes; useful on remote file systems.
-c=FORMAT: use the specified FORMAT instead of the default.
–printf=FORMAT: like –format, but interpret backslash escapes, and do not output a mandatory trailing newline; if you want a newline, add \n in FORMAT
-t: print the information in terse form
–help: display help text and exits
–version: display version information and exits

Mode

The MODE argument of the –cached flag has three options

Advertisement
  • always: will use cached attributes if available
  • never: will try to synchronize with the latest attributes
  • default: will leave it up to the underlying file system.

Format Options

It could be for scripting, readability, user preference, or something else entirely but if the default option isn’t enough then all you have to do is pass the format options to the stat command. Below is the list of the different options for the stat command.

Note: When using the format options make sure to wrap the options in single or double parentheses.

The valid format sequences for files (without –file-system):

%a     permission bits in octal (note '#' and '0' printf flags)
%A     permission bits and file type in human readable form
%b     number of blocks allocated (see %B)
%B     the size in bytes of each block reported by %b
%C     SELinux security context string
%d     device number in decimal (st_dev)
%D     device number in hex (st_dev)
%Hd    major device number in decimal
%Ld    minor device number in decimal%f     raw mode in hex
%F     file type
%g     group ID of owner
%G     group name of owner
%h     number of hard links
%i     inode number
%m     mount point
%n     file name
%N     quoted file name with dereference if symbolic link
%o     optimal I/O transfer size hint
%s     total size, in bytes
%r     device type in decimal (st_rdev)
%R     device type in hex (st_rdev)
%Hr    major device type in decimal, for character/block device special files
%Lr    minor device type in decimal, for character/block device special files
%t     major device type in hex, for character/block device special files
%T     minor device type in hex, for character/block device special files
%u     user ID of owner
%U     user name of owner
%w     time of file birth, human-readable; - if unknown
%W     time of file birth, seconds since Epoch; 0 if unknown
%x     time of last access, human-readable
%X     time of last access, seconds since Epoch
%y     time of last data modification, human-readable
%Y     time of last data modification, seconds since Epoch
%z     time of last status change, human-readable
%Z     time of last status change, seconds since Epoch

Valid format sequences for file systems:

%a     free blocks available to non-superuser
%b     total data blocks in file system
%c     total file nodes in file system
%d     free file nodes in file system
%f     free blocks in file system
%i     file system ID in hex
%l     maximum length of filenames
%n     file name
%s     block size (for faster transfers)
%S     fundamental block size (for block counts)
%t     file system type in hex
%T     file system type in human readable form

–terse is equivalent to the following FORMAT:

%n %s %b %f %u %g %D %i %h %t %T %X %Y %Z %W %o %C

–terse –file-system is equivalent to the following FORMAT:

Advertisement
%n %i %l %t %s %S %b %f %a %c %d

Conclusion:

As Linux enthusiasts, sysadmins, and curious minds delve into the world of file metadata, the stat command emerges as a key ally. The Linux stat command serves as an invaluable tool for users navigating the landscape of file metadata on Linux systems. From its basic usage, providing essential information about files and directories, to its advanced features that allow users to tailor output formats and explore file systems.

Stephen Oduntan is the founder and CEO of SirsteveHQ, one of the fastest growing independent web hosts in Nigeria. Stephen has been working online since 2010 and has over a decade experience in Internet Entrepreneurship.

Continue Reading
Advertisement
Comments

Trending

Copyright © 2024 SirsteveHQ. All Rights Reserved.