10 Examples of the ‘stat’ Command in Ubuntu

10 Examples of the 'stat' Command in Ubuntu

Introduction

The stat command in Ubuntu is a powerful tool for retrieving detailed information about files and file systems. It provides more information than typical commands like ls or du. This article explores 10 practical examples of using the stat command in Ubuntu to help you understand its capabilities and applications.

What Is the ‘stat’ Command?

The stat command displays comprehensive information about files, directories, and file systems. It provides data such as file size, permissions, inode number, access times, and more. This command is especially useful for system administrators and developers who need detailed insights into file attributes.

1. Basic Usage of the ‘stat’ Command

The simplest use of the stat command involves running it with a file name. This returns basic information about the file.

stat filename.txt

This command will display details like the file’s size, blocks, IO block size, file type, and permissions. It also provides access, modification, and change times.

2. Displaying Information in a Custom Format

The stat command allows you to customize the output format. The --format or -c option lets you specify which information to display.

stat --format="%s %n" filename.txt

In this example, %s represents the file size, and %n shows the file name. You can combine various format specifiers to create a customized output.

3. Checking File System Information

You can use the -f option with the stat command to display file system information. This is useful when you need details about the file system rather than individual files.

stat -f /

This command provides information about the file system type, block size, total blocks, and available blocks.

4. Displaying Information About a Directory

The stat command works with directories as well as files. Running it on a directory provides information about the directory itself, not its contents.

stat /home/user/

This command displays details such as the directory’s inode number, permissions, and timestamps.

5. Viewing Timestamps in the UTC Format

By default, stat displays timestamps in the local time zone. You can use the --time option to change this to UTC.

stat --time=utc filename.txt

This command will show the access, modification, and change times in UTC, which can be useful for consistency across different time zones.

6. Checking Symlink Information

When you use stat on a symbolic link, it provides information about the link itself, not the target file. Use the -L option to follow the symlink and get information about the target.

stat -L symlinkname

This command returns details about the file to which the symbolic link points, rather than the symlink itself.

7. Printing File Permissions in Numeric Format

By default, stat displays file permissions in a human-readable format. You can change this to a numeric format using the --format option.

stat --format="%a" filename.txt

This command outputs the file permissions as a three-digit number, which can be useful for scripting or detailed permission analysis.

8. Displaying Inode Information

Inodes are crucial for understanding how files are stored on disk. The stat command can display inode-related information using specific format specifiers.

stat --format="%i %n" filename.txt

Here, %i represents the inode number, and %n shows the file name. This information helps in understanding the file’s storage and retrieval process.

9. Getting File Size in Human-Readable Format

The stat command can display file sizes in a human-readable format, making it easier to interpret large sizes.

stat --format="%s %n" filename.txt | numfmt --to=iec

This command combines stat with numfmt to convert the file size into a format like KB, MB, or GB, which is easier to understand at a glance.

10. Using the ‘stat’ Command in Scripts

The stat command is highly useful in shell scripts. You can extract specific information using format specifiers and use this data for conditional checks or logging.

file_size=$(stat --format="%s" filename.txt)
if [ $file_size -gt 10000 ]; then
    echo "File is larger than 10 KB."
fi

This script checks the file size and prints a message if the file is larger than 10 KB. The stat command’s ability to return specific information makes it a valuable tool in scripting.

Conclusion

The stat command is an essential utility in Ubuntu for retrieving detailed information about files and file systems. Its versatility and ability to format output make it useful in a wide range of scenarios. Whether you are a system administrator, developer, or casual user, mastering the stat command will enhance your ability to manage and analyze files in Linux.

These 10 examples should give you a solid foundation for using the stat command in your daily tasks. Explore its options further to unlock even more potential for file management in Ubuntu.

Fedya Serafiev

Fedya Serafiev

Fedya Serafiev owns the website linuxcodelab.eu. He finds satisfaction in helping people solve even the most complex technical problems. His current goal is to write easy-to-follow articles so that such problems do not arise at all.

Thank you for reading the article! If you found the information useful, you can donate using the buttons below: