logo

How to Check the Metadata of a File in Julia 📂Julia

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.

NameTypeDescription
descUnion{String, Base.OS_HANDLE}file path or OS file descriptor
sizeInt64file size (in bytes)
deviceUIntID of the device on which the file resides
inodeUIntfile’s inode number
modeUIntfile protection mode (permissions)
nlinkIntnumber of hard links to the file
uidUIntuser ID of the file owner
gidUIntgroup ID of the file owner
rdevUIntif the file refers to a device, the referenced device ID
blksizeInt64recommended block size by the filesystem
blocksInt64number of allocated 512-byte blocks
mtimeFloat64last modification time of the file (Unix timestamp)
ctimeFloat64time 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