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 ...

Get The Ruby Programming Language now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.