用于管理文件的Ruby脚本

时间:2013-04-17 09:09:58

标签: ruby bash file-management

我正在寻找一个可以执行以下操作的脚本:

我有一组普通文件,例如“myFile.log”,我想以当前状态离开。

我想将所有myFile.log.year-month-day-hour类型的文件放入带有单独子文件夹的gzipped tar存档中。

有人能指出我如何完成这项工作的正确方向吗?

1 个答案:

答案 0 :(得分:1)

如果你想对Ruby中的文件做任何有趣的事情,你需要

require 'fileutils'

在IRB中查看您可以使用的方法

1.9.3-p327 :001 > require 'fileutils'
 => true
1.9.3-p327 :002 > File.methods.sort - Object.methods
 => [:absolute_path, :atime, :basename, :binread, :binwrite, :blockdev?, :chardev?, :chmod, :chown, :copy_stream, :ctime, :delete, :directory?, :dirname, :executable?, :executable_real?, :exist?, :exists?, :expand_path, :extname, :file?, :fnmatch, :fnmatch?, :for_fd, :foreach, :ftype, :grpowned?, :identical?, :join, :lchmod, :lchown, :link, :lstat, :mtime, :open, :owned?, :path, :pipe, :pipe?, :popen, :read, :readable?, :readable_real?, :readlines, :readlink, :realdirpath, :realpath, :rename, :select, :setgid?, :setuid?, :size, :size?, :socket?, :split, :stat, :sticky?, :symlink, :symlink?, :sysopen, :truncate, :try_convert, :umask, :unlink, :utime, :world_readable?, :world_writable?, :writable?, :writable_real?, :write, :zero?]
1.9.3-p327 :007 > FileUtils.methods.sort - Object.methods
 => [:cd, :chdir, :chmod, :chmod_R, :chown, :chown_R, :cmp, :collect_method, :commands, :compare_file, :compare_stream, :copy, :copy_entry, :copy_file, :copy_stream, :cp, :cp_r, :getwd, :have_option?, :identical?, :install, :link, :ln, :ln_s, :ln_sf, :makedirs, :mkdir, :mkdir_p, :mkpath, :move, :mv, :options, :options_of, :private_module_function, :pwd, :remove, :remove_dir, :remove_entry, :remove_entry_secure, :remove_file, :rm, :rm_f, :rm_r, :rm_rf, :rmdir, :rmtree, :safe_unlink, :symlink, :touch, :uptodate?]
1.9.3-p327 :013 > Dir.methods.sort - Object.methods
 => [:[], :chdir, :chroot, :delete, :entries, :exist?, :exists?, :foreach, :getwd, :glob, :home, :mkdir, :open, :pwd, :rmdir, :unlink]

从例如开始Dir.entries('/path'),使用Ruby的数组方法处理列表,使用正则表达式提取部分名称等