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

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

  • Osx
  • December 25, 2023

The ‘stat’ command is used to display file status. It provides information about file properties such as size, permissions, creation and access dates, among others.

Use case 1: Show file properties such as size, permissions, creation and access dates

Code:

stat path/to/file

Motivation: This use case is useful when you need to quickly check various properties of a file, such as its size, permissions, and dates of creation and access.

Explanation:

  • ‘stat’ is the command itself.
  • ‘path/to/file’ is the path to the file you want to check the properties for.

Example output:

  File: path/to/file
  Size: 1024         	Blocks: 8          IO Block: 4096   regular file
Device: 1234         	Inode: 5678        Links: 1
Access: (0644/-rw-r--r--)  Uid: (  501/ user)   Gid: (   20/ group)
Access: 2022-10-27 08:32:15.000000000 -0500
Modify: 2022-10-27 08:32:15.000000000 -0500
Change: 2022-10-27 08:32:15.000000000 -0500
 Birth: 2022-10-27 08:32:15.000000000 -0500

Use case 2: Same as above but verbose (more similar to Linux’s ‘stat’)

Code:

stat -x path/to/file

Motivation: The ‘-x’ option provides a more detailed and verbose output, similar to Linux’s ‘stat’ command. This can be helpful when you need to view additional information about the file.

Explanation:

  • ‘stat’ is the command itself.
  • ‘-x’ is an option that makes the output more detailed and verbose.
  • ‘path/to/file’ is the path to the file you want to check the properties for.

Example output:

  File: "path/to/file"
File type: regular file
Size: 1024         	Blocks: 8          IO Block: 4096   regular file
Device: 1234         	Inode: 5678        Links: 1
Access: (0644/-rw-r--r--)  Uid: (  501/ user)   Gid: (   20/ group)
Access: 2022-10-27 08:32:15.000000000 -0500
Modify: 2022-10-27 08:32:15.000000000 -0500
Change: 2022-10-27 08:32:15.000000000 -0500
 Birth: 2022-10-27 08:32:15.000000000 -0500

Use case 3: Show only octal file permissions

Code:

stat -f %Mp%Lp path/to/file

Motivation: This use case is useful when you specifically need to view the file’s permissions in octal format.

Explanation:

  • ‘stat’ is the command itself.
  • ‘-f’ is an option that specifies the output format.
  • ‘%Mp%Lp’ is the format string that displays the octal file permissions.
  • ‘path/to/file’ is the path to the file you want to check the permissions for.

Example output:

-rw-r--r--

Use case 4: Show owner and group of the file

Code:

stat -f "%Su %Sg" path/to/file

Motivation: This use case is helpful when you need to quickly check the owner and group of a file.

Explanation:

  • ‘stat’ is the command itself.
  • ‘-f’ is an option that specifies the output format.
  • ‘%Su %Sg’ is the format string that displays the owner and group of the file.
  • ‘path/to/file’ is the path to the file you want to check the owner and group for.

Example output:

user group

Use case 5: Show the size of the file in bytes

Code:

stat -f "%z %N" path/to/file

Motivation: This use case is useful when you specifically need to know the size of a file in bytes.

Explanation:

  • ‘stat’ is the command itself.
  • ‘-f’ is an option that specifies the output format.
  • ‘%z’ is the format string that displays the size of the file in bytes.
  • ‘%N’ is the format string that displays the file name.
  • ‘path/to/file’ is the path to the file you want to check the size for.

Example output:

1024 path/to/file

Conclusion:

The ‘stat’ command is a powerful tool for displaying file status and provides useful information about file properties. It can be used to check file permissions, sizes, dates, and more. By using different options and format strings, you can customize the output according to your specific needs.

Tags :

Related Posts

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

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

The ‘mkswap’ command is used to set up a Linux swap area on a device or in a file.

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

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

Jigsaw is a Laravel-based static site builder for PHP. It allows developers to easily create static websites using the Laravel framework.

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

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

Rage is a simple, secure, and modern file encryption tool that allows users to encrypt and decrypt files with small explicit keys, no configuration options, and UNIX-style composability.

Read More