Perl Chmod Command: How to Set and Remove File and Directory Permissions
PERL stands for Practical Extraction and Report Language. It’s a programming language which runs on different platforms like Windows, Unix and Macintosh. C or Unix shell developers will find it easy to migrate to this platform. It’s an open source software and is known for its stability. The current version of Perl is 5.16.2. Perl provides support for both procedural and object-oriented programs. It’s easily able to connect to databases such as Oracle, Sybase, MySQL and more. This language is convenient to use with markup languages such as HTML, XML and others. Perl offer support for Unicode and is Y2K compliant. Perl is quite easy to learn, even if you’re new to programming. Take this introductory Perl course to see for yourself.
Today we walk you through the Perl change mode command. This command is used to set permissions to the directories and files. We assume that you have the basic knowledge of programming. If not you can take this introductory course to programming to help get you started.
What is Chmod?
Chmod is an acronym for Change Mode. It comes as a set of three numbers. Each of these three numbers is calculated by adding three other numbers. This command is used for both file and directories. The syntax for this command is as follows.
chmod MODE, LIST
Here the Mode is the permissions and list is the file(s) whose permission is to set/changed according to the mode.
You can learn more about chmod in this Linux course (chmod in Perl and Linux work the same).
Modes of the Command Chmod
- U stands for the user who is the owner of the file.
- g stands for group who are the members of the file’s group
- stands for other who are neither the members or the file owner.
- a stands for all, i.e. ugo.
Operators for Chmod Command
These operators are used to set or remove the permissions of the files. They are as follows.
- The ‘+’ operator adds the specified modes.
- The ‘-‘ operator removes the specified modes.
- The “=” operator results in making the specified modes into the exact mode for the file.
Modes of the Chmod Command
The three basic modes in Perl are listed below. Take a look.
- ‘r’ stands for read permission for a file or directory’s content.
- ‘w’ stands for write permission for a file or directory.
- ‘x’ stands for execute permission for a file or recursive permission for a directory.
Special execute permission
‘X’ is used to set special execute permission to both the files and directories. It is usually used along with the -R option. For example, chmod -R a+X <dir name> Remove the execute permission for all files in a directory tree. However.it allows directory browsing.
The Numerical Values and the Various Combination of the Modes
The value of Read(r) is 4, Write(w) is 2 and Execute(x) is 1. The following table lists all the possible combinations which a file or directory can have using the three command types.
Digit rwx permission set
0 — no access to the file
1 –x The file has only execute permission
2 -w- Only write permission is set
3 -wx The file has both write and execute permission but no read permission.
4 r– Only read permission is given.
5 r-x read and execute permissions are given.
6 rw- read and write permission is set for the file.
7 rwx All the three permissions are set, read, write and execute.
Normally, for files the permission settings are 777, 755, 666 or 644. While for the directories the settings are 777 or 755. For CGI (Common Gateway Interface) scripts the settings are 755, the data files are set to 666 and the configuration files have the settings 644. The first number indicates the host , the second stands for the group, and the third number stands for the others, the visitors who visit the site.
Examples Illustrating the Use of Chmod Command
- chmod(“+x”,”file1″,”file2″)-Here you are setting the execute permission to the files file 1 and file2 for all types of users.
- chmod(“o=,g-w”,”file1″,”file2″)– This command removes all the three permission for others,here it is the world. The write permission for the group is removed.
- chmod(“=u”,”file1″,”file2″) -Here all the permissions are set for all the users.
- chmod g+w <dir name>– This command adds write permission to the group’s permissions for a directory
- chmod a-w <filename>– This command removes write permission for all three type of users.
- chmod ug=rx<filename>– This command sets the permissions read and execute for the user and group. Here there is no write permission.
- chmod a+r <filename>– Here the read permission is set for all.
- chmod a-x <filename>– The execute permission is removed for all.
- chmod a+rw <filename>-This sets the permission to read and write for all. Note that there is no execute permission.
- chmod +rwx <filename>– This command sets the default permission which is -rwxr-xr-x.
- chmod u=rw,go= <filename>– You use this command to set read and write permission for the owner of the file. It allows all permissions for group and others.
- chmod -R u+w,go-w <directoryname>- This command adds write permission for the user and removes write permission for the others and the group for all the files in this directory.
- chmod = <filename>– You can use this command to remove permissions for all.
- chmod 777 <filename>– You can use this command to set all permissions for all.
- chmod 664 <filename>– This is used to set read and write for owner and group and only read permission for others.
- chmod 0755 <filename>– The 0 indicates no special modes. This command allows all permissions to users and only read and execute permission to group and others.
- chmod -R u+rwX,g-rwx,o-rwx <directoryname>– This command sets all permission for the owner of the directory, read and write f for the owner of the files within the directory and no permissions for group and others.
Example 1: Program to Call Chmod in Perl
#!/usr/bin/perl use strict; use warnings; my $filename = "example.txt"; chmod 0600, $filename or die "Couldn't change the permission to $filename: $!"; my $dir = "exampledir"; chmod(0660, $dir) or die "Couldn't set the permission to $dir: $!"; exit 0;
This program uses chmod function to set permissions to a file and directory. In case chmod doesn’t execute properly, an error message is displayed on the output screen. The first line of the above program is called the shebang line. It is used to make the Perl script portable to Unix and Cygwin. It is recommended to use the same even on Windows. ‘use strict’ and ‘ use warnings’ statements are used to tell the Perl interpreter to check for programming errors. The my keyword declares the listed variables to be local to the enclosing block. Die command is used to display a message for the user.
Example 2: Program to Call Chmod on a List of Files
#!/usr/bin/perl use strict; use warnings; my @files = ("example1.txt", "example2,txt", "example3.txt"); my $res = chmod 0600, @files; print "Modified $res files\n"; die $! if ($!); exit 0;
In this program, we use the chmod command on three files. The variable res accepts the number of files successfully modified by the chmod command.
Hope this article helped you understand the Perl chmod command better. Do experiment with these examples and try them out for yourself. You may also want to learn the basics of other scripting languages like bash or Python (you can learn with this course) to compare what works best for you.
Perl students also learn
Empower your team. Lead the industry.
Get a subscription to a library of online courses and digital learning tools for your organization with Udemy Business.