Change the permissions of a list of files.
The chmod function changes the file permissions of one or more files. It takes a numeric mode as the first argument followed by a list of filenames, and returns the number of files successfully changed.
The mode is typically specified as an octal number, such as 0755 for owner read/write/execute with group and others having read/execute. Unlike the shell command, Perl's chmod does not accept symbolic mode strings.
chmod is essential for scripts that create files and need to set specific access rights, such as making scripts executable or restricting access to configuration files containing sensitive data.
| Name | Description | Optional |
|---|---|---|
MODE |
Numeric (octal) permission mode to apply. | No |
LIST |
One or more file paths to modify. | No |
# Make a script executable
chmod 0755, "script.pl"
or warn "chmod failed: $!";
# Restrict access to a config file
chmod 0600, "secret.conf";
# Change multiple files at once
my $count = chmod 0644, glob("*.txt");
print "Changed $count files\n";