Stat Command - Display File Information on the Command Line
While working on the Linux command line you interact with a lot of different files. Each of these files has attributes attached to them. These attributes contain details about the files size, permissions, modification time, and an assortment of other information. There are a multitude of reasons that you might need to the print the attributes of a file. This is where the Linux stat command comes in.
What is the Stat Command?
The stat command is part of the GNU Core Utilities which comes pre-installed with almost all UNIX and Linux systems. It is used to print the attributes of a file to standard output (STDOUT). In this article we will discuss how to use the stat command, using it's options and formatting the output.
Basic Syntax of Stat Command
The basic syntax that the stat command uses a simple structure. You only need to provide the name of the file as an argument to the command. The general syntax is:
stat [OPTION] ...filename...
Now, let us see how the actual command works in a real situation. Let's use it to display information about myfile.txt.
Passing a filename as an argument is all that is required. However, there are several options available to expand the usefulness of the utility.
Stat Command Output Explained
The screenshot above is typical of the output you would see using the stat command. Let's break down the output, label by label.
- File - Name of the file
- Size - Size of the file in kb
- Blocks - Number of blocks allocated on disk
- IO Block - IO Block size
- File type - (i.e. regular file, symbolic link)
- Device - Major and minor devices number concatenated
- Inode - Files inode number
- Links - Number of links to the file on the filesystem
- Access - Permissions written in binary reference and basic
- UID - User ID of the file owner
- GID - Group ID of the group owner
- Context - SELinux context
- Access - Last time the file was accessed
- Modify - Last time the file's contents have been changed
- Change - Last time the file's attributes have been changed
- Birth - Time file was created
Stat Command Options
Now that you know how to fetch the basic information of any file, it is time to see what other options the valuable stat command provides. The command could be used to get a specific piece of information instead of all the information. It can also be used to fetch details about a file system instead of a file. Additionally, the output could be modified into a more readable format for ease of use in other programs. Here are some of the options you could try your hands on.
Show Filesystem Information
You can use the -f (--filesystem) option to pull information about a filesystem instead of a file.
[[email protected] ~]$ stat -f /tmp File: "/tmp" ID: 0 Namelen: 255 Type: tmpfs Block size: 4096 Fundamental block size: 4096 Blocks: Total: 2037690 Free: 2037679 Available: 2037679 Inodes: Total: 2037690 Free: 2037652
Follow Symbolic Link / Dereference
If you run the stat command on a link, it will give you the information about that symlink, not the file it references.
[[email protected] ~]$ stat myfilelink File: myfilelink -> myfile.txt Size: 10 Blocks: 0 IO Block: 4096 symbolic link Device: fd02h/64770d Inode: 13670759 Links: 1 ...OUTPUT TRUNCATED...
Using the -L (--dereference) option will tell stat to follow the link and give you the information on the file the link references.
[[email protected] ~]$ stat -L myfilelink File: myfilelink Size: 890 Blocks: 8 IO Block: 4096 regular file Device: fd02h/64770d Inode: 13640825 Links: 1 ...OUTPUT TRUNCATED...
Use Terse Output / Remove Labels
The -t (--terse) output is all the same information, but without the labels and formatting. This is useful when using the output in scripts that need to parse specific information.
[[email protected] ~]$ stat -t myfile.txt myfile.txt 890 8 81b4 1000 1000 fd02 13640825 1 0 0 1572312639 1568509270 1572312646 1568509270 4096 unconfined_u:object_r:user_home_t:s0
Format The Stat Command Output
The last option is -c (--format). This allows you to use format sequences to show just the exact information you need.
The syntax for using formatting is using the -c switch followed by the desired sequence and then the filename (or filesystem name) as an argument.
Here is an example of formatting the output to show only the name of the file owner.
[[email protected] ~]$ stat -c %U myfile.txt savona
Formatting Output of the Stat Command
There are many different formats you could use to display the information on your file. Here are some examples of the most common format sequences.
Display the Inode Number of a File
[[email protected] ~]$ stat -c %i myfile.txt 13640825
Show the File Type of a File
[[email protected] ~]$ stat -c %F myfile.txt regular file
Same example with a symbolic link to show the difference:
[[email protected] ~]$ stat -c %F myfilelink symbolic link
Show Filesystem Type
[[email protected] ~]$ stat -f -c %T /vault smb2
Combining Format Sequences
You can combine any number of formatting sequences in a single command to get the exact information you want. Here we use a few different sequences to get the owner name, group name of owner and permissions of myfile.txt.
stat -c "%U %G %A" myfile.txt
NOTE: If you put spaces between your sequences, you will have to wrap it in quotes.
You can even use a custom seperator. For example, let's say you wanted to dump the same information seperated by a comma. This may be useful if you want to copy this data into a spreadsheet as comma separated (CSV) format.
[[email protected] ~]$ stat -c "%U,%G,%A" myfile.txt savona,savona,-rw-rw-r--
Another example using colons:
[[email protected] ~]$ stat -c "%U:%G:%A" myfile.txt savona:savona:-rw-rw-r--
For a full list of valid formatting sequences see the man page (linked in the resources section) or use stat --help.
The examples above should guide you towards a better understanding of the stat command. With several command line options and formats out there, it is best if you use these examples and apply them to the files on your system to get more hands-on knowledge. Continued learning might help you fetch the exact file or file system details efficiently in real-time operations.
Resources and Links
This site uses Akismet to reduce spam. Learn how your comment data is processed.