stat

stat linux command cheatsheet by Thamizhiniyan C S

Introduction

The stat command prints details about files and file systems.


Syntax

stat [arguments] [filename]

stat [argument] = "FORMAT" [filename]


Important Flags

FlagDescription

-L, --dereference

follow links

-f, --file-system

display file system status instead of file status

--cached=MODE

specify how to use cached attributes; useful on remote file systems. See MODE below

-c, --format=FORMAT

use the specified FORMAT instead of the default; output a newline after each use of FORMAT

--printf=FORMAT

like --format, but interpret backslash escapes, and do not output a mandatory trailing newline; if you want a newline, include \n in FORMAT

-t, --terse

print the information in terse form

--help

display this help and exit

--version

output version information and exit

MODE values

FlagDescription

always

will use cached attributes if available, while

never

will try to synchronize with the latest attributes, and

default

will leave it up to the underlying file system.


Examples

CommandDescription
stat -f MyFile

show details about the file system the file _MyFile_ is on

stat -f /home

To print information about the file system a particular [directory](https://phoenixnap.com/glossary/what-is-a-directory) is on

stat MyFile MyFile2

To print info on multiple files

stat /usr/local/bin/vim

To print info about the symlink

stat -L stat /usr/local/bin/vim

To get information about the file the symlink points to

stat -t MyFile

To print an one line, space separated output of all details

stat -c=%i MyFile

Prints only the _MyFile_ inode number

stat --printf='%i\n' MyFile

Prints only the _MyFile_ inode number

stat --format=%F:%u:%y MyFile

Prints the file type, user ID, and last modified ti


Customize Output Format

The two arguments used to customize the stat output are:

  • -c (--format). Tells stat to use the specified format string instead of the default format.

  • --printf. Similar to -c, but it allows the use of backslashes in the format string.

The following table lists the most commonly used format operands for files and directories:

OperandFilesFile Systems

%a

Access rights in octal format.

Free blocks available to non-superuser.

%A

Access rights in human-readable form.

/

%b

Number of blocks allocated.

Total data blocks in a file system.

%B

The size in bytes of each block.

/

%c

/

Total data blocks in a file system.

%C

The raw mode in hex.

/

%d

Device number in decimal.

Total file nodes in a file system.

%D

Device number in hex.

/

%f

Raw mode in hex.

Free file nodes in a file system.

%F

File type.

/

%g

Group owner ID.

/

%G

Group owner name.

/

%h

Hard links number.

/

%i

Inode number.

File system ID in hex.

%l

/

Free blocks in the file system.

%m

Mount point.

%n

File name.

File name.

%N

Quoted file name with dereferencing if a symlink is used.

/

%o

Optimal I/O transfer size hint.

/

%s

Total size.

Block size.

%S

/

Fundamental block size.

%t

Major device type in hex.

File system type in hex.

%T

Minor device type in hex.

File system type in human-readable form.

%u

User ID.

/

%U

User name.

/

%w

File birth time, human-readable.

/

%W

File birth time, seconds since Epoch.

/

%x

Last access time, human-readable.

/

%X

Last access time, seconds since Epoch.

/

%y

Last data modification time, human-readable.

/

%Y

Last data modification time, seconds since Epoch.

/

%z

Last status change time, human-readable.

/

%Z

Last status change time, seconds since Epoch.

/

Last updated