Copy a file
The fs.copy function is used to copy a file from one location to another, the first argument is a string of the source file path, and the second argument is a string of the destination file path.
If the destination file exists, it will overwrite the file.
INFO
The fs.copy
function is only used to copy a file or a symlink.
nv,
use std.fs;
fn main() throws {
try fs.copy("path/to/source.txt", "path/to/destination.txt");
}
Link a file
We have fs.link method to create a hard link to a file, and fs.symlink method to create a symbolic link to a file.
nv,
use std.fs;
fn main() throws {
try fs.link("path/to/source.txt", "path/to/destination.txt");
try fs.symlink("path/to/source.txt", "path/to/destination.txt");
}
You also can use fs.readlink method to read the target of a symbolic link, this will return the string path of the link source.
nv,
use std.fs;
fn main() throws {
try fs.symlink("path/to/source.txt", "path/to/destination.txt");
let target = try fs.readlink("path/to/destination.txt");
println(target);
// Output: path/to/source.txt
}
And the fs.unlink method to remove a link.
Actually, the
fs.unlink
is a alias offs.remove_file
.