How to use the command 'stat' (with examples)

How to use the command 'stat' (with examples)

The ‘stat’ command is used to display file and filesystem information. It provides various properties of a specific file such as size, permissions, creation and access dates, and more. The command can also provide information about the filesystem where a file is located.

Use case 1: Display properties about a specific file

Code:

stat path/to/file

Motivation: This use case is useful when you need to retrieve detailed information about a specific file. By running the ‘stat’ command followed by the path to the file, you can quickly obtain information such as file size, permissions, timestamps, and more.

Explanation:

  • ‘stat’: The command itself.
  • ‘path/to/file’: The path to the file for which you want to display properties.

Example output:

  File: path/to/file
  Size: 1024     	Blocks: 8          IO Block: 4096   regular file
Device: 801h/2049d	Inode: 131075      Links: 1
Access: (0664/-rw-rw-r--)  Uid: (  501/  user)   Gid: (  20/   group)
Access: 2022-01-01 09:00:00.000000000 +0000
Modify: 2022-01-01 09:00:00.000000000 +0000
Change: 2022-01-01 09:00:00.000000000 +0000
 Birth: 2022-01-01 09:00:00.000000000 +0000

Use case 2: Display properties about a specific file without labels

Code:

stat --terse path/to/file

Motivation: In some cases, you may prefer a more concise output without labels. This use case allows you to display the file properties without any additional information, which can be beneficial when you only need the raw values.

Explanation:

  • ‘stat’: The command itself.
  • ‘–terse’: An option to display the properties without labels.
  • ‘path/to/file’: The path to the file for which you want to display the properties.

Example output:

path/to/file 1024 8 801 1 0664 501 20 2022-01-01 09:00:00.000000000 +0000 2022-01-01 09:00:00.000000000 +0000 2022-01-01 09:00:00.000000000 +0000 2022-01-01 09:00:00.000000000 +0000

Use case 3: Display information about the filesystem where a specific file is located

Code:

stat --file-system path/to/file

Motivation: When working with files, it can be helpful to know the filesystem on which they reside. This use case allows you to obtain information about the filesystem that contains the specified file.

Explanation:

  • ‘stat’: The command itself.
  • ‘–file-system’: An option to display information about the filesystem.
  • ‘path/to/file’: The path to the file for which you want to display the information.

Example output:

  File: path/to/file
    ID: 0        Namelen: 255     Type: tmpfs
Block size: 4096       Fundamental block size: 4096
Blocks: Total: 88585842   Free: 88394266   Available: 88394256
Inodes: Total: 88585842   Free: 88394248

Use case 4: Show only octal file permissions

Code:

stat --format="%a %n" path/to/file

Motivation: If you only need to retrieve the octal file permissions of a specific file, this use case is handy. By using the ‘–format’ option, you can specify the desired output format.

Explanation:

  • ‘stat’: The command itself.
  • ‘–format="%a %n"’: An option to format the output. ‘%a’ represents the octal file permissions, and ‘%n’ represents the file name.
  • ‘path/to/file’: The path to the file for which you want to display the permissions.

Example output:

644 path/to/file

Use case 5: Show the owner and group of a specific file

Code:

stat --format="%U %G" path/to/file

Motivation: Knowing the owner and group of a file is crucial for managing permissions and access control. This use case focuses on displaying the owner and group of a specific file.

Explanation:

  • ‘stat’: The command itself.
  • ‘–format="%U %G"’: An option to format the output. ‘%U’ represents the owner, and ‘%G’ represents the group.
  • ‘path/to/file’: The path to the file for which you want to display the owner and group.

Example output:

user group

Use case 6: Show the size of a specific file in bytes

Code:

stat --format="%s %n" path/to/file

Motivation: Determining the size of a file in bytes is often needed for various operations or calculations. This use case allows you to retrieve the size of a specific file in bytes.

Explanation:

  • ‘stat’: The command itself.
  • ‘–format="%s %n"’: An option to format the output. ‘%s’ represents the size in bytes, and ‘%n’ represents the file name.
  • ‘path/to/file’: The path to the file for which you want to display the size.

Example output:

1024 path/to/file

Conclusion:

The ‘stat’ command is a versatile tool for retrieving detailed information about files and filesystems. Whether you need to display file properties, file permissions, owner and group, or filesystem information, the ‘stat’ command provides a convenient way to obtain these details.

Related Posts

How to use the command qm resume (with examples)

How to use the command qm resume (with examples)

This article will demonstrate the different use cases of the qm resume command, which is used to resume a virtual machine in Proxmox Virtual Environment (PVE).

Read More
How to use the command 'git info' (with examples)

How to use the command 'git info' (with examples)

Git is a popular version control system that allows developers to keep track of changes in their code.

Read More
How to use the command "lighthouse" (with examples)

How to use the command "lighthouse" (with examples)

The lighthouse command is a powerful tool for analyzing web applications and web pages.

Read More