Sometimes I see chmod commands that use four octal digits instead of three -- what is the optional first digit for? For example, chmod 777 is equivalent to chmod a+rwx; what's the same command for chmod 2777?
What is the first chmod octal digit in a four-digit value for?
chmod
Please note that chmod 777 filename is the equivalent of chmod 0777 filename in this example.The first octal digit sets the setuid, setgid and sticky bits (see this article for more details on setuid/setgid).  octal 2 means to set group ID on the file.  So, the equivalent would be to do a chmod a+rwx filename, then chmod g+s filename.  The chmod info page does explain this in more detail.