How to use the stat command on Linux
The Linux stat
command shows you more details than the ls
command. The following article will show you how to use the stat
command in Linux.
Like ls
, the stat
command has many options. This makes it a great candidate for the use of alias. This makes using it much more convenient and you don't need to remember a complicated set of command line options.
Learn about the stat command on Linux
- What does the stat command tell me?
- Use stat with multiple files
- Symbolic links
- Report Terse
- Custom output format
What does the stat command tell me?
Take a look at the following stat command:
stat ana.h
The information we have is:
- File: The name of the file.
- Size: The file size is in bytes.
- Blocks: The number of file system blocks required, to store on the hard drive.
- IO Block: The size of the file system block.
- File type: The type of object that the metadata describes.
- Device: The device number is in hexadecimal and decimal.
- Inode: Number of inode. That is the ID number of this inode.
- Links: This figure shows how many hard links point to this file.
- Access: File permissions are displayed in traditional read, write, and execute formats.
- Uid: The user ID and account name of the owner.
- Gid: The group ID and the account name of the owner.
- Access: Mark the access time.
- Modify: Mark the modified time. This is the time when the file content was last modified.
- Change: Mark the time of change. This is the time when the file's properties or contents were last modified.
- Birth: Set to show the original creation date of the file, but this is not done in Linux.
Use stat with multiple files
To have stat
reporting on several files at once, pass the file name to the stat
on the command line:
stat ana.h ana.o
To use the stat
on a set of files, use the appropriate pattern. Question mark '?' represent any single character and the asterisk '*' represent any string of characters.
Use stat to report on the file system
The stat
command can report the status of the file systems, as well as the status of the files. The -f (filesystem) option requires the stat
to report on the file system on which the file resides. Note that you can also move a directory like '/' to the stat
instead of the file name.
stat -f ana.c
The stat command tells us:
- File: The name of the file.
- ID: The file system ID in hexadecimal notation.
- Namelen: The maximum length allowed for file names.
- Type: File system type.
- Block size: The amount of data required to read for optimal data transfer rate.
- Fundamental block size : The size of each file system block.
- Blocks:
- Total: The total number of all blocks in the file system.
- Free: The number of free blocks in the file system.
- Available: Number of free blocks available to normal users (not root).
- Inodes:
- Total: The total number of inodes in the file system.
- Free: The number of empty inodes in the file system.
Symbolic links
If you use the stat
on a file that is actually a symbolic link, it will report on that link. If you want the stat to report on the file the link points to, use the -L (dereference) option. The file code.c is a symbolic link to ana.c. Consider it when there is no -L option:
stat code.c
The file name shows code.c pointing to (->) ana.c. File size is only 11 bytes. There are no blocks available for storing this link. The file type is listed as a symbolic link.
Run the command again and add the -L option :
stat -L code.c
The detailed display results for the file are indicated by the symbolic link. But note that the file name is still provided as code.c. This is the name of the link, not the target file.
Report Terse
The -t (terse) option causes the stat to provide a concise summary:
stat -t ana.c
No clues were given. To understand what it means, until you memorize the field sequence, you need to cross-reference this output to the full output.
Custom output format
A better way to get a different set of data from the stat
is to use a custom format. There is a long list of tokens called a format string. Each of these represents a data element. Select the ones you want to have in the output and create a format string. When you call the stat
and pass the format string to it, the output will only include the data components you request.
There are two options for accepting a format string --format and --printf. The difference between them is --printf interprets the escape sequences of type C and it does not automatically add a newline character to the output.
Let's create a format string and convert it to stat
. The format order to use is % n for the file name, % s for the file size, and % F for the file type. Add an escape string to the end of the string to ensure each file is processed on a new line:
"File %n is %s bytes, and is a %Fn"
The example will convert this to a stat
using the --printf option .
stat --printf="File %n is %s bytes, and is a %Fn" code.c ana/ana.?
Reports for each file are listed on a new line.
Custom formats allow you to access more data elements than are included in the standard stat
output.
As you can see, there is plenty of scope for extracting specific data elements that interest you.
You should read it
- How to use the which command in Linux
- 20+ essential Linux security commands
- 14 interesting Linux commands in Terminal
- Basic Linux commands everyone needs to know
- The Cat command in Linux
- How to use the last command in Linux
- How to use ss command on Linux
- 11 uses of ps command in Linux
- Instructions for using zforce command on Linux
- How to limit access to su command in Linux
- 12 things Linux is easier to do in the command line than graphical software
- 11 df commands in Linux (with example)
Maybe you are interested
9 ways to fix the error of not being able to save files as JPEG or PNG in Photoshop
Download standard and beautiful Word file experience initiative cover template
How to use the file search command on Windows, find saved files
How to split sheet into multiple separate Excel files
How to merge PDF files, merge and join multiple PDF files into a single file
How to clean up iPhone junk, delete junk files to free up iPhone storage