|Home » Learning Curve
Files, Ownership, Permissions, Stuff Like That (2)
It's really simple. Really it is.
[Note: if Unix is new to you then you need to read the first part in this series before proceeding. Click here.]
The first article in this series ended with a question: if Unix files have permissions bits for read, write, execute and the article explains what read and write mean, then what happens with the execute bit?
Yes indeed. Here's a new experiment to illustrate what the execute bit is all about.
Launch TextEdit again. TextEdit might be set up to use rich text but you don't want that now. In such case, go to your Format menu and select Make Plain Text. Your TextEdit window should now look like this.
Now type in the following.
echo Hello, World
Now save this file on disk in your Documents folder as 'hello' (no extension). Tick off the option to add the extension 'txt'. (You can leave the plain text encoding as is.)
You've just written a Unix shell script. That's something that can be executed (run) on your system. When it executes, it will simply output 'Hello, World' and exit. So try it - see how far you get.
-bash: ~/Documents/hello: Permission denied
What a cheeky thing to say! Permission denied? Well yes of course - you haven't set the execute bit yet. So try again.
$ chmod 755 ~/Documents/hello # chmod changes the 'mode' (permissions) of a file
That's much better!
To run any script - such as your 'hello' script or the myriad startup scripts on the system; or to run ordinary 'command line programs' - tools as they're sometimes called; to run ordinary OS X GUI applications: you need to have execute permission.
You can stop any script, any tool, any application from running by removing execute permission.
Here's a cute trick with Unix: everything on Unix - and that includes directories - is a file. Even directories are just files.
So the same 'rwx' permissions apply to directories as well. And reading a directory is easy enough to understand - you want to be able to list its contents. And writing to a directory the same thing - you want to be able to modify its contents. By renaming a file, adding a file, removing a file. But what does 'execute' mean for directories?
Execute for directories means 'enter'. Try the following.
$ cd ~/Documents
$ mkdir TEST # Make the directory 'TEST' under 'Documents'
$ cd TEST # Enter the directory 'TEST'
$ pwd # print your working directory
~/Documents/TEST # Yep you're in 'TEST' alright!
$ cd .. # Scoot back up (to the parent directory 'Documents')
$ chmod 644 TEST # Change permissions to what 'hello' originally was
$ cd TEST # try to enter again
-bash: cd: TEST: Permission denied # Nope!
The working directory is a cornerstone concept in Unix. You can experiment on your own to see how nested hierarchies of directories can effectively prohibit access.
More Bits? More Stuff?
Yes there are more file permissions bits. There are six all told. But two of these can't be modified. And for the last one: that could very well be the subject of another article.
And there are further ways of protecting files. What about them? Unix wasn't learned in a day. Not even in two days.
Learning Curve: Files, Ownership, Permissions, Stuff Like That