So you have written your first program in Perl and it probably looks something like this
#!/usr/local/bin/perl
# We will call the program hello_world
print “Hello world\n”
Now you try to run it with the command ./hello_world, only to find that it won’t run. Well the reason is obvious, well to experienced users anyway. The problem is that your program doesn’t have permission to be executed. Instead you first need to run chmod u+x hello_world, then ./hello_world will run as expected.
The function of the chmod command is to change the mode of a files. The modes are permissions and special modes. I will only touch on permissions in this post. The format for this command is as follows
chmod [references] [operator] [modes] file1...
references: who you want to apply the changes to
operator: how the changes are to be made
modes: which changes are to be made
The references are; u, o, g, and a. u is the owner of the file. g is users who are members of the file’s group. o is outsider, members who are not specified by either u or g. Finally a is all, this is equivalent to uog.
The operators are; +, -, and =. As can be expected you use + to add permissions and - to remove them. The = is a little harder to explain but is made perfectly clear with an example (which is later in this post).
The modes are; r, w, x. They stand for read, write, and executable respectively.
Well now I suppose you would like to see some examples of this in use. For all these examples we will assume that hello_world has the following initial modes: -rwxr-xr-x, this means that the owner (the first grouping of rwx) has the ability to read, write, and execute. However, group members (the second grouping) and outsiders (the last grouping) can only read and execute
Example 1: Make it so that the owner only has executable permission. After using the following command, the file’s modes will look like —xr-xr-x
chmod u=x hello_world
Example 2: Make is so that the owner can only read and execute. After using the following command, the file’s modes will look like -r-xr-xr-x
chmod u-w hello_world
Example 3: Make it so that group users and outsiders can read and write only. While not affecting the owner’s permissions. After using the following command the file’s modes will look like -rwxrw-rw-
chmod og=rw hello_world
Example 4: Make it so that no one has execute ability. The file’s modes will look like -rw-r–r– after using the following command.
chmod a-x hello_world
Example 5: Make is so that people that aren’t group members or the owner have write permission. The file’s modes will look like -rwxr-xrwx after using the following command.
chmod o+w hello_world
So that wasn’t too bad, and hopefully you noticed how the = operator works. Finally, it is worth mentioning that in order to see a file’s modes you only need to use ls -l file. For more information on chmod check out this Wikipedia entry, and for examples follow this link.