O'Reilly logo

The Ruby Programming Language by David Flanagan, Yukihiro Matsumoto

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Files and Directories

The File class defines quite a few class methods for working with files as entries in a filesystem: methods for testing the size or existence of a named file, for example, and methods for separating a filename from the directory name that precedes it. These are class methods and they do not operate on File objects; instead, filenames are specified as strings. Similarly, the Dir class defines class methods for working with and reading filenames from filesystem directories. The subsections that follow demonstrate how to:

  • Work with and manipulate filenames and directory names

  • List directories

  • Test files to determine their type, size, modification time, and other attributes

  • Delete, rename, and perform similar operations on files and directories

Note that the methods described here query and manipulate files, but do not read or write file content. Reading and writing files is covered in Input/Output.

File and Directory Names

The class methods of the File and Dir classes operate on files and directories specified by name. Ruby uses Unix-style filenames with / as the directory separator character. You can use the forward slash character in your filenames, even when using ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required