Ruby File

Contents


Directory

Open directory

open

dir = Dir.open("/usr/local/bin")
dir.each{|file| puts file}
dir.close

each is to enumerate files in dir
If you use block method, you don’t need close method

Dir.open("/usr/local/bin"){|dir| puts dir.path}  # path : Directory path

Methods

Name Description
pwd, getwd Get current dir
chdir Change directory
mkdir Create new directory
rmdir Remove directory

Examples

Dir.pwd
Dir.chdir("/usr/local")   #=> 0 Success
Dir.mkdir("/tmp/homuhomu") #=> 0 Success
Dir.rmdir("/tmp/homuhomu") #=> 0 Success

File

File class

Read file

file = File.open("Read.me")
file.read    # Read all data
file.close

Set number of byte

file = File.open("README.md")
file.read(100)  # 100 bytes
file.close

Block

File.open('README.md') { |file| file.read }

Read per line

gets

file = File.new("README.md", "r")
while (line = file.gets)
    puts "#{line}"
end
file.close

Block

File.open("README.md", "r") do |file|
    while (line = file.gets)
        puts "#{line}"
    end
end

Exception handle


mode

Mode Description
r Read
w Write
a Add
r+ Read and write
w+ Write and read
a+ Read and write and add mode

write

File.open('new-file', "w"){|file| file.write("Hello World!") }

File attr

Name Description
File.basename Get file path
File.dirname Get directory name
File.extname Get index
File.split Get array including dir name and file name
File.stat Get File::Stat
File.lstat Get File::Stat
File.atime latest access time
File.ctime latest state change time
File.mtime latest update time
File.path Get file path
File.join Add File::SEPARATOR and create file path
File.mtime('README.md')
File.open('README'){|file| file.mtime}

If you use “.” as basename, you can get only file name.

FileTest module

Nmae Description
File.exists? Whether the file exists or not
File.file? The path is file or not
File.directory? Directory or not
File.symlink? Symbolic link or not
File.executable? Executable or not
File.readable? Readable or not
File.writable? Writable or not
File.size File size
File.directory?('/usr/local')       #=> true
File.directory?('/usr/local/bin/zsh')   #=> false

UNIX command?

Get file attr

File.chmod(0644, 'README')

Set latest access time and update time

File.utime(Time.now, Time.now, 'FIXME')

Convert path to absolute path

File.expand_path('README')

Remove file, rename

File.delete('README.md')
File.rename('README', 'FIXME')

Truncate file
truncate file defined byte

File.truncate('FIXME', 0)

Lock

File.open('README', "w"){|file| file.flock(File::LOCK_EX)}

fileutil

Basic library of IO.
FileUtils is used like UNIX command
You need to add require

require 'fileutils'

Change directory(cd), current directory(pwd)

FileUtils.cd(dir, options={})
FileUtils.pwd

Copy(cp, copy, cp_r)

FileUtils.cp(src, dest, options={})
FileUtils.copy(src, dest, options={})

cp_r

FileUtils.cp_r(src, dst, options={})

recursive, you can use directory as src.

Move(mv/move)

FileUtils.mv(src, dist, options={})
FileUtils.move(src, dist, options={})

Remove(rm/remove)

FileUtils.rm(list, options={})
FileUtils.remove(list, options ={})

rmdir

rm_r, rm_rf

FileUtils.rm_r(list, options={})
FileUtils.rm_rf(list, options={})

touch

FileUtils.touch(list, options={})

mkdir

FileUtils.mkdir(list, options={})
FileUtils.mkdir_p(list, options={})

chown, chmod

FileUtils.chown(user, group, list, options={})
FileUtils.chmod(mode, list, options ={})

ln, ln_s/symlink

FileUtils.ln_s(src, dest, option ={})
FileUtils.symlink(src, dest, options={})