How to Check the Metadata of a File in Julia
Description1
In Julia, to inspect a file’s metadata you can use the stat() function. It is included in Base’s Filesystem standard library, so no additional package needs to be loaded. stat returns a StatStruct object containing various information such as file size, modification time, creation time, and so on.
Code
If you create an arbitrary text file and pass it to stat(), you get something like the following.
julia> info = stat("test.txt")
StatStruct for "test.txt"
size: 82 bytes
device: 1692283895
inode: 26886
mode: 0o100666 (-rw-rw-rw-)
nlink: 1
uid: 0
gid: 0
rdev: 0
blksz: 4096
blocks: 0
mtime: (1 minute ago)
ctime: (1 minute ago)
julia> typeof(info)
Base.Filesystem.StatStruct
julia> info.size
82
julia> info.device
0x0000000064de33f7
The meaning of each field is as follows.
| Name | Type | Description |
|---|---|---|
| desc | Union{String, Base.OS_HANDLE} | file path or OS file descriptor |
| size | Int64 | file size (in bytes) |
| device | UInt | ID of the device on which the file resides |
| inode | UInt | file’s inode number |
| mode | UInt | file protection mode (permissions) |
| nlink | Int | number of hard links to the file |
| uid | UInt | user ID of the file owner |
| gid | UInt | group ID of the file owner |
| rdev | UInt | if the file refers to a device, the referenced device ID |
| blksize | Int64 | recommended block size by the filesystem |
| blocks | Int64 | number of allocated 512-byte blocks |
| mtime | Float64 | last modification time of the file (Unix timestamp) |
| ctime | Float64 | time when file metadata last changed (Unix timestamp) |
To view times more conveniently, 🔒(25/10/29)a conversion step is required. There are also functions that return individual attributes separately.
julia> filesize("test.txt")
82
julia> mtime("test.txt")
1.7514459083956373e9
Environment
- OS: Windows11
- Version: Julia 1.11.3
