5 ‘stat’ Command Examples for Linux Newbies


Linux is a popular operating system that is used by millions of people around world. One of most important tools for Linux users is ‘stat’ command, which provides information about files and directories. In this article, we’ll explore five useful ‘stat’ command examples for Linux newbies.

Getting Basic Information with ‘stat’ Command

The first and simplest use of ‘stat’ command is to get basic information about a file or directory. To do this, simply type following command in your terminal −

stat [filename or directory]

For example, if you want to get information about a file called ‘example.txt’, type following command −

stat example.txt

This will give you a basic summary of file, including its size, creation and modification dates, and permissions.

Finding File Type

Another useful feature of ‘stat’ command is that it can help you determine file type of a file. This is particularly helpful if you are dealing with files of different types, such as text files, image files, or executable files.

To find out file type of a file, use following command −

stat --format '%F' [filename or directory]

For example, if you want to find out file type of a file called ‘example.txt’, type following command −

stat --format '%F' example.txt

This will give you file type, which in this case should be ‘regular file’.

Displaying Access Permissions

The ‘stat’ command can also be used to display access permissions of a file or directory. This is helpful when you need to know who has access to a file or directory, and what kind of access they have.

To display access permissions of a file, use following command −

stat --format '%A' [filename or directory]

For example, if you want to display access permissions of a file called ‘example.txt’, type following command −

stat --format '%A' example.txt

This will display access permissions of file, such as ‘rw-r--r--’, which indicates that owner of file has read and write access, while others can only read file.

Showing File Size in Human-Readable Format

The ‘stat’ command can also be used to display size of a file in a human-readable format, such as kilobytes or megabytes. This is helpful when you need to quickly determine size of a file without having to do any conversions.

To display size of a file in a human-readable format, use following command −

stat --format '%s' [filename or directory] | numfmt --to=iec

For example, if you want to display size of a file called ‘example.txt’ in a human-readable format, type following command −

stat --format '%s' example.txt | numfmt --to=iec

This will display size of file in a format like ‘23K’ or ‘45M’, which is much easier to read than a raw number in bytes.

Getting Last Access Time of a File

Finally, ‘stat’ command can be used to get last access time of a file. This is helpful when you need to know when a file was last accessed, which can be useful for tracking changes to files or for troubleshooting.

To get last access time of a file, use following command −

stat --format '%x' [filename or directory]

For example, if you want to get last access time of a file called ‘example.txt’, type following command −

stat --format '%x' example.txt

This will display last access time of the file in a format like ‘YYYY-MM-DD HH:MM:SS’, which indicates year, month, day, hour, minute, and second when file was last accessed.

Displaying Last Modified Time of a File

Similar to getting last access time of a file, ‘stat’ command can also be used to display last modified time of a file. This is helpful when you need to know when a file was last modified or edited.

To get last modified time of a file, use following command −

stat --format '%y' [filename or directory]

For example, if you want to get last modified time of a file called ‘example.txt’, type following command −

stat --format '%y' example.txt

This will display last modified time of file in a format like ‘YYYY-MM-DD HH:MM:SS’, indicating when file was last modified.

Displaying Inode Number of a File

In Linux, every file and directory is assigned a unique inode number, which is used by operating system to identify file or directory. ‘stat’ command can be used to display inode number of a file, which can be helpful for troubleshooting or debugging.

To display inode number of a file, use following command −

stat --format '%i' [filename or directory]

For example, if you want to display inode number of a file called ‘example.txt’, type following command −

stat --format '%i' example.txt

This will display inode number of file, which is a unique identifier used by operating system.

Displaying Number of Hard Links to a File

In Linux, a hard link is a reference to a file that allows it to be accessed from multiple locations in file system. ‘stat’ command can be used to display number of hard links to a file, which can be helpful for understanding how file is being used.

To display number of hard links to a file, use following command −

stat --format '%h' [filename or directory]

For example, if you want to display number of hard links to a file called ‘example.txt’, type following command −

stat --format '%h' example.txt

This will display number of hard links to file, which indicates how many times file is referenced in file system.

Displaying File System Block Size

In Linux, files are stored on file system in blocks, which are typically a few kilobytes in size. ‘stat’ command can be used to display file system block size, which can be helpful for understanding how files are stored on file system.

To display file system block size, use following command −

stat --format '%o' [filename or directory]

For example, if you want to display file system block size of a file called ‘example.txt’, type following command −

stat --format '%o' example.txt

This will display file system block size in bytes, which can help you understand how files are stored on file system and how they are allocated space.

Conclusion

The ‘stat’ command is a powerful tool for Linux users, providing a wealth of information about files and directories. In this article, we’ve explored five useful examples of ‘stat’ command for Linux newbies, including getting basic information, finding file type, displaying access permissions, showing file size in a human-readable format, and getting last access time of a file.

By using these examples, Linux users can better understand and manage their files and directories, improving their overall productivity and efficiency. Whether you’re a new Linux user or an experienced pro, ‘stat’ command is an essential tool in your toolkit.

Updated on: 10-Apr-2023

154 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements