module FileUtils
Overview
NOTE To useFileUtils, you must explicitly import it withrequire "file_utils"
Extended Modules
Defined in:
file_utils.crInstance Method Summary
-
#cd(path : Path | String) : Nil
Changes the current working directory of the process to the given stringpath.
-
#cd(path : Path | String, &)
Changes the current working directory of the process to the given stringpath and invoked the block, restoring the original working directory when the block exits.
-
#cmp(filename1 : Path | String, filename2 : Path | String) : Bool
Compares two filesfilename1 tofilename2 to determine if they are identical.
-
#cp(src_path : Path | String, dest : Path | String) : Nil
Copies the filesrc_path to the file or directorydest.
-
#cp(srcs : Enumerable(Path | String), dest : Path | String) : Nil
Copies a list of filessrc todest.
-
#cp_r(src_path : Path | String, dest_path : Path | String) : Nil
Copies a file or directorysrc_path todest_path.
-
#ln(src_path : Path | String, dest_path : Path | String) : Nil
Creates a hard linkdest_path which points tosrc_path.
-
#ln(src_paths : Enumerable(Path | String), dest_dir : Path | String) : Nil
Creates a hard link to each path insrc_paths inside thedest_dir directory.
-
#ln_s(src_path : Path | String, dest_path : Path | String) : Nil
Creates a symbolic linkdest_path which points tosrc_path.
-
#ln_s(src_paths : Enumerable(Path | String), dest_dir : Path | String) : Nil
Creates a symbolic link to each path insrc_paths inside thedest_dir directory.
-
#ln_sf(src_path : Path | String, dest_path : Path | String) : Nil
Like
#ln_s(Path | String, Path | String), but overwritesdest_pathif it exists and is not a directory or ifdest_path/src_pathexists. -
#ln_sf(src_paths : Enumerable(Path | String), dest_dir : Path | String) : Nil
Creates a symbolic link to each path insrc_paths inside thedest_dir directory, ignoring any overwritten paths.
-
#mkdir(path : Path | String, mode = 511) : Nil
Creates a new directory at the givenpath.
-
#mkdir(paths : Enumerable(Path | String), mode = 511) : Nil
Creates a new directory at the givenpaths.
-
#mkdir_p(path : Path | String, mode = 511) : Nil
Creates a new directory at the givenpath, including any non-existing intermediate directories.
-
#mkdir_p(paths : Enumerable(Path | String), mode = 511) : Nil
Creates a new directory at the givenpaths, including any non-existing intermediate directories.
-
#mv(src_path : Path | String, dest_path : Path | String) : Nil
Movessrc_path todest_path.
-
#mv(srcs : Enumerable(Path | String), dest : Path | String) : Nil
Moves everysrcs todest.
-
#pwd : String
Returns the current working directory.
-
#rm(path : Path | String) : Nil
Deletes thepath file given, raises if the path does not exist.
-
#rm(paths : Enumerable(Path | String)) : Nil
Deletes allpaths files given, raises if any of the paths doesn't exist.
-
#rm_f(path : Path | String) : Nil
Deletes the givenpath file, ignoring it if it does not exist.
-
#rm_f(paths : Enumerable(Path | String)) : Nil
Deletes allpaths files given, ignoring non-existing ones.
-
#rm_r(path : Path | String) : Nil
Deletes a file or directorypath.
-
#rm_r(paths : Enumerable(Path | String)) : Nil
Deletes a list of files or directoriespaths.
-
#rm_rf(path : Path | String) : Nil
Deletes a file or directorypath.
-
#rm_rf(paths : Enumerable(Path | String)) : Nil
Deletes a list of files or directoriespaths.
-
#rmdir(path : Path | String) : Nil
Removes the directory at the givenpath.
-
#rmdir(paths : Enumerable(Path | String)) : Nil
Removes all directories at the givenpaths.
-
#touch(path : Path | String, time : Time = Time.utc) : Nil
Attempts to set the access and modification times of the file named in thepath parameter to the value given intime.
-
#touch(paths : Enumerable(Path | String), time : Time = Time.utc) : Nil
Attempts to set the access and modification times of each file given in thepaths parameter to the value given intime.
Instance Method Detail
Changes the current working directory of the process to the given stringpath.
require "file_utils"
FileUtils.cd("/tmp")
NOTE Alias ofDir.cd
Changes the current working directory of the process to the given stringpath and invoked the block, restoring the original working directory when the block exits.
require "file_utils"
FileUtils.cd("/tmp") { Dir.current } # => "/tmp"
NOTE Alias ofDir.cd with block
Compares two filesfilename1 tofilename2 to determine if they are identical.
Returnstrue if content are the same,false otherwise.
require "file_utils"
File.write("file.cr", "1")
File.write("bar.cr", "1")
FileUtils.cmp("file.cr", "bar.cr") # => true
NOTE Alias ofFile.same_content?
Copies the filesrc_path to the file or directorydest. Ifdest is a directory, a file with the same basename assrc_path is created indest Permission bits are copied too.
require "file_utils"
File.touch("afile")
File.chmod("afile", 0o600)
FileUtils.cp("afile", "afile_copy")
File.info("afile_copy").permissions.value # => 0o600
Copies a list of filessrc todest. dest must be an existing directory.
require "file_utils"
Dir.mkdir("files")
FileUtils.cp({"bar.cr", "afile"}, "files")
Copies a file or directorysrc_path todest_path. Ifsrc_path is a directory, this method copies all its contents recursively. Ifdest is a directory, copies src to dest/src.
require "file_utils"
FileUtils.cp_r("files", "dir")
Creates a hard linkdest_path which points tosrc_path. Ifdest_path already exists and is a directory, creates a linkdest_path/src_path.
require "file_utils"
# Create a hard link, pointing from /usr/bin/emacs to /usr/bin/vim
FileUtils.ln("/usr/bin/vim", "/usr/bin/emacs")
# Create a hard link, pointing from /tmp/foo.c to foo.c
FileUtils.ln("foo.c", "/tmp")
Creates a hard link to each path insrc_paths inside thedest_dir directory.
Ifdest_dir is not a directory, raises anArgumentError.
require "file_utils"
# Create /usr/bin/vim, /usr/bin/emacs, and /usr/bin/nano as hard links
FileUtils.ln(["vim", "emacs", "nano"], "/usr/bin")
Creates a symbolic linkdest_path which points tosrc_path. Ifdest_path already exists and is a directory, creates a linkdest_path/src_path.
require "file_utils"
# Create a symbolic link pointing from logs to /var/log
FileUtils.ln_s("/var/log", "logs")
# Create a symbolic link pointing from /tmp/src to src
FileUtils.ln_s("src", "/tmp")
Creates a symbolic link to each path insrc_paths inside thedest_dir directory.
Ifdest_dir is not a directory, raises anArgumentError.
require "file_utils"
# Create symbolic links in src/ pointing to every .c file in the current directory
FileUtils.ln_s(Dir["*.c"], "src")
Like#ln_s(Path | String, Path | String), but overwritesdest_path if it exists and is not a directory
or ifdest_path/src_path exists.
require "file_utils"
# Create a symbolic link pointing from bar.c to foo.c, even if bar.c already exists
FileUtils.ln_sf("foo.c", "bar.c")
Creates a symbolic link to each path insrc_paths inside thedest_dir directory, ignoring any overwritten paths.
Ifdest_dir is not a directory, raises anArgumentError.
require "file_utils"
# Create symbolic links in src/ pointing to every .c file in the current directory,
# even if it means overwriting files in src/
FileUtils.ln_sf(Dir["*.c"], "src")
Creates a new directory at the givenpath. The linux-style permissionmode can be specified, with a default of 777 (0o777).
require "file_utils"
FileUtils.mkdir("src")
NOTE Alias ofDir.mkdir
Creates a new directory at the givenpaths. The linux-style permissionmode can be specified, with a default of 777 (0o777).
require "file_utils"
FileUtils.mkdir(["foo", "bar"])
Creates a new directory at the givenpath, including any non-existing intermediate directories. The linux-style permissionmode can be specified, with a default of 777 (0o777).
require "file_utils"
FileUtils.mkdir_p("foo")
NOTE Alias ofDir.mkdir_p
Creates a new directory at the givenpaths, including any non-existing intermediate directories. The linux-style permissionmode can be specified, with a default of 777 (0o777).
require "file_utils"
FileUtils.mkdir_p(["foo", "bar", "baz", "dir1", "dir2", "dir3"])
Movessrc_path todest_path.
NOTE Ifsrc_path anddest_path exist on different mounted filesystems, the file atsrc_path is copied todest_path and then removed.
require "file_utils"
FileUtils.mv("afile", "afile.cr")
Moves everysrcs todest.
require "file_utils"
FileUtils.mv(["foo", "bar"], "src")
Deletes thepath file given, raises if the path does not exist.
require "file_utils"
FileUtils.rm("afile.cr")
NOTE Alias ofFile.delete
Deletes allpaths files given, raises if any of the paths doesn't exist.
require "file_utils"
FileUtils.rm(["dir/afile", "afile_copy"])
Deletes the givenpath file, ignoring it if it does not exist.
require "file_utils"
FileUtils.rm_f("afile.cr")
NOTE Alias ofFile.delete?
Deletes allpaths files given, ignoring non-existing ones.
require "file_utils"
FileUtils.rm_f(["dir/afile", "afile_copy"])
Deletes a file or directorypath. Ifpath is a directory, this method removes all its contents recursively.
require "file_utils"
FileUtils.rm_r("dir")
FileUtils.rm_r("file.cr")
Deletes a list of files or directoriespaths. If one path is a directory, this method removes all its contents recursively.
require "file_utils"
FileUtils.rm_r(["files", "bar.cr"])
Deletes a file or directorypath. Ifpath is a directory, this method removes all its contents recursively. Ignores all errors.
require "file_utils"
FileUtils.rm_rf("dir")
FileUtils.rm_rf("file.cr")
FileUtils.rm_rf("non_existent_file")
Deletes a list of files or directoriespaths. If one path is a directory, this method removes all its contents recursively. Ignores all errors.
require "file_utils"
FileUtils.rm_rf(["dir", "file.cr", "non_existent_file"])
Removes the directory at the givenpath.
require "file_utils"
FileUtils.rmdir("baz")
NOTE Alias ofDir.delete
Removes all directories at the givenpaths.
require "file_utils"
FileUtils.rmdir(["dir1", "dir2", "dir3"])
Attempts to set the access and modification times of the file named in thepath parameter to the value given intime.
If the file does not exist, it will be created.
require "file_utils"
FileUtils.touch("afile.cr")
NOTE Alias ofFile.touch