# The ‹chmod› command serves to change permission bits of files # (well, i-nodes, so that covers directories, specials and so on). # Let's start by creating an empty file: echo creating file.txt... touch file.txt echo hello > file.txt # this also works cat file.txt # this works as expected # We have checked that we can read the file and write into it -- # this is normal and expected. However, we can change the # permissions (using ‹chmod›) to remove either of those access # rights (or both). First, we check what the initial permissions # were: ls -l file.txt # Let's look at ‹chmod› then. What it does is manipulate the # individual bits, but using a somewhat different syntax. The # formatting we see in the output of ‹ls› is optimized for output, # but writing out the permissions in this form could be somewhat # tedious, especially if all we want is to flip one of the bits. For # this reason, ‹chmod› gives names to each of the triplet: ‹u› for # user (owner), ‹g› for owning group and ‹o› for others. Beware that # ‹o› does «not» mean the owner! # What we can do now is tell ‹chmod› which bits we are interested in # and what to do with them. We can either set bits with ‹+› or # remove them with ‹-›. For instance, ‹g+w› would mean ‘add the ‹w› # bit to the group triplet’, or in other words, let the users in the # owning group write into this file. To remove the write permission # from ourselves (the owner), we will use ‹u-w›, like this: echo echo removing the write permission and testing... chmod u-w file.txt ls -l file.txt echo world >> file.txt # Likewise, we can remove the read permission from ourselves, using # ‹u-r›. Then we won't be able to read from the file either: echo echo removing the read permission and testing... chmod u-r file.txt ls -l file.txt cat file.txt # permission error # Multiple bits can be specified in a single group. Let's restore # the original permissions: echo echo "restoring permissions (and testing again)" chmod u+rw file.txt ls -l file.txt echo world >> file.txt cat file.txt # If you want to specify multiple groups in a single ‹chmod› # command, that can be done too: separate the groups with a comma, # like this: ‹g+r,o-w›. You can also set the permissions # directly, using assignment, or combine the two alternatives: # ‹chmod u=rw,g+x›). # With that out of the way, let's try an exercise (in ‹1.txt›).