Intro To 'stat' Command In Linux
2023-06-15 - By Robert Elder
I use the 'stat' command to show file system attributes of files on my system:
stat README.txt
File: README.txt
Size: 13 Blocks: 8 IO Block: 4096 regular file
Device: 10303h/66307d Inode: 1837700 Links: 1
Access: (0664/-rw-rw-r--) Uid: ( 1000/ robert) Gid: ( 1000/ robert)
Access: 2023-04-10 22:14:51.642526898 -0400
Modify: 2023-04-10 22:14:51.642526898 -0400
Change: 2023-04-10 22:14:51.642526898 -0400
Birth: -
The 'stat' Command Vs. The 'ls' Command
Many of the pieces of information that are shown by the 'stat' command are also shown by the ls command:
ls -li README.txt
1837700 -rw-rw-r-- 1 robert robert 13 Apr 10 22:14 README.txt
When compared to the 'ls' command, the output from the 'stat' command focuses mainly on file system details like inodes, block sizes, and verbose descriptions of the file's permissions and timestamp attributes.
File System Information
You can use the '-f' flag to show information about a file's host file system:
stat -f README.txt
File: "README.txt"
ID: ae86a3cf56bc3662 Namelen: 255 Type: ext2/ext3
Block size: 4096 Fundamental block size: 4096
Blocks: Total: 23883881 Free: 5196751 Available: 3971958
Inodes: Total: 6111232 Free: 5582899
Custom Format Strings
The 'stat' command allows you to specify a format string to selectively output only the information that you need. Here are several different types of files that we can use for this example:
ls -ld README.txt /dev/null /usr /run/initctl
crw-rw-rw- 1 root root 1, 3 Jun 10 10:35 /dev/null
-rw-rw-r-- 1 robert robert 13 Jun 10 13:44 README.txt
prw------- 1 root root 0 Jun 10 10:35 /run/initctl
drwxr-xr-x 14 root root 4096 Feb 9 2021 /usr
This command prints the file type of each argument:
stat -c "'%n' is a %F." README.txt /dev/null /usr /run/initctl
'README.txt' is a regular file.
'/dev/null' is a character special file.
'/usr' is a directory.
'/run/initctl' is a fifo.
This command prints the file size and the number of blocks that each file uses:
stat -c "'%n' has size %s and uses %b blocks." README.txt /dev/null /usr /run/initctl
'README.txt' has size 13 and uses 8 blocks.
'/dev/null' has size 0 and uses 0 blocks.
'/usr' has size 4096 and uses 8 blocks.
'/run/initctl' has size 0 and uses 0 blocks.
This command reports the access rights in both human readable and octal format:
stat -c "'%n' has access rights: %A (%a in octal)." README.txt /dev/null /usr /run/initctl
'README.txt' has access rights: -rw-rw-r-- (664 in octal).
'/dev/null' has access rights: crw-rw-rw- (666 in octal).
'/usr' has access rights: drwxr-xr-x (755 in octal).
'/run/initctl' has access rights: prw------- (600 in octal).
And that's why the 'stat' command is my favourite Linux command.
Intro To 'stty' Command In Linux
Published 2023-10-04 |
$1.00 CAD |
Intro To 'nproc' Command In Linux
Published 2023-07-15 |
Intro To 'comm' Command In Linux
Published 2023-09-06 |
How To Force The 'true' Command To Return 'false'
Published 2023-07-09 |
A Surprisingly Common Mistake Involving Wildcards & The Find Command
Published 2020-01-21 |
A Guide to Recording 660FPS Video On A $6 Raspberry Pi Camera
Published 2019-08-01 |
Intro To 'chroot' Command In Linux
Published 2023-06-23 |
Join My Mailing List Privacy Policy |
Why Bother Subscribing?
|