Returns the last component of the given file path. Returns whether the given path is the absolute file path. Returns the absolute file path for the given path. Returns the FIFO file created at the given path.Ĭreates a symbolic link for the given file path. Same as ::new, but when given a block will yield the file to the block, and close the file upon exiting the block.Ĭreates a new name for an existing file using a hard link. Opens the file at the given path returns the file. Here, class File provides methods that are useful for:
which provides dozens of additional methods. Inherits from class IO, in particular, methods for creating, reading, and writing files Various constants for the methods in File can be found in File::Constants.
The only change that can be made is to make the file read-only, which is reported as 0444. For instance, on Windows NT the default permission bits are 0644, which means read/write for owner, read-only for all others. In this case, the remaining permission bits will be synthesized to resemble typical values. On non-Posix operating systems, there may be only the ability to make a file read-only or read-write. If the permissions are for a directory, the meaning of the execute bit changes when set the directory can be searched. Higher-order bits may also be used to indicate the type of file (plain, directory, pipe, socket, and so on) and various other special features. The permission bits 0644 (in octal) would thus be interpreted as read/write for owner, and read-only for group and other. For each of these entities, permissions may be set to read, write, or execute the file: On Unix-based systems, permissions are viewed as a set of three octets, for the owner, the group, and the rest of the world. In the description of File methods, permission bits are a platform-specific set of bits that indicate permissions of a file. File includes the methods of module FileTest as class methods, allowing you to write (for example) File.exist?("foo").
You have learned how to manage files & folders in Ruby using built-in methods like File.read & File is an abstraction of any file object accessible by the program and is closely associated with class IO.
There are some extra file handling utilities you can get access to within the FileUtils module.įor example, you can compare files, touch a file (to update the last access & modification time), or copy files & directories with cp_r.ītw the “r” in cp_r stands for “recursive”. Using the Dir class it’s also possible to print the current working directory:Ĭreate a temporary directory with mktmpdir:
Use this if you only want to search for directories:
This one line of code will recursively list all files in Ruby, starting from the current directory: # All files containing "spec" in the name Using Dir.glob you can get a list of all the files that match a certain pattern. You can also get stats for a file, like file size, permissions, creation date, etc: If you want to process a file one line at a time, you can use the foreach method.įile.foreach("users.txt") When you’re done working with a file you want to close it to free up memory & system resources.Īs an alternative to having to open & close the file, you can use the File.read method:
If you’re working with a file that has multiple lines you can either split the file_data, or use the readlines method plus the chomp method to remove the new line characters. You can read the contents of the file in three ways. Read the file, the whole file, line by line, or a specific amount of bytes.Īs a result you’ll get a File object, but not the contents of the file yet.