Home » Faqs » System » Chmod |
From perl you want to set the permissions on a file or directory.
There is a perl function called chmod. It can be called on files or directories. You can use it as follows:
#!/usr/bin/perl use strict; use warnings; # # Chmod a file # my $filename = "test.txt"; chmod 0600, $filename or die "Couldn't chmod $filename: $!"; # # Chmod an existing directory # my $dir = "testchmod"; chmod(0660, $dir) or die "Couldn't chmod $dir: $!"; exit 0;
You can also use chmod on a list of files. Chmod returns the number of files that were modified, so you can tell if all the chmods were successful.
#!/usr/bin/perl use strict; use warnings; # # Chmod a file # my @files = ("test.txt", "test2,txt", "test3.txt"); my $res = chmod 0600, @files; print "Modified $res files\n"; die $! if ($!); exit 0;
If two of the three files existed, the output of this program would be:
Modified 2 files No such file or directory at ./test.pl line 12.
perldoc -f chmod