【问题标题】:Ruby script to manage files用于管理文件的 Ruby 脚本
【发布时间】:2013-04-09 23:29:31
【问题描述】:

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

我有一组普通文件,例如“myFile.log”,我想保留它们的当前状态。

我想将类型为 myFile.log.year-month-day-hour 的所有文件放入一个 gzip 压缩的 tar 存档中,每天都有单独的子文件夹。

任何人都可以为我指出正确的方向吗?

【问题讨论】:

标签: ruby bash file-management


【解决方案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 的数组方法处理列表、正则表达式提取部分名称等

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-02-08
    • 2016-03-20
    • 2011-01-28
    • 1970-01-01
    • 2017-11-09
    • 2022-08-22
    • 2010-12-09
    • 2019-06-14
    相关资源
    最近更新 更多