- Linux Windows Install Setup Configuration Project
- Using stat command to display file or filesystem status on Linux system.
- Using stat command to display file or filesystem status on Linux system.
- Linux command name: stat
- The step by step example using stat command on Linux Fedora Core machine.
- Check file status.
- stat
- —terse is equivalent to the following FORMAT:
- —terse —file-system is equivalent to the following FORMAT:
- REPORTING BUGS
- COPYRIGHT
- SEE ALSO
- AUTHOR
- Linux Stat Command Examples
- Syntax of Stat Command
- Display Linux File Status Using Stat Command
- Display the Information About the File System
- Enable or Follow the Symbolic Links
- Display Output in a Customized Format
- Display Output in the Terse Format
- Conclusion
- About the author
- Karim Buzdar
Linux Windows Install Setup Configuration Project
Using stat command to display file or filesystem status on Linux system.
Using stat command to display file or filesystem status on Linux system.
The example of using Linux stat command on bash shell to display file or filesystem status on Linux Fedora Core machine.
Linux command name: stat
The stat command can be use to check the single file status ( like file properties on Windows) or check the filesystem status on Linux machine. The example below show the step by step on using the stat command to check file status and check filesystem status.
Display file or filesystem status.
[root@fedora ~]# type -t stat
[root@fedora ~]# type -p stat
The stat command will gather the information about a file or filesystem status. This command is useful for checking file permission, inode and symbolic link, for example:
[root@fedora ~]# stat install.log
Size: 61891 Blocks: 144 IO Block: 4096 regular file
Device: 900h/2304d Inode: 910338 Links: 1
Access: (0644/-rw-r—r—) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2005-05-05 12:50:11.000000000 +0800
Modify: 2005-05-05 15:44:16.000000000 +0800
Change: 2005-05-05 15:44:16.000000000 +0800
The output of this command show size, filetype, permission, user id, group id and so on… for the install.log file.
The following are some of the flags and arguments that can be used for the stat command:
-f, —filesystem display filesystem status instead of file status
-c —format=FORMAT use the specified FORMAT instead of the default
-L, —dereference follow links
-Z, —context print the security context
-t, —terse print the information in terse form
—help display this help and exit
—version output version information and exit
The step by step example using stat command on Linux Fedora Core machine.
Type in ‘ stat ‘ on the Linux shell prompt and press [Enter] key to execute the command.
[root@fedora ~]# stat
Try `stat —help’ for more information.
From the result above, the ‘ stat ‘ command that we issue on the command prompt is not complete.
The output of the above command suggest that we try to issue ‘ stat —help ‘ command to get more information on how to use the stat command.
[root@fedora ~]# stat —help
Display file or filesystem status.
-f, —filesystem display filesystem status instead of file status
-c —format=FORMAT use the specified FORMAT instead of the default
-L, —dereference follow links
-Z, —context print the security context
-t, —terse print the information in terse form
—help display this help and exit
—version output version information and exit
The valid format sequences for files (without —filesystem):
%A Access rights in human readable form
%B The size in bytes of each block reported by `%b’
%b Number of blocks allocated (see %B)
%d Device number in decimal
%N Quoted File name with dereference if symbolic link
%T Minor device type in hex
%t Major device type in hex
%X Time of last access as seconds since Epoch
%Y Time of last modification as seconds since Epoch
%y Time of last modification
%Z Time of last change as 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
%C — Security context in SELinux
%l Maximum length of filenames
%s Optimal transfer block size
%T Type in human readable form
From the help file for the command stat above , there is some options to use with the stat command.
Check file status.
Try to issue ‘ stat ‘ command and then put name of the file we wish to check the status.
[root@fedora ~]# stat install.log
Size: 71590 Blocks: 160 IO Block: 4096 regular file
Device: fd00h/64768d Inode: 940994 Links: 1
Access: (0644/-rw-r—r—) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2005-10-22 22:41:22.000000000 -0400
Modify: 2005-10-23 03:22:34.000000000 -0400
Change: 2005-10-23 03:22:34.000000000 -0400
Then try to issue the same command and now with the -f option to display filesystem status instead of file status.
[root@fedora ~]# stat -f install.log
ID: 0 Namelen: 255 Type: ext2/ext3
Blocks: Total: 2388558 Free: 593795 Available: 470506 Size: 4096
Inodes: Total: 2466048 Free: 2126726
From the example above the command stat can be use to check the file or filesystem status, this quite useful is you want to check the size of file, file IO block, file Inode number, number of link and access permission granted to the file.
stat — display file or filesystem status
for more information on stat command:
Step-by-step how to procedure above tested on:
Operating System: GNU/Linux Fedora Core 4
Kernel Release: 2.6.11-1.1369_FC4
Kernel Version: #1 Thu Jun 2 22:55:56 EDT 2005
Machine Hardware: i686
Machine Processor: i686
Hardware Platform: i386
Shell: GNU bash, version 3.00.16(1)-release (i386-redhat-linux-gnu)
Installation Type: Full Installation (Custom)
Keywords: using stat command, stat command, linux stat command, display file status, filesystem status, check inode, check file ststus, check file permission, check symbolic link, check inode number.
stat
Mandatory arguments to long options are mandatory for short options too.
display file system status instead of file status
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
like —format, but interpret backslash escapes, and do not output a mandatory trailing newline; if you want a newline, include \n in FORMAT
print the information in terse form
display this help and exit
output version information and exit
The MODE argument of —cached can be: always, never, or default. ‘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.
The valid format sequences for files (without —file-system):
permission bits in octal (note ‘#’ and ‘0’ printf flags)
permission bits and file type in human readable form
number of blocks allocated (see %B)
the size in bytes of each block reported by %b
SELinux security context string
device number in decimal (st_dev)
device number in hex (st_dev)
major device number in decimal
minor device number in decimal
quoted file name with dereference if symbolic link
optimal I/O transfer size hint
device type in decimal (st_rdev)
device type in hex (st_rdev)
major device type in decimal, for character/block device special files
minor device type in decimal, for character/block device special files
major device type in hex, for character/block device special files
minor device type in hex, for character/block device special files
time of file birth, human-readable; — if unknown
time of file birth, seconds since Epoch; 0 if unknown
time of last access, human-readable
time of last access, seconds since Epoch
time of last data modification, human-readable
time of last data modification, seconds since Epoch
time of last status change, human-readable
time of last status change, seconds since Epoch
Valid format sequences for file systems:
free blocks available to non-superuser
total data blocks in file system
total file nodes in file system
free file nodes in file system
free blocks in file system
maximum length of filenames
block size (for faster transfers)
fundamental block size (for block counts)
file system type in human readable form
—terse is equivalent to the following FORMAT:
—terse —file-system is equivalent to the following FORMAT:
NOTE: your shell may have its own version of stat, which usually supersedes the version described here. Please refer to your shell’s documentation for details about the options it supports.
REPORTING BUGS
COPYRIGHT
Copyright © 2022 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later .
This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law.
SEE ALSO
stat(2), statfs(2), statx(2) Full documentation or available locally via: info ‘(coreutils) stat invocation’
AUTHOR
Written by Michael Meskes.
Linux Stat Command Examples
The stat command displays the detailed summary of the given file or file system. We will show you in this article the basic use of the stat command with different examples.
Syntax of Stat Command
The basic syntax of the Linux stat command is given below:
More than one file name and options can pass an argument with the stat command that controls the command behavior and results.
Display Linux File Status Using Stat Command
When only given the file name with the stat command, the complete information about file status will display on the terminal. For example, the following file detail can be displayed by using the state command:
Display the Information About the File System
To display the information about system status instead of a file means to check the system status in which the given file resides. In this case, use the option “-f” with the file name which is given below:
The following details about the system status will show on the terminal:
You can also pass the directory or filesystem as an argument instead of passing the file name, which is shown in the following screenshot:
Enable or Follow the Symbolic Links
As we know, Linux supports symbolic links (symlink). When you use the stat command on a symlink, it will display the following about the symlink, but it doesn’t provide the information about the file it points to. You can review the following screenshot:
To follow or enable the symlinks, use the “-L” option with the stat command as follows:
So, by using the above command, the symlink and file information will display on the terminal.
Display Output in a Customized Format
Two different options are available that can be used with the stat command to format the output: -c, –printf=”format” and (–format=”format”).
The –format automatically inserts a new line after each operand’s result. The –printf option interprets the backslash and escape sequences.
Let’s explain with an example. Type the following command to try the –printf option:
The meaning of the format sequences, which are used in the above command given below:
%U – Display the owner name like ‘kbuzdar’
%G – used for group’s owner name such as ‘kbuzdar’
%z – display the time of last change in status in human-readable format
There are more sequences you can use with the –printf option according to your needs.
Display Output in the Terse Format
Using the stat command, you can display the details in the terse format that is mostly useful for parsing. Type the option “-t” or –terse with the stat command to display the information in the terse format as follows:
To explore more options about the stat command, read the manual pages of the stat command by running the following terminal command:
Conclusion
This article gives a brief information about the stat command. We have explained all the basic and important options in this article that will hopefully help you.
About the author
Karim Buzdar
Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications. As an IT engineer and technical author, he writes for various web sites. He blogs at LinuxWays.