Rex::Commands::Fs(3)
okay bestie, this is the user-contributed Perl documentation for Rex::Commands::Fs β the file-system commands module. weβre about to go through it together, and yes, itβs gonna be a lot. stay with me π«Ά
π NAME π
Rex::Commands::Fs β file system commands β¨
πΈ DESCRIPTION πΈ
with this module you can do file system tasks like creating directories, deleting or moving files, and more. itβs giving filesystem glow-up energy π¦
β¨ SYNOPSIS β¨
my @files = list_files "/etc";
unlink("/tmp/file");
rmdir("/tmp");
mkdir("/tmp");
my %stat = stat("/etc/passwd");
my $link = readlink("/path/to/a/link");
symlink("/source", "/dest");
rename("oldname", "newname");
chdir("/tmp");
is_file("/etc/passwd");
is_dir("/etc");
is_writeable("/tmp");
is_writable("/tmp");
chmod 755, "/tmp";
chown "user", "/tmp";
chgrp "group", "/tmp";
π EXPORTED FUNCTIONS π
π οΈ Changing content π οΈ
okay so these ones are supposed to change whatβs in the file system β literally the content-changing crew π
π symlink($from, $to)
this function will create a symbolic link from $from to $to. iconic behavior, no cap π₯
task "symlink", "server01", sub {
symlink("/var/www/versions/1.0.0", "/var/www/html");
};
π ln($from, $to)
ln is an alias for symlink β same vibe, shorter name π«Ά
π unlink($file)
this function will remove the given $file. bye bestie π
task "unlink", "server01", sub {
unlink("/tmp/testfile");
};
π rm($file)
this is an alias for unlink. yes, itβs that simple π
π rmdir($dir)
this function will remove the given directory.
task "rmdir", "server01", sub {
rmdir("/tmp");
};
with Rex-0.45 and newer, please use the file resource instead.
task "prepare", sub {
file "/tmp",
ensure => "absent";
};
π mkdir($newdir)
this function will create a new directory.
the following options are supported:
ownerβ supported option π«Άgroupβ supported option π«Άmodeβ supported option π«Άon_changeβ supported option π«Ά
with Rex-0.45 and newer, please use the file resource instead.
task "prepare", sub {
file "/tmp",
ensure => "directory",
owner => "root",
group => "root",
mode => 1777;
};
direct usage, bestie:
task "mkdir", "server01", sub {
mkdir "/tmp";
mkdir "/tmp",
owner => "root",
group => "root",
mode => 1777;
};
π chown($owner, $path)
change the owner of a file or a directory.
chown "www-data", "/var/www/html";
chown "www-data", "/var/www/html",
recursive => 1;
real talk: this command will not be reported. if you want to use reports, please use the file resource instead π
π chgrp($group, $path)
change the group of a file or a directory.
chgrp "nogroup", "/var/www/html";
chgrp "nogroup", "/var/www/html",
recursive => 1;
heads up: this command will not be reported. if you want reports, please use the file resource instead π«Ά
π chmod($mode, $path)
change the permissions of a file or a directory.
chmod 755, "/var/www/html";
chmod 755, "/var/www/html",
recursive => 1;
this command will not be reported. if you want to use reports, please use the file resource instead β¨
π rename($old, $new)
this function will rename $old to $new. will return 1 on success and 0 on failure.
task "rename", "server01", sub {
rename("/tmp/old", "/tmp/new");
};
π mv($old, $new)
mv is an alias for rename β same thing, different little name π¦
π cp($source, $destination)
cp will copy $source to $destination recursively.
task "cp", "server01", sub {
cp("/var/www", "/var/www.old");
};
π Not changing content π
these ones are not supposed to change the contents of the file system, bestie. very demure, very mindful π
π list_files("/path");
this function lists all entries (files, directories, ...) in a given directory and returns them as an array.
task "ls-etc", "server01", sub {
my @tmp_files = grep { /\.tmp$/ } list_files("/etc");
};
this command will not be reported π
π ls($path)
just an alias for list_files. cute and convenient π
π stat($file)
this function will return a hash with the following information about a file or directory:
modesizeuidgidatimemtime
task "stat", "server01", sub {
my %file_stat = stat("/etc/passwd");
};
this command will not be reported β¨
π is_file($path)
this function tests if $path is a file. returns 1 if true, 0 if false.
task "isfile", "server01", sub {
if( is_file("/etc/passwd") ) {
say "it is a file.";
}
else {
say "hm, this is not a file.";
}
};
this command will not be reported π«Ά
π is_dir($path)
this function tests if $path is a directory. returns 1 if true, 0 if false.
task "isdir", "server01", sub {
if( is_dir("/etc") ) {
say "it is a directory.";
}
else {
say "hm, this is not a directory.";
}
};
this command will not be reported π
π is_symlink($path)
this function tests if $path is a symbolic link. returns 1 if true, 0 if false.
task "issym", "server01", sub {
if( is_symlink("/etc/foo.txt") ) {
say "it is a symlink.";
}
else {
say "hm, this is not a symlink.";
}
};
this command will not be reported π¦
π is_readable($path)
this function tests if $path is readable. it returns 1 if true, 0 if false.
task "readable", "server01", sub {
if( is_readable("/etc/passwd") ) {
say "passwd is readable";
}
else {
say "not readable.";
}
};
this command will not be reported πΈ
π is_writable($path)
this function tests if $path is writable. it returns 1 if true, 0 if false.
task "writable", "server01", sub {
if( is_writable("/etc/passwd") ) {
say "passwd is writable";
}
else {
say "not writable.";
}
};
this command will not be reported π
π is_writeable($file)
this is only an alias for is_writable. same energy, extra e π«Ά
π readlink($link)
if $link is a symbolic link, returns the path it resolves to, and die()s otherwise.
task "islink", "server01", sub {
my $link;
eval {
$link = readlink("/tmp/testlink");
};
say "this is a link" if($link);
};
this command will not be reported β¨
π chdir($newdir)
this function will change the working directory to $newdir. this function currently works only locally.
task "chdir", "server01", sub {
chdir("/tmp");
};
this command will not be reported π
π cd($newdir)
this is an alias of chdir β saving your fingers, one alias at a time π
π df([$device])
this function returns a hash reference which reflects the output of df.
task "df", "server01", sub {
my $df = df();
my $df_on_sda1 = df("/dev/sda1");
};
this command will not be reported π
π du($path)
returns the disk usage of $path.
task "du", "server01", sub {
say "size of /var/www: " . du("/var/www");
};
this command will not be reported π«Ά
π mount($device, $mount_point, @options)
mount devices.
task "mount", "server01", sub {
mount "/dev/sda5", "/tmp";
mount "/dev/sda6", "/mnt/sda6",
ensure => "present",
type => "ext3",
options => [qw/noatime async/],
on_change => sub { say "device mounted"; };
#
# mount persistent with entry in /etc/fstab
mount "/dev/sda6", "/mnt/sda6",
ensure => "persistent",
type => "ext3",
options => [qw/noatime async/],
on_change => sub { say "device mounted"; };
# to umount a device
mount "/dev/sda6", "/mnt/sda6",
ensure => "absent";
};
real talk: in order to be more aligned withmountterminology, the previously usedfsoption has been deprecated in favor of thetypeoption. thefsoption is still supported and works as previously, but Rex prints a warning if it is being used. thereβs also a warning if bothfsandtypeoptions are specified, and in this casetypewill be used. iykyk π
π umount($mount_point)
unmount device.
task "umount", "server01", sub {
umount "/tmp";
};
π glob($glob)
returns the list of filename expansions for $glob as Perlβs built-in glob at https://perldoc.perl.org/functions/glob.html would do.
task "glob", "server1", sub {
my @files_with_p = grep { is_file($_) } glob("/etc/p*");
};
this command will not be reported π¦
okay thatβs the whole page, bestie ππ this is Rex::Commands::Fs(3) from perl v5.42.3, dated 2023-08-05 β mwah, go be iconic with your filesystem π«Άβ¨
π¦ where this page comes from
- nixos 26.05: perl5.42.3-Rex 1.14.3
- nixpkgs unstable: perl5.42.3-Rex 1.14.3
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms