File metadata and permissions

File.stat

File.stat asks the system about a path and returns an object with the answer: size, mode, owner, the three times and the type. It raises if the path does not exist, which is the difference from File.exist?:

ruby -e 'File.write("a.txt", "x\n"); p File.stat("a.txt").size; p File.stat("a.txt").mtime.class'

2
Time

The type predicates are on File itself and are the short form: File.file?, File.directory?, File.symlink?, File.readable?. Each one stats the path, so a loop that asks twice does the work twice.

The mode in octal

The mode packs the file type and the permission bits into one integer, which is why it is printed in octal. A directory and a regular file:

ruby -e 'p File.stat(".").mode.to_s(8)'

"40755"

ruby -e 'File.write("a.txt", ""); File.chmod(0o644, "a.txt"); p File.stat("a.txt").mode.to_s(8)'

"100644"

The leading 4 is a directory and the 10 is a regular file. The last three digits are the permissions in the order owner, group, other: 755 is read-write-execute for the owner and read-execute for the rest, 644 is read-write for the owner and read for the rest. chmod takes the number with a 0o prefix, or a string such as "u+x".

A file that a program writes is created with the process umask applied, usually 644. Making it executable is an explicit chmod 0o755, which is what an install script does after it copies a command.

mtime and the other times

stat.mtime is when the contents last changed, atime when it was last read, and ctime when the metadata last changed. All three are Time objects, so the formatting methods work directly:

ruby -e 'File.write("a.txt", "x\n"); p File.mtime("a.txt").strftime("%Y-%m-%d %H:%M:%S")'

"2026-11-05 00:14:44"

File.mtime is the shortcut for File.stat(path).mtime. The timestamp has sub-second precision even when strftime shows whole seconds, which is what the next lesson watches.

Symlinks

A symlink is a path that points at another path. File.stat follows it and reports the target; File.lstat reports the link itself:

ruby -e 'File.write("a.txt", "x\n"); File.symlink("a.txt", "link.txt"); p File.symlink?("link.txt"); p File.stat("link.txt").size; p File.lstat("link.txt").size'

true
2
5

Two bytes for the target's contents, five for the name it stores. A cleanup script that walks a tree with lstat sees the link and does not descend into the target, which is what stops a loop when a link points at its own directory.

A directory report

The pieces together: a list of names, a stat per name, and a formatted line. The program writes a.txt with two bytes and b.txt with six, and sets both timestamps so the output is the same on every run. The widths come from lesson 2.9:

File.write("a.txt", "xx") File.write("b.txt", "xxxxxx") stamp = Time.new(2026, 1, 1, 12, 0) File.utime(stamp, stamp, "a.txt") File.utime(stamp, stamp, "b.txt") Dir.glob("*.txt").sort.each do |name| stat = File.stat(name) puts format("%-12s %5d %s", name, stat.size, stat.mtime.strftime("%Y-%m-%d %H:%M")) end

ruby report.rb

a.txt 2 2026-01-01 12:00 b.txt 6 2026-01-01 12:00

File.expand_path is the missing piece when a report must name the file it read. It turns a relative path into an absolute one, with ~ for the home folder:

ruby -e 'p File.expand_path("~")'

"/home/agent"

What to watch for

The mode is a number, not a permission API. To ask whether the current user may read a file, use File.readable?, which takes the owner and the group into account; the mode bits alone do not.

Times come from the file system and have a resolution that depends on it. A test that writes a file and compares a timestamp to Time.now can fail on a file system with one-second precision; compare with a tolerance, or compare the contents.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What does File.stat do for a path that does not exist?

    Answer

    Raises Errno::ENOENT

    File.exist? is the check; stat asks the system and raises when there is nothing to ask about.

  2. What does this program print? File.write("a.txt", "x\n") p File.stat("a.txt").size

    Answer

    2

    The string holds the letter and the newline, so the file is two bytes long.

  3. What does this program print? File.write("a.txt", "x\n") p File.stat("a.txt").mtime.class

    Answer

    Time

    All three times are Time objects, so the formatting methods work on them directly.

  4. What does this program print? p File.stat(".").mode.to_s(8)

    Answer

    "40755"

    The 4 marks a directory, and the rest is the permission triplet printed in octal as a string.

  5. What does this program print? File.write("a.txt", "") File.chmod(0o644, "a.txt") p File.stat("a.txt").mode.to_s(8)

    Answer

    "100644"

    The 10 marks a regular file and 644 is the permission triplet.

  6. What does this program print? File.write("a.txt", "x\n") File.symlink("a.txt", "link.txt") p File.symlink?("link.txt") p File.stat("link.txt").size p File.lstat("link.txt").size

    Answer

    true / 2 / 5

    stat follows the link and sees the two bytes of the file; lstat sees the five characters of the name it stores.

  7. What does File.lstat report?

    Answer

    The link itself, not the target it points at

    That is what lets a cleanup walk see the link without descending into the target.

  8. What does this program print? Dir.chdir("/tmp") do p File.expand_path("a.txt") end

    Answer

    "/tmp/a.txt"

    expand_path turns a relative path into an absolute one against the working directory, and needs no file to exist.

  9. What does this program print? File.write("a.txt", "xx") File.write("b.txt", "xxxxxx") stamp = Time.new(2026, 1, 1, 12, 0) File.utime(stamp, stamp, "a.txt") File.utime(stamp, stamp, "b.txt") Dir.glob("*.txt").sort.each do |name| stat = File.stat(name) puts format("%-12s %5d %s", name, stat.size, stat.mtime.strftime("%Y-%m-%d %H:%M")) end

    Answer

    a.txt 2 2026-01-01 12:00 / b.txt 6 2026-01-01 12:00

    The format pads the name to twelve columns and the size to five, and utime fixes both timestamps.

  10. A program must know whether the current user may read a file. What should it call?

    Answer

    File.readable?

    The mode bits alone ignore the owner and the groups; File.readable? takes them into account.

Article author: Arthur Isaev