8 Stat Command Examples in Linux
In Linux, there always seems to be an ingenious way of getting things done. For any task, there’s always more than one command-line utility to execute it in a better way. The Linux stat command is a command-line tool used to display detailed information about a file. In this guide, we highlight 8 stat command usages in Linux. This works across all Linux distributions.
Linux stat command
Think of stat command as a better version of the ls -l command. While the -l flag provides more details about files such as file ownership and permissions, The stat command goes deeper under the hood and provides a wealth of information about a file.
The syntax for Linux stat command is as shown:
$ stat [OPTION] filename
1) Stat command with no arguments
In its simplest form — without any parameters — the stat command displays the default output. This includes file size and type, device type, inode number, UID, GID, number of links and access/modification dates of the file.
For example, to view the file details of a file residing in the current home directory, execute:
$ stat file1.txt
Let’s flesh out the output:
- File : This shows the name of the file.
- Size : Size of the file in bytes.
- Block : Number of blocks allocated to the file.
- IO Block : This is the byte size of every block.
- Device : The device number in hexadecimal or decimal format.
- Inode : This is the inode number of the file.
- Links : Number of hard links associated with the file.
- Access : File permissions either in symbolic or numeric format.
- Uid : User ID & name of the owner.
- Gid : Group ID & name of the owner.
- Context : SeLinux security context
- File type : Shows what type the file is (Whether a regular file, symbolic link etc).
- Access : Shows the last time the file was accessed.
- Modify : Shows the last time the contents of the file were changed.
- Change : Shows the last time a file’s metadata e.g permissions & ownership was changed.
2) View information about multiple files
You can view a detailed report on multiple files by specifying the files on the command line one after the other as shown.
$ stat file1.txt file2.pdf
3) Display file system status
You can check the filesystem status where the file is sitting on by using the -f option as shown. This gives you the block size, total & available memory to mention just a few attributes.
$ stat -f /home
4) Display information in terse form
The -t option is used to display information in a terse format as shown:
$ stat -t file1.txt
5) Enable following of symbolic links
Usually, if you run the stat command on a symbolic link, it will only give you information about the link, and not the file that the link points to. Take for example the /usr/share/zoneinfo/America/Cayman symbolic link.
$ stat /usr/share/zoneinfo/America/Cayman
In the example above, the symbolic link /usr/share/zoneinfo/America/Cayman points to ( ->) Panama. The symbolic link is only 6 bytes.
To get information on the file that the links points to, use the -L option also known as the dereference option.
$ stat -L /usr/share/zoneinfo/America/Cayman
This now displays information about the file and not the link, even though the output suggests it’s the link. This is because we passed it as an argument with the stat command.
6) Format sequencing
From previous examples, we have seen that stat command prints out a barrage of information on the terminal. If you want specific information, you can custom the output using a format sequence to provide exactly what you need and leave other details.
Popular expressions used to custom the output include –printf or –format option
For example, to display inode of a file only, use the %i format sequence as shown. The n operand print a new line.
$ stat --printf='%in' file1.txt
To display access rights and uid (User ID) use the %a and %u format sequences.
$ stat --printf='%a:%un' file1.txt
The –format option prints out a new line without requiring an additional operand.
$ stat --format='%a:%F' file1.txt
Here’s a complete list of the format sequences that you can use:
- %a Displays the access rights in octal format.
- %A Displays the access rights in a human readable format.
- %b This is the number of blocks allocated (see %B).
- %B the size in bytes of each block reported by %b.
- %C Shows the SELinux security context string.
- %d Displays the device number in a decimal format.
- %D The device number in hexadecimal format.
- %f Displays the raw mode in hexadecimal.
- %F Displays the file type.
- %g Prints the group ID of owner.
- %G Prints the group name of owner.
- %h Displays the number of hard links.
- %i Prints out the inode number.
- %m Prints the mount point.
- %n Displays the file name of the file
- %N Shows quoted file name with dereference if symbolic link
- %o Prints the optimal I/O transfer size hint.
- %s total size, in bytes.
- %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 Shows the user ID of owner.
- %U Prints the username of owner.
- %w Reveals the time of file birth, human-readable; — if unknown.
- %W Prints the time of file birth, seconds since Epoch; 0 if unknown.
- %x The time of last access, human-readable.
- %X The time of last access, seconds since Epoch.
- %y Displays the last time of last modification, human-readable.
- %Y Prints the time of last modification, seconds since Epoch.
- %z This is the time of last change, human-readable.
- %Z The time of last change, seconds since Epoch.
7) Getting help with stat command
For more command options , use the –help option with stat command as shown.
$ stat --help
You can also visit the man pages of stat command, run
$ man stat
8) Checking the version of stat
Finally to check the version of stat command, run the command:
$ stat --version
That’s what we had in store for you in this guide. As you can see, the stat command goes over and above what the ls command gives you and is ideal in gathering detailed information about a file or file system.
Also Read : 14 Useful ‘ls’ Command Examples in Linux
The post 8 Stat Command Examples in Linux first appeared on LinuxTechi.