fileutils.rb

Copyright (c) 2000,2001 Minero Aoki <aamine@loveruby.net>

This program is free software.
You can distribute/modify this program under the same terms of ruby.

module FileUtils

The module which implements basic file operations.

Module Functions

FileUtils#cd( [options,] dir )
FileUtils#cd( [options,] dir ) {|dir| .... }
Options = noop verbose

changes current directory to DIR.

If this method is called with block, moves to the old current
directory after the block execution finished.

  FileUtils.cd :verbose, '/'   # chdir and report it
FileUtils#pwd
FileUtils#getwd
returns name of the current dirctory.
This method is same to Dir.pwd.

  FileUtils.chdir '/home/aamine'
  p FileUtils.pwd   # => "/home/aamine"
FileUtils#newest?( [options,] newer, older1, older2, ... )
FileUtils#uptodate?( [options,] newer, older1, older2, ... )
FileUtils#newer?( [options,] newer, older )
Options = verbose

returns true if NEWER is newer than all OLDERs.
Non-exist files are older than any file.

  FileUtils.newest? 'hello.o', 'hello.c', 'hello.h' or system 'make'
FileUtils#mkdir( [options,] dir1, dir2, ... )
Options = noop verbose

makes directories DIRs.

  FileUtils.mkdir 'test'
  FileUtils.mkdir %w( tmp data )     # This method accepts arrays.
  FileUtils.mkdir :noop, 'notexist'  # does not create really
FileUtils#mkdir_p( [options,] dir1, dir2, ... )
FileUtils#mkpath( [options,] dir1, dir2, ... )
FileUtils#makedirs( [options,] dir1, dir2, ... )
Options = noop verbose

makes dirctories DIRs and all its parent directories.
For example,

  FileUtils.mkdir_p '/usr/local/bin/ruby'

causes to make directories below (if it does not exist).
    * /usr
    * /usr/local
    * /usr/local/bin
    * /usr/local/bin/ruby
FileUtils#ln( [options,] old, new )
FileUtils#link( [options,] old, new )
Options = noop verbose

makes hard link NEW which links to OLD.
If NEW is a directory, creates link NEW/OLD.

  FileUtils.ln :verbose, 'gcc', 'cc'
FileUtils#ln( [options,] file1, file2 ..., dir )
FileUtils#link( [options,] file1, file2 ..., dir )
Options = noop verbose

links DIR/OLD1 to OLD1, DIR/OLD2 to OLD2, ....

  FileUtils.ln 'cp', 'mv', 'mkdir', '/usr/bin'
  FileUtils.ln %w( cp mv mkdir ), '/usr/bin'    # same result
FileUtils#ln_s( [options,] old, new )
FileUtils#symlink( [options,] old, new )
Options = force noop verbose

makes symbolic link NEW which links to OLD.
If last argument is a directory, links DIR/OLD1 to OLD1,
DIR/OLD2 to OLD2, ....

  FileUtils.ln_s '/usr/bin/ruby', '/usr/local/bin/ruby'
  FileUtils.ln_s :force, 'verylongnamesourcefile.c', 'c'
FileUtils#ln_s( [options,] file1, file2 ..., dir )
FileUtils#symlink( [options,] file1, file2 ..., dir )
Options = force noop verbose

makes symbolic link dir/file1, dir/file2 ... which point to
file1, file2 ... If DIR is not a directory, raises Errno::ENOTDIR.
This method removes target file when :force option is set.

 FileUtils.ln_s Dir.glob('bin/*.rb'), '/home/aamine/bin'
FileUtils#cp( [options,] src, dest )
FileUtils#copy( [options,] src, dest )
Options = preserve noop verbose

copies a file SRC to DEST. If DEST is a directory, copies
SRC to DEST/SRC.

  FileUtils.cp 'eval.c', 'eval.c.org'
FileUtils#cp( [options,] file1, file2 ..., dir )
FileUtils#copy( [options,] file1, file2 ..., dir )
Options = preserve noop verbose

copies FILE1 to DIR/FILE1, FILE2 to DIR/FILE2 ...

  FileUtils.cp 'cgi.rb', 'complex.rb', 'date.rb', '/usr/lib/ruby/1.6'
  FileUtils.cp :verbose, %w(cgi.rb complex.rb date.rb), '/usr/lib/ruby/1.6'
FileUtils#cp_r( [options,] src, dest )
Options = preserve noop verbose

copies SRC to DEST. If SRC is a directory, this method copies
its all contents recursively. If DEST is a directory, copies
SRC to DEST/SRC.

  # installing ruby library "mylib" under the site_ruby
  FileUtils.rm_r :force, site_ruby + '/mylib'
  FileUtils.cp_r 'lib/', site_ruby + '/mylib'
FileUtils#cp_r( [options,] file1, file2 ..., dir )
Options = preserve noop verbose

copies file/directory FILE1 to DIR/FILE1, FILE2 to DIR/FILE2 ...
If FILE is a directory, copies its all contents recursively.

  FileUtils.cp_r 'mail.rb', 'field.rb', 'debug/', site_ruby + '/tmail'
  FileUtils.cp_r :noop, :verbose, Dir.glob('*.rb'), '/home/aamine/lib/ruby'
FileUtils#mv( [options,] src, dest )
FileUtils#move( [options,] src, dest )
Options = noop verbose

moves a file SRC to DEST.
If FILE and DEST exist on the different disk partition,
copies it.

  FileUtils.mv 'badname.rb', 'goodname.rb'
  FileUtils.mv :verbose, 'stuff.rb', 'lib/ruby'
FileUtils#mv( [options,] file1, file2 ..., dir )
FileUtils#move( [options,] file1, file2 ..., dir )
Options = noop verbose

moves FILE1 to DIR/FILE1, FILE2 to DIR/FILE2 ...
If FILE and DEST exist on the different disk partition,
copies it.

  FileUtils.mv 'junk.txt', 'dust.txt', '/home/aamine/.trash/'
  FileUtils.mv :noop, :verbose, Dir.glob('test*.rb'), 'T'
FileUtils#rm( [options,] file1, file2, ... )
FileUtils#remove( [options,] file1, file2, ... )
Options = force noop verbose

remove FILEs. This method cannot remove directory.
This method ignores all errors when :force option
is set.

  FileUtils.rm 'junk.txt', 'dust.txt'
  FileUtils.rm Dir['*.so'], Dir['*.o']   # all methods accepts array
  FileUtils.rm :force, 'NotExistFile'    # never raises error
FileUtils#rm_r( [options,] file1, file2, ... )
Options = force noop verbose

remove FILEs. If FILE is a directory, removes its all contents
recursively. This method ignores all errors when :force option
is set.

  FileUtils.rm_r Dir.glob('/tmp/*')
  FileUtils.rm_r :force, '/'          #  :-)
FileUtils#cmp( [options,] a, b )
FileUtils#identical?( [options,] a, b )
Options = verbose

returns true if contents of file A and B is identical.

  FileUtils.cmp 'somefile', 'somefile'  #=> true
  FileUtils.cmp '/bin/cp', '/bin/mv'    #=> maybe false.
FileUtils#install( [options,] src, dest, mode = <from's> )
Options = noop verbose

If SRC is not same to DEST, copies it and changes the permittion
mode to MODE.

  FileUtils.install :verbose, 'ruby', '/usr/local/bin/ruby', 0755
  FileUtils.install :verbose, 'lib.rb', '/usr/local/lib/ruby/site_ruby'
FileUtils#update_file( [options,] dest, content )
Options = noop verbose

If file DEST is not same to CONTENT (is a String),
writes CONTENT to DEST.

  FileUtils.update_file :verbose, 'index.html', content
FileUtils#chmod( [options,] mode, file1, file2, ... )
Options = noop verbose

changes permittion bits on the named FILEs to the bit pattern
represented by MODE.

  FileUtils.chmod 0644, 'my.rb', 'your.rb'
  FileUtils.chmod 0755, 'somecommand'
  FileUtils.chmod :verbose, 0755, '/usr/bin/ruby'
FileUtils#touch( [options,] file1, file2, .... )
Options = noop verbose

updates modification time of file1, file2, ...
If fileN does not exist, creates it.

  FileUtils.touch 'main.c'; system 'make'

module FileUtils::Verbose

This class has all methods of FileUtils module and it works as same, but outputs messages before action. You can also pass verbose flag to all methods.

module FileUtils::NoWrite

This class has all methods of FileUtils module, but never changes files/directories.