1. Computing

Getting Information About a File in Ruby Using File::Stat


Getting Information About a File in Ruby Using File::Stat

How large is a file? When was it modified? Is the file executable? The File::Stat class is a class that give access to such information. Its name comes from the *NIX stat system call, and this system call is used to gather the information for File::Stat on many systems.

At its core, a File::Stat object is just a simple data structure. It stores various pieces of information about a file at the time that the system call returned that information. If the file changes, for example if someone makes it executable or adds some data to the end of the file, then the information in the File::Stat object will not be automatically updated. It's a simple, unintelligent and unclever data structure.

This data structure can actually be obtained from any IO object, returned by the IO#stat method. This is particularly useful for obtaining information about files that are already open (since the File class is a child class of IO). However, for most of the information that the File::Stat object holds, you might not necessarily want to open the file just to learn about it. For this, Ruby provides the File::stat class method, which will query the operating system about the file without opening it. It takes a single parameter, the filename of the file in question.

So what kind of things will File::Stat tell you?

  • atime - Returns the time the file was last accessed. Not all operating systems and filesystems keep track of this information though, so it may appear as the "epoch" time (AKA Jan 1st 1970). All of the time methods return the time as a DateTime object, regardless of how the date or time is represented by the operating system.
  • ctime - Returns the time the directory information about the file was last changed. In other words, when was the file last moved or renamed. On NTFS this will return when the file was created.
  • mtime - Returns the time the file was last modified.
  • size - Returns the size of the file in bytes. This is perhaps the most common use for the File::Stat class.
  • zero? - Returns true if the file is zero bytes in length. Note that this can conceivably return true if the file is a special file, such as a symbolic link, a device file, a socket, etc.
  • readable? and writable? - Returns true if the Ruby process can read from or write to the file. This depends on who owns the file, the permissions of the file and the user id of the Ruby process.
  • symlink? - Returns true if the file is a symbolic link. If the file is not a symbolic link or the operating system does not support symbolic links, the method will return false. Note that the File::stat automatically follows symbolic links. If File::stat is called on a symbolic link, it is the equivalent to calling the method on the file pointed to by the symlink. To get the status of a symbolic link, use the File::lstat method instead.
  • chardev? and blockdev? - These will return true if the file is a character or block device. These are files on UNIX systems that represent interfaces to either physical devices or operating system features.


Here is a common thing one might want to be able to do: iterate over the files in a directory and determine the file sizes and the total size. To complicate matters further, the program must work recursively, totaling the file sizes of all files in all subdirectories of the current directory. This is achieved using the find library included with Ruby.

#!/usr/bin/env ruby
require "find"

files = []
total = 0
Find.find('.') do|f|
  files << f
  total += File.stat(f).size

puts "There are #{files.length} files"
puts "They are %0.2f megabytes in size" % (total / 1024.0 / 1024.0)
  1. About.com
  2. Computing
  3. Ruby
  4. Beginning Ruby
  5. Getting Information About a File in Ruby Using File::Stat

©2014 About.com. All rights reserved.