module FileUtils
Extended Modules
Defined in:
file_utils.crInstance Method Summary
-
#cd(path : String)
Changes the current working directory of the process to the given string path.
-
#cd(path : String, &block)
Changes the current working directory of the process to the given string path and invoked the block, restoring the original working directory when the block exits.
-
#cmp(filename1 : String, filename2 : String)
Compares two files filename1 to filename2 to determine if they are identical.
-
#cmp(stream1 : IO, stream2 : IO)
Compares two streams stream1 to stream2 to determine if they are identical.
-
#cp(src_path : String, dest : String)
Copies the file src_path to the file or directory dest.
-
#cp(srcs : Enumerable(String), dest : String)
Copies a list of files src to dest.
-
#cp_r(src_path : String, dest_path : String)
Copies a file or directory src_path to dest_path.
-
#ln(src_path : String, dest_path : String)
Creates a hard link dest_path which points to src_path.
-
#ln(src_paths : Enumerable(String), dest_dir : String)
Creates a hard link to each path in src_paths inside the dest_dir directory.
-
#ln_s(src_path : String, dest_path : String)
Creates a symbolic link dest_path which points to src_path.
-
#ln_s(src_paths : Enumerable(String), dest_dir : String)
Creates a symbolic link to each path in src_paths inside the dest_dir directory.
-
#ln_sf(src_path : String, dest_path : String)
Like
#ln_s(String, String)
, but overwritesdest_path
if it exists and is not a directory or ifdest_path/src_path
exists. -
#ln_sf(src_paths : Enumerable(String), dest_dir : String)
Creates a symbolic link to each path in src_paths inside the dest_dir directory, ignoring any overwritten paths.
-
#mkdir(paths : Enumerable(String), mode = 511) : Nil
Creates a new directory at the given paths.
-
#mkdir(path : String, mode = 511) : Nil
Creates a new directory at the given path.
-
#mkdir_p(path : String, mode = 511) : Nil
Creates a new directory at the given path, including any non-existing intermediate directories.
-
#mkdir_p(paths : Enumerable(String), mode = 511) : Nil
Creates a new directory at the given paths, including any non-existing intermediate directories.
-
#mv(src_path : String, dest_path : String) : Nil
Moves src_path to dest_path.
-
#mv(srcs : Enumerable(String), dest : String) : Nil
Moves every srcs to dest.
-
#pwd : String
Returns the current working directory.
-
#rm(path : String) : Nil
Deletes the path file given.
-
#rm(paths : Enumerable(String)) : Nil
Deletes all paths file given.
-
#rm_r(path : String) : Nil
Deletes a file or directory path.
-
#rm_r(paths : Enumerable(String)) : Nil
Deletes a list of files or directories paths.
-
#rm_rf(path : String) : Nil
Deletes a file or directory path.
-
#rm_rf(paths : Enumerable(String)) : Nil
Deletes a list of files or directories paths.
-
#rmdir(path : String) : Nil
Removes the directory at the given path.
-
#rmdir(paths : Enumerable(String)) : Nil
Removes all directories at the given paths.
-
#touch(paths : Enumerable(String), time : Time = Time.now)
Attempts to set the access and modification times of each file given in the paths parameter to the value given in time.
-
#touch(path : String, time : Time = Time.now)
Attempts to set the access and modification times of the file named in the path parameter to the value given in time.
Instance Method Detail
Changes the current working directory of the process to the given string path.
require "file_utils"
FileUtils.cd("/tmp")
NOTE Alias of Dir.cd
Changes the current working directory of the process to the given string path and invoked the block, restoring the original working directory when the block exits.
FileUtils.cd("/tmp") { Dir.current } # => "/tmp"
NOTE Alias of Dir.cd
with block
Compares two files filename1 to filename2 to determine if they are identical.
Returns true
if content are the same, false
otherwise.
File.write("file.cr", "1")
File.write("bar.cr", "1")
FileUtils.cmp("file.cr", "bar.cr") # => true
Compares two streams stream1 to stream2 to determine if they are identical.
Returns true
if content are the same, false
otherwise.
File.write("afile", "123")
stream1 = File.open("afile")
stream2 = IO::Memory.new("123")
FileUtils.cmp(stream1, stream2) # => true
Copies the file src_path to the file or directory dest. If dest is a directory, a file with the same basename as src_path is created in dest Permission bits are copied too.
File.chmod("afile", 0o600)
FileUtils.cp("afile", "afile_copy")
File.info("afile_copy").permissions # => 0o600
Copies a list of files src to dest. dest must be an existing directory.
Dir.mkdir("files")
FileUtils.cp({"bar.cr", "afile"}, "files")
Copies a file or directory src_path to dest_path. If src_path is a directory, this method copies all its contents recursively.
FileUtils.cp_r("files", "dir")
Creates a hard link dest_path which points to src_path. If dest_path already exists and is a directory, creates a link dest_path/src_path.
# 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 in src_paths inside the dest_dir directory.
If dest_dir is not a directory, raises an ArgumentError
.
# Create /usr/bin/vim, /usr/bin/emacs, and /usr/bin/nano as hard links
FileUtils.ln(["vim", "emacs", "nano"], "/usr/bin")
Creates a symbolic link dest_path which points to src_path. If dest_path already exists and is a directory, creates a link dest_path/src_path.
# 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 in src_paths inside the dest_dir directory.
If dest_dir is not a directory, raises an ArgumentError
.
# Create symbolic links in src/ pointing to every .c file in the current directory
FileUtils.ln_s(Dir["*.c"], "src")
Like #ln_s(String, String)
, but overwrites dest_path
if it exists and is not a directory
or if dest_path/src_path
exists.
# 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 in src_paths inside the dest_dir directory, ignoring any overwritten paths.
If dest_dir is not a directory, raises an ArgumentError
.
# 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 given paths. The linux-style permission mode can be specified, with a default of 777 (0o777).
FileUtils.mkdir(["foo", "bar"])
Creates a new directory at the given path. The linux-style permission mode can be specified, with a default of 777 (0o777).
FileUtils.mkdir("src")
NOTE Alias of Dir.mkdir
Creates a new directory at the given path, including any non-existing intermediate directories. The linux-style permission mode can be specified, with a default of 777 (0o777).
FileUtils.mkdir_p("foo")
NOTE Alias of Dir.mkdir_p
Creates a new directory at the given paths, including any non-existing intermediate directories. The linux-style permission mode can be specified, with a default of 777 (0o777).
FileUtils.mkdir_p(["foo", "bar", "baz", "dir1", "dir2", "dir3"])
Moves every srcs to dest.
FileUtils.mv(["foo", "bar"], "src")
Deletes all paths file given.
FileUtils.rm(["dir/afile", "afile_copy"])
Deletes a file or directory path. If path is a directory, this method removes all its contents recursively.
FileUtils.rm_r("dir")
FileUtils.rm_r("file.cr")
Deletes a list of files or directories paths. If one path is a directory, this method removes all its contents recursively.
FileUtils.rm_r(["files", "bar.cr"])
Deletes a file or directory path. If path is a directory, this method removes all its contents recursively. Ignore all errors.
FileUtils.rm_rf("dir")
FileUtils.rm_rf("file.cr")
FileUtils.rm_rf("non_existent_file")
Deletes a list of files or directories paths. If one path is a directory, this method removes all its contents recursively. Ignore all errors.
FileUtils.rm_rf(["dir", "file.cr", "non_existent_file"])
Removes all directories at the given paths.
FileUtils.rmdir(["dir1", "dir2", "dir3"])
Attempts to set the access and modification times of each file given in the paths parameter to the value given in time.
If the file does not exist, it will be created.
FileUtils.touch(["foo", "bar"])
Attempts to set the access and modification times of the file named in the path parameter to the value given in time.
If the file does not exist, it will be created.
FileUtils.touch("afile.cr")
NOTE Alias of File.touch