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

  1. What does the stat command tell me?
  2. Use stat with multiple files
  3. Symbolic links
  4. Report Terse
  5. Custom output format

What does the stat command tell me?

Take a look at the following stat command:

 stat ana.h 

How to use the stat command on Linux Picture 1

The information we have is:

  1. File: The name of the file.
  2. Size: The file size is in bytes.
  3. Blocks: The number of file system blocks required, to store on the hard drive.
  4. IO Block: The size of the file system block.
  5. File type: The type of object that the metadata describes.
  6. Device: The device number is in hexadecimal and decimal.
  7. Inode: Number of inode. That is the ID number of this inode.
  8. Links: This figure shows how many hard links point to this file.
  9. Access: File permissions are displayed in traditional read, write, and execute formats.
  10. Uid: The user ID and account name of the owner.
  11. Gid: The group ID and the account name of the owner.
  12. Access: Mark the access time.
  13. Modify: Mark the modified time. This is the time when the file content was last modified.
  14. Change: Mark the time of change. This is the time when the file's properties or contents were last modified.
  15. 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.

How to use the stat command on Linux Picture 2

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:

  1. File: The name of the file.
  2. ID: The file system ID in hexadecimal notation.
  3. Namelen: The maximum length allowed for file names.
  4. Type: File system type.
  5. Block size: The amount of data required to read for optimal data transfer rate.
  6. Fundamental block size : The size of each file system block.
  7. Blocks:
    1. Total: The total number of all blocks in the file system.
    2. Free: The number of free blocks in the file system.
    3. Available: Number of free blocks available to normal users (not root).
  8. Inodes:
    1. Total: The total number of inodes in the file system.
    2. 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.

3.8 ★ | 8 Vote

May be interested

  • How to limit access to su command in LinuxHow to limit access to su command in Linux
    if you have added linux to your data center or are just using a single linux machine for your business, you need to make sure it is as secure as possible.
  • 12 things Linux is easier to do in the command line than graphical software12 things Linux is easier to do in the command line than graphical software
    graphical user interfaces (guis) were invented to make life easier for computer users, so it's very common to use them to perform most everyday tasks.
  • 11 df commands in Linux (with example)11 df commands in Linux (with example)
    with the df command in linux, you can easily check your system for details about disk space and file size. these 12 examples of df commands will show you how to do them in detail.
  • How to use the Linux command line on Android with TermuxHow to use the Linux command line on Android with Termux
    android is a very operating system 'capacity with more and more desktop accessibility applications. however, sometimes you want to make some things on android that can be as easy as desktop. fortunately, you can use the termux tool, which builds on the existing infrastructure and provides a command line environment that allows you to install real linux applications on your android device.
  • Instructions for using find command in LinuxInstructions for using find command in Linux
    the find command is one of the most important and handy commands on a linux system. as its name suggests, the command can find files on a linux pc based on a variety of conditions and variables you set.
  • How to use the history command in LinuxHow to use the history command in Linux
    as you spend more and more time in terminal sessions, you will constantly find new commands that make everyday tasks more efficient. the gnu history command is one of them.
  • Instructions for using pstree command on LinuxInstructions for using pstree command on Linux
    pstree is a powerful and useful command to display processes running in linux. like the ps command, it shows all the processes that are currently active on your login system. the main difference is that when running the pstree command, processes are organized into tree sorting instead of lists like using the ps command.
  • The dd command in Linux, How to use the dd commandThe dd command in Linux, How to use the dd command
    dd is a command line utility for unix-like and unix operating systems, with the main purpose of converting and copying files.
  • Use the Top command to see the processes running in LinuxUse the Top command to see the processes running in Linux
    the top command in linux is used to display all processes running in the linux environment. this tutorial shows you how to use the top command through explaining the various parameters available and the content they display.
  • How to use the dmesg command in LinuxHow to use the dmesg command in Linux
    the dmesg command has been used to troubleshoot server and linux desktops for decades. it's time to start using this handy command!