Overview of stat
`stat` command shows metadata that the file system stores to manage the object, rather than the content of the file or directory itself. It provides much more detailed information than `ls -l`, such as the creation time, last modification time, and access count, making it useful for analyzing file change history or access patterns.
Main Roles of stat
`stat` command is mainly used for the following purposes:
Key Areas of Usage
- **Check File Attributes**: Accurately verify the ownership, permissions, size, etc., of a file.
- **Analyze Timestamps**: Track the change history of files by checking various time information such as creation, last modification, and last access times.
- **Symbolic Link Information**: Check both the target path of the symbolic link and the information of the link itself.
- **File System Debugging**: Diagnose problems in the file system through disk space usage, block allocation, inode information, etc.
Understanding stat Output Information
`stat` command output consists of various items. Understanding the meaning of each item is important.
Key Output Items
- File: The name of the target file or directory.
- Size: The size of the file (in bytes). For directories, it is usually displayed as 4096 bytes.
- Blocks: The number of 512-byte blocks that the file occupies on disk.
- IO Block: The efficient block size for I/O operations for the file.
- Type: Indicates the type of file. For example: regular file, directory, symbolic link, etc.
- Device: The ID (major:minor) of the device where the file is located.
- Inode: The unique identifier (inode number) of the file within the file system. Hard links share the same inode.
- Links: The number of hard links to the file.
- Access: The permissions of the file (in octal and rwx format).
- Uid / Gid: The owner (User ID) and group (Group ID) of the file, along with their names.
- Access / Modify / Change: `Access`: last access time (atime), `Modify`: last modification time (mtime), `Change`: last change time (ctime). 'Birth' refers to the creation time (crtime), which may not be supported on all file systems.
Key Options for the stat Command
`stat` command provides several useful options to specify the output format or change how symbolic links are handled.
1. Basic Output Options
2. Symbolic Link Handling Options
3. Output Format Specifying Options
Generated command:
Try combining the commands.
Description:
`stat` Executes the command.
Combine the above options to virtually execute commands with AI.
Usage Examples
Learn how to check and analyze the detailed attributes of files/directories through various usage examples of the `stat` command.
Check All Information of a Single File
stat config.ini
Outputs all detailed information such as size, permissions, owner, timestamps, etc., of the `config.ini` file.
Check Information of the Symbolic Link Itself
stat -P mylink.sh
When `mylink.sh` points to `actual_script.sh`, outputs the information of `mylink.sh` itself. (Size, owner, etc., of the link file)
Check Information of the Original File Pointed by the Symbolic Link
stat -L mylink.sh
When `mylink.sh` points to `actual_script.sh`, outputs the information of the `actual_script.sh` file.
Output Specific Information in Custom Format
stat -c '%n %s %y' my_document.txt
Outputs only the name, size (in bytes), and last modification time (YYYY-MM-DD HH:MM:SS) of the `my_document.txt` file.
Check Information of the File System Containing the File
stat --file-system /var/log
Checks the type, block size, mount point, and other information of the file system where the `/var/log` directory is located.
Check Unix Timestamp (mtime) of a File
stat -c '%Y' important_data.csv
Useful for retrieving the modification time of a file in Unix timestamp (seconds) for calculations in scripts.