Skip to content
Daniel Berger edited this page Mar 17, 2023 · 14 revisions

The file-find library is a way to find files on your computer’s filesystem. It is superior to the find library that ships with Ruby’s standard library in that it allows you to setup “rules” as objects, which you can restrict in various ways, and reuse as needed.

Installation

gem install file-find

Synopsis

# Find all files ending in '.rb', do not follow symlinks, skip directories, and only
# search the two paths provided (and their subdirectories).

rule = File::Find.new(
  :pattern    => "*.rb",
  :follow     => false,
  :path       => ['/usr/local/lib', '/opt/local/lib'],
  :directory? => false
)

rule.find{ |f|
  puts f
}

Options

  • atime
    Limits searches by file access time, where the value you supply is the number of days back from the time that the File::Find#find method was called.
  • ctime
    Limits searches by file change time, where the value you supply is the number of days back from the time that the File::Find#find method was called.
  • filetest
    An array of two element arrays for storing FileTest methods and their boolean value. Note that FileTest methods can be passed individually, too, e.g. :directory? => false.
  • follow
    Controls the behavior of how symlinks are followed. If set to true (the default), then follows the file pointed to. If false, it considers the symlink itself.
  • ftype
    Limits searches to specific types of files. The possible values here are those returned by the File.ftype method.
  • group
    Limits searches to files that belong to a specific group, where the group can be either a group name or ID.
  • inum
    Limits search to a file with a specific inode number. Ignored on MS Windows.
  • links
    Limits search to files with the specified number of links.
  • maxdepth
    Limits search to a maximum depth into the tree relative to the starting search directory.
  • mindepth
    Limits searches to a minimum depth into the tree relative to the starting search directory.
  • mount
    Limits searches to the same filesystem as the specified directory. For Windows users, this refers to the volume.
  • mtime
    Limits searches by file modification time, where the value you supply is the number of days back from the time that the File::Find#find method was called.
  • name
    The name pattern used to limit file searches. The patterns that are legal for Dir.glob are legal here. The default is ‘*’, i.e. everything.
  • path
    The starting path(s) for the search. The default is the current directory. This can be a single path or an array of paths.
  • perm
    Limits searches to files which have permissions that match the octal value that you provide. For purposes of this comparison, only the user, group, and world settings are used. Do not use a leading 0 in the values that you supply, e.g. use 755 not 0755. You may optionally use symbolic permissions, e.g. “g+rw”, “u=rwx”, etc.
    Not currently supported on MS Windows.
  • previous
    The file that matched previously in the current search.
  • prune
    Skips files or directories that match the string provided as an argument.
  • size
    If the value passed is an integer, this option limits searches to files that match the size, in bytes, exactly. If a string is passed, you can use the standard comparable operators to match files, e.g. “>= 200” would limit searches to files greater than or equal to 200 bytes.
  • user
    Limits searches to files that belong to a specific user, where the user can be either a user name or an ID.

Other Methods

  • options
    The list of options originally passed to the constructor and/or used by the File::Find#find method.
Clone this wiki locally