1Rex::Commands::Fs(3) User Contributed Perl Documentation Rex::Commands::Fs(3)
2
3
4
6 Rex::Commands::Fs - File system commands
7
9 With this module you can do file system tasks like creating
10 directories, deleting or moving files, and more.
11
13 my @files = list_files "/etc";
14
15 unlink("/tmp/file");
16
17 rmdir("/tmp");
18 mkdir("/tmp");
19
20 my %stat = stat("/etc/passwd");
21
22 my $link = readlink("/path/to/a/link");
23 symlink("/source", "/dest");
24
25 rename("oldname", "newname");
26
27 chdir("/tmp");
28
29 is_file("/etc/passwd");
30 is_dir("/etc");
31 is_writeable("/tmp");
32 is_writable("/tmp");
33
34 chmod 755, "/tmp";
35 chown "user", "/tmp";
36 chgrp "group", "/tmp";
37
39 Changing content
40 These commands are supposed to change the contents of the file system.
41
42 symlink($from, $to)
43
44 This function will create a symbolic link from $from to $to.
45
46 task "symlink", "server01", sub {
47 symlink("/var/www/versions/1.0.0", "/var/www/html");
48 };
49
50 ln($from, $to)
51
52 "ln" is an alias for "symlink"
53
54 unlink($file)
55
56 This function will remove the given $file.
57
58 task "unlink", "server01", sub {
59 unlink("/tmp/testfile");
60 };
61
62 rm($file)
63
64 This is an alias for "unlink".
65
66 rmdir($dir)
67
68 This function will remove the given directory.
69
70 task "rmdir", "server01", sub {
71 rmdir("/tmp");
72 };
73
74 With Rex-0.45 and newer, please use the file resource instead.
75
76 task "prepare", sub {
77 file "/tmp",
78 ensure => "absent";
79 };
80
81 mkdir($newdir)
82
83 This function will create a new directory.
84
85 With Rex-0.45 and newer, please use the file resource instead.
86
87 task "prepare", sub {
88 file "/tmp",
89 ensure => "directory",
90 owner => "root",
91 group => "root",
92 mode => 1777;
93 };
94
95 task "mkdir", "server01", sub {
96 mkdir "/tmp";
97
98 mkdir "/tmp",
99 owner => "root",
100 group => "root",
101 mode => 1777;
102 };
103
104 chown($owner, $path)
105
106 Change the owner of a file or a directory.
107
108 chown "www-data", "/var/www/html";
109
110 chown "www-data", "/var/www/html",
111 recursive => 1;
112
113 This command will not be reported.
114
115 If you want to use reports, please use the file resource instead.
116
117 chgrp($group, $path)
118
119 Change the group of a file or a directory.
120
121 chgrp "nogroup", "/var/www/html";
122
123 chgrp "nogroup", "/var/www/html",
124 recursive => 1;
125
126 This command will not be reported.
127
128 If you want to use reports, please use the file resource instead.
129
130 chmod($mode, $path)
131
132 Change the permissions of a file or a directory.
133
134 chmod 755, "/var/www/html";
135
136 chmod 755, "/var/www/html",
137 recursive => 1;
138
139 This command will not be reported.
140
141 If you want to use reports, please use the file resource instead.
142
143 rename($old, $new)
144
145 This function will rename $old to $new. Will return 1 on success and 0
146 on failure.
147
148 task "rename", "server01", sub {
149 rename("/tmp/old", "/tmp/new");
150 };
151
152 mv($old, $new)
153
154 "mv" is an alias for "rename".
155
156 cp($source, $destination)
157
158 "cp" will copy $source to $destination recursively.
159
160 task "cp", "server01", sub {
161 cp("/var/www", "/var/www.old");
162 };
163
164 Not changing content
165 These commands should not change the contents of the file system.
166
167 list_files("/path");
168
169 This function lists all entries (files, directories, ...) in a given
170 directory and returns them as an array.
171
172 task "ls-etc", "server01", sub {
173 my @tmp_files = grep { /\.tmp$/ } list_files("/etc");
174 };
175
176 This command will not be reported.
177
178 ls($path)
179
180 Just an alias for "list_files".
181
182 stat($file)
183
184 This function will return a hash with the following information about a
185 file or directory:
186
187 mode
188 size
189 uid
190 gid
191 atime
192 mtime
193
194 task "stat", "server01", sub {
195 my %file_stat = stat("/etc/passwd");
196 };
197
198 This command will not be reported.
199
200 is_file($path)
201
202 This function tests if $path is a file. Returns 1 if true, 0 if false.
203
204 task "isfile", "server01", sub {
205 if( is_file("/etc/passwd") ) {
206 say "it is a file.";
207 }
208 else {
209 say "hm, this is not a file.";
210 }
211 };
212
213 This command will not be reported.
214
215 is_dir($path)
216
217 This function tests if $path is a directory. Returns 1 if true, 0 if
218 false.
219
220 task "isdir", "server01", sub {
221 if( is_dir("/etc") ) {
222 say "it is a directory.";
223 }
224 else {
225 say "hm, this is not a directory.";
226 }
227 };
228
229 This command will not be reported.
230
231 is_symlink($path)
232
233 This function tests if $path is a symbolic link. Returns 1 if true, 0
234 if false.
235
236 task "issym", "server01", sub {
237 if( is_symlink("/etc/foo.txt") ) {
238 say "it is a symlink.";
239 }
240 else {
241 say "hm, this is not a symlink.";
242 }
243 };
244
245 This command will not be reported.
246
247 is_readable($path)
248
249 This function tests if $path is readable. It returns 1 if true, 0 if
250 false.
251
252 task "readable", "server01", sub {
253 if( is_readable("/etc/passwd") ) {
254 say "passwd is readable";
255 }
256 else {
257 say "not readable.";
258 }
259 };
260
261 This command will not be reported.
262
263 is_writable($path)
264
265 This function tests if $path is writable. It returns 1 if true, 0 if
266 false.
267
268 task "writable", "server01", sub {
269 if( is_writable("/etc/passwd") ) {
270 say "passwd is writable";
271 }
272 else {
273 say "not writable.";
274 }
275 };
276
277 This command will not be reported.
278
279 is_writeable($file)
280
281 This is only an alias for "is_writable".
282
283 readlink($link)
284
285 If $link is a symbolic link, returns the path it resolves to, and
286 "die()"s otherwise.
287
288 task "islink", "server01", sub {
289 my $link;
290 eval {
291 $link = readlink("/tmp/testlink");
292 };
293
294 say "this is a link" if($link);
295 };
296
297 This command will not be reported.
298
299 chdir($newdir)
300
301 This function will change the working directory to $newdir. This
302 function currently works only locally.
303
304 task "chdir", "server01", sub {
305 chdir("/tmp");
306 };
307
308 This command will not be reported.
309
310 cd($newdir)
311
312 This is an alias of "chdir".
313
314 df([$device])
315
316 This function returns a hash reference which reflects the output of
317 "df".
318
319 task "df", "server01", sub {
320 my $df = df();
321 my $df_on_sda1 = df("/dev/sda1");
322 };
323
324 This command will not be reported.
325
326 du($path)
327
328 Returns the disk usage of $path.
329
330 task "du", "server01", sub {
331 say "size of /var/www: " . du("/var/www");
332 };
333
334 This command will not be reported.
335
336 mount($device, $mount_point, @options)
337
338 Mount devices.
339
340 task "mount", "server01", sub {
341 mount "/dev/sda5", "/tmp";
342 mount "/dev/sda6", "/mnt/sda6",
343 ensure => "present",
344 type => "ext3",
345 options => [qw/noatime async/],
346 on_change => sub { say "device mounted"; };
347 #
348 # mount persistent with entry in /etc/fstab
349
350 mount "/dev/sda6", "/mnt/sda6",
351 ensure => "persistent",
352 type => "ext3",
353 options => [qw/noatime async/],
354 on_change => sub { say "device mounted"; };
355
356 # to umount a device
357 mount "/dev/sda6", "/mnt/sda6",
358 ensure => "absent";
359
360 };
361
362 In order to be more aligned with "mount" terminology, the previously
363 used "fs" option has been deprecated in favor of the "type" option. The
364 "fs" option is still supported and works as previously, but Rex prints
365 a warning if it is being used. There's also a warning if both "fs" and
366 "type" options are specified, and in this case "type" will be used.
367
368 umount($mount_point)
369
370 Unmount device.
371
372 task "umount", "server01", sub {
373 umount "/tmp";
374 };
375
376 glob($glob)
377
378 Returns the list of filename expansions for $glob as Perl's built-in
379 glob <https://perldoc.perl.org/functions/glob.html> would do.
380
381 task "glob", "server1", sub {
382 my @files_with_p = grep { is_file($_) } glob("/etc/p*");
383 };
384
385 This command will not be reported.
386
387
388
389perl v5.30.2 2020-04-06 Rex::Commands::Fs(3)