Objects of class Dir are directory streams representing directories in the underlying file system. They provide a variety of ways to list directories and their contents.
The directory used in these examples contains the two regular files (config.h and main.rb), the parent directory (..), and the directory itself (.).
See also: File.
Returns a new directory object for the named directory.
Alias for .new(path)
Returns an array of all files that match against any of patterns.
Returns an array of all files that match against any of patterns.
DEPRECATED Use the overload with a match parameter instead
Returns an array of all files that match against any of patterns.
Returns an array of all files that match against any of patterns.
DEPRECATED Use the overload with a match parameter instead
Changes the current working directory of the process to the given string.
Changes the current working directory of the process to the given string and invokes the block, restoring the original working directory when the block exits.
See #children.
Returns an absolute path to the current working directory.
Removes the directory at path.
Removes the directory at path, or returns false if the directory does not exist.
See #each.
See #each_child.
Returns true if the directory at path is empty, otherwise returns false.
See #entries.
Returns true if the given path exists and is a directory
Returns an array of all files that match against any of patterns.
Returns an array of all files that match against any of patterns.
DEPRECATED Use the overload with a match parameter instead
Returns an array of all files that match against any of patterns.
Returns an array of all files that match against any of patterns.
DEPRECATED Use the overload with a match parameter instead
Yields all files that match against any of patterns.
Yields all files that match against any of patterns.
DEPRECATED Use the overload with a match parameter instead
Yields all files that match against any of patterns.
Yields all files that match against any of patterns.
DEPRECATED Use the overload with a match parameter instead
Creates a new directory at the given path.
Creates a new directory at the given path, including any non-existing intermediate directories.
Opens a directory and yields it, closing it at the end of the block.
Returns the tmp dir used for tempfile.
Returns an array containing all of the filenames except for . and .. in the given directory.
Closes the directory stream.
Calls the block once for each entry in this directory, passing the filename of each entry as a parameter to the block.
Must return an Iterator over the elements in this collection.
Calls the block once for each entry except for . and .. in this directory, passing the filename of each entry as a parameter to the block.
Returns an iterator over of the all entries in this directory except for . and ...
Returns an array containing all of entries in the given directory including "." and "..".
Appends a String representation of this object which includes its class name, its object address and the values of all instance variables.
Returns the path of this directory.
Reads the next entry from dir and returns it as a string.
Repositions this directory to the first entry.
Appends a short String representation of this object which includes its class name and its object address.
Iterable(String)
Enumerable(String)
Enumerable(String)
Reference
Reference
Reference
Object
Object
Object
Alias for .new(path)
Returns an array of all files that match against any of patterns.
The pattern syntax is similar to shell filename globbing, see File.match? for details.
NOTE Path separator in patterns needs to be always /. The returned file names use system-specific path separators.
Returns an array of all files that match against any of patterns.
The pattern syntax is similar to shell filename globbing, see File.match? for details.
NOTE Path separator in patterns needs to be always /. The returned file names use system-specific path separators.
Changes the current working directory of the process to the given string.
Changes the current working directory of the process to the given string and invokes the block, restoring the original working directory when the block exits.
Returns an absolute path to the current working directory.
The result is similar to the shell commands pwd (POSIX) and .cd (Windows).
On POSIX systems, it respects the environment value $PWD if available and if it points to the current working directory.
Removes the directory at path. Raises File::Error on failure.
On Windows, also raises File::Error if path points to a directory that is a reparse point, such as a symbolic link. Those directories can be deleted using File.delete instead.
Removes the directory at path, or returns false if the directory does not exist. Raises File::Error on other kinds of failure.
On Windows, also raises File::Error if path points to a directory that is a reparse point, such as a symbolic link. Those directories can be deleted using File.delete? instead.
See #each_child.
Returns true if the directory at path is empty, otherwise returns false. Raises File::NotFoundError if the directory at path does not exist.
Dir.mkdir("bar")
Dir.empty?("bar") # => true
File.write("bar/a_file", "The content")
Dir.empty?("bar") # => false Returns true if the given path exists and is a directory
Dir.mkdir("testdir")
Dir.exists?("testdir") # => true Returns an array of all files that match against any of patterns.
Dir.glob "path/to/folder/*.txt" # Returns all files in the target folder that end in ".txt". Dir.glob "path/to/folder/**/*" # Returns all files in the target folder and its subfolders.
The pattern syntax is similar to shell filename globbing, see File.match? for details.
NOTE Path separator in patterns needs to be always /. The returned file names use system-specific path separators.
Returns an array of all files that match against any of patterns.
Dir.glob "path/to/folder/*.txt" # Returns all files in the target folder that end in ".txt". Dir.glob "path/to/folder/**/*" # Returns all files in the target folder and its subfolders.
The pattern syntax is similar to shell filename globbing, see File.match? for details.
NOTE Path separator in patterns needs to be always /. The returned file names use system-specific path separators.
Yields all files that match against any of patterns.
The pattern syntax is similar to shell filename globbing, see File.match? for details.
NOTE Path separator in patterns needs to be always /. The returned file names use system-specific path separators.
Yields all files that match against any of patterns.
The pattern syntax is similar to shell filename globbing, see File.match? for details.
NOTE Path separator in patterns needs to be always /. The returned file names use system-specific path separators.
Creates a new directory at the given path. The linux-style permission mode can be specified, with a default of 777 (0o777).
NOTE mode is ignored on windows.
Dir.mkdir("testdir")
Dir.exists?("testdir") # => true Creates a new directory at the given path, including any non-existing intermediate directories. The linux-style permission mode can be specified, with a default of 777 (0o777).
Opens a directory and yields it, closing it at the end of the block. Returns the value of the block.
Returns an array containing all of the filenames except for . and .. in the given directory.
Calls the block once for each entry in this directory, passing the filename of each entry as a parameter to the block.
Dir.mkdir("testdir")
File.write("testdir/config.h", "")
d = Dir.new("testdir")
d.each { |x| puts "Got #{x}" } produces:
Got . Got .. Got config.h
Calls the block once for each entry except for . and .. in this directory, passing the filename of each entry as a parameter to the block.
Dir.mkdir("testdir")
File.write("testdir/config.h", "")
d = Dir.new("testdir")
d.each_child { |x| puts "Got #{x}" } produces:
Got config.h
Returns an iterator over of the all entries in this directory except for . and ...
See #each_child(&)
Dir.mkdir("test")
File.touch("test/foo")
File.touch("test/bar")
dir = Dir.new("test")
iter = d.each_child
iter.next # => "foo"
iter.next # => "bar" Returns an array containing all of entries in the given directory including "." and "..".
Dir.mkdir("testdir")
File.touch("testdir/file_1")
File.touch("testdir/file_2")
Dir.new("testdir").entries # => ["..", "file_1", "file_2", "."] Appends a String representation of this object which includes its class name, its object address and the values of all instance variables.
class Person
def initialize(@name : String, @age : Int32)
end
end
Person.new("John", 32).inspect # => #<Person:0x10fd31f20 @name="John", @age=32> Returns the path of this directory.
Dir.mkdir("testdir")
dir = Dir.new("testdir")
Dir.mkdir("testdir/extendeddir")
dir2 = Dir.new("testdir/extendeddir")
dir.path # => "testdir"
dir2.path # => "testdir/extendeddir" Reads the next entry from dir and returns it as a string. Returns nil at the end of the stream.
d = Dir.new("testdir")
array = [] of String
while file = d.read
array << file
end
array.sort # => [".", "..", "config.h"] Repositions this directory to the first entry.
Appends a short String representation of this object which includes its class name and its object address.
class Person
def initialize(@name : String, @age : Int32)
end
end
Person.new("John", 32).to_s # => #<Person:0x10a199f20>
© 2012–2026 Manas Technology Solutions.
Licensed under the Apache License, Version 2.0.
https://crystal-lang.org/api/1.19.0/Dir.html