Error:

Open file for reading

f = File.new("test.txt", "r")

Open file for writing

f = File.new("test.txt", "w")

Open file for appending

f = File.new("test.txt", "a+")

Open file for read/write

f = File.new("test.txt", "r+")

Expand tilde in the filename

expanded = File.expand_path('~/file.txt') # => /Users/user/file.txt

Create temporary file

require 'tempfile'

file = Tempfile.new('foo')
file.path # => "/tmp/foo.17234.0"
file.write('test')
file.flush
file.close

Lock file

lock = File.open("/tmp/prog.lock", File::RDWR|File::CREAT, 0644)
lock.flock(File::LOCK_EX)

Flush output

f = File.new("test.txt", "w")
f.write('test')
f.flush

Open file by file descriptor

fd = 2
stderr_io = IO.new(fd)

Read all contents of the file

contents = File.read("test.txt")

Read number of bytes from the binary file

bin = File.open("test.txt", "rb")
header = bin.sysread(8)

Read one line from the file

f = File.open("test.txt", "r")
line = f.gets

Read file line by line

File.open("test.txt", "r") do |f|
    while (line = f.gets) do
        puts "--> #{line}"
    end
end

Write line to file

f = File.open("test.txt", "a+")
f.puts('new line')
f.close

Write number of bytes to the binary file

f = File.open("test.bin", "wb")
f.syswrite("\x01\x02\x03\x04")
f.close

Get file creation time

File.ctime('test.txt') # => 2013-11-25 15:07:10 -0800

Get file modification time

File.mtime('test.txt') # => 2013-11-25 15:07:10 -0800

Set file modification time

path = 'test.txt'
modification_time = Time.now
File.utime(File.atime(path), modification_time, path) 

Delete the file

File.delete('test.bin')

Copy file

require 'fileutils'
FileUtils.copy('test.txt', 'test.txt~')

Move file

require 'fileutils'
FileUtils.mv('test.txt', 'test.txt~')

Rename file

require 'fileutils'
FileUtils.mv('test.txt', 'test.txt~')

Get file's inode

File.stat('test.txt').ino

Process all files in the directory

path = '/usr/lib'
Dir.foreach(path) do |item|
  next if item == '.' or item == '..'
  puts "==> #{item}"
end

Process contents of directory recursively

require 'find'
path = '/usr/lib'
Find.find(path) do |item|
  puts "==> #{item}"
end

Get list of file matching glob

txt = Dir["/path/to/*.txt"] # => ["/path/to/1.txt", "/path/to/2.txt"]

Remove directory and its contents

require 'fileutils'
FileUtils.rm_rf('a')

Get file extension

File.extname('/etc/config.ini') # => ".ini"

Get file name without extension

path = '/etc/config.ini'
filename = File.basename(path, File.extname(path)) # => "config"

Get directory part of the path

path = '/etc/config.ini'
dir = File.dirname(path) # => "/etc"

Get file name without directory

path = '/etc/config.ini'
file = File.basename(path) # => "config.ini"

Check if path is absolute

require 'pathname'
Pathname.new('/etc/config.ini').absolute? # => true
Pathname.new('config.ini').absolute?      # => false

Check if path is relative

require 'pathname'
Pathname.new('/etc/config.ini').relative? # => false
Pathname.new('config.ini').relative?      # => true

Make path absolute

abs_path = File.absolute_path('../file.txt') # => "/Users/user/Projects/CodeGofer/file.txt"

Get last path component

last_component = File.basename('/a/b/c/') # => "c"

Create path with components

File.join("a", "b", "c") # => "a/b/c"

Resolve symlinks in the path

real = File.realpath('/etc') # => "/private/etc" (On Mac OS X)

Normalize path

normalized = File.expand_path("../../../../bin") # => "/Users/bin"

Check if path exists

File.exists?("/etc") # => true

Check if path exists and it's a regular file

File.file?("/etc")        # => false
File.file?("/etc/passwd") # => true

Check if path exists and it's a directory

File.directory?("/etc")        # => true
File.directory?("/etc/passwd") # => false

Check if path exists and it's readable

File.readable?("/etc/passwd") # => true
File.readable?("/etc/master.passwd") # => false

Check if path exists and it's writable

File.writable?("/Users/user/.bashrc") # => true
File.writable?("/etc/master.passwd")  # => false

Check if path exists and it's executable

File.executable?("/usr/bin/less")       # => true
File.executable?("/etc/master.passwd")  # => false

Get file's owner

File.stat("/etc/master.passwd").uid # => 0

Get file's group

File.stat("/etc/master.passwd").gid # => 0

Get file's mode

File.stat("/usr/bin/less").mode # => 33261 (0100755)

Set file's owner

# Set user id to 0
File.chown(0, nil, "/tmp/foo.txt")

Set file's group

# Set group id to 0
File.chown(nil, 0, "/tmp/foo.txt")

Set file's mode

File.chmod(0755, "/tmp/foo.txt")

Create symlink

File.symlink('/Users/gonzo', 'homedir')

Resolve symlink

File.readlink('/etc') # => "private/etc" (on Mac OS X)