A short hands-on introduction to the concept of unix for the Mac user.
Hey, Mac user. Did you know that there is a foundation of Unix underneath your Mac OSX operating system? Unix? What’s that? Unix was an important part of how computer operating systems came to be what they are. And unix harkens back to the old days of computing, when communication with your computer was by way of a keyboard and text screen. You type commands to your computer and it responds with text on the screen. Besides its nostalgic appeal and historical interest, there is a great deal of raw computational power in a text based command system that unix made possible, and that is still useful to this day, and thus, the Mac user should take the opportunity learn a little history of some revolutionary principles that unix introduced to the world of computers, while learning to control your computer in a completely different way.
Unix is not any one single operating system or user interface, but a whole family of them, related in an evolutionary hierarchy with ever growing branches. In that sense unix is more of a concept than any one particular software package. The revolutionary and evolutionary aspect of unix is its fundamental reprogrammability. The user is encouraged from the outset to customize their environment by defining environment variables and aliases, writing shell scripts and c programs, to expand on and improve on the basic original unix, and besides the hard-core kernel of unix that does the lowest level tasks, the great majority of the unix interface is an accumulation of scripts and programs that people have written over the years, and are still writing, that expand on and improve on the system. If you don’t like the way a command works, redefine it to make it work the way you want. And that was the beginning of a long evolution of a number of different variations of unix all evolving in parallel, while also borrowing from each other their best features, and that evolution continues to this day.
To open a unix command window on your Mac use Finder to find to your
Applications > Utilities > Terminal.app,
First, type “whoami”. This is what happens when I do it. (The $ [dollar] sign below is the original standard unix prompt, although on your Mac the prompt is more likely “slehars-MacBook:~ slehar$” or something similar.)
$ whoami slehar
It answers that I am slehar, the username I chose for myself when I set up my account. Next, type “pwd” which means “print working directory” or print the name of the directory that I am currently working in. Here’s what happens when I type it.
$ pwd /Users/slehar
Now lets see what is in this directory, lets get a directory listing, which is done with the command “ls”. Here’s what my directory contained when I wrote this.
$ ls Applications Library Public bubw2 Desktop Movies Sites cprogs Documents Music bin sandbox Downloads Pictures bp tst
These are all files or folders in my directory. We can get a clearer picture of what they are with the “-F” command-line argument to the “ls” command, by typing “ls -F” which yields
$ ls -F Applications/ Library/ Public/ bubw2/ Desktop/ Movies/ Sites/ cprogs/ Documents/ Music/ bin/ sandbox/ Downloads/ Pictures/ bp@ tst*
The names that have a trailing/ slash/ are directories that contain files or other directories, whereas the names without a trailing slash are the names of files. Here’s another variation on the ls command using the “-a” command-line argument to show all files and folders. There is an old unix convention that any filenames that .begin .with .a .dot are “invisible” files, they are not normally listed in a directory listing, but they can be made visible again with the -a flag as shown below
ls -a . Documents Music .. Downloads Pictures .CFUser TextEncoding Library Public Desktop Movies Sites
Here we see new files like .CFUser. There are also always two special files in every directory, one called . (dot), which means THIS directory, the other called .. (dot dot) which means the parent of this directory. Typing “ls” is equivalent to typing “ls .” which means list THIS directory.
To get the most detailed directory listing use the “-l” long listing command-line flag
$ ls -l drwxr-xr-x 2 slehar staff 68 Mar 6 13:49 Applications drwx------+ 4 slehar staff 136 Mar 28 06:59 Desktop drwx------+ 16 slehar staff 544 Mar 28 06:57 Documents drwx------+ 59 slehar staff 2006 Mar 31 06:48 Downloads drwx------+ 38 slehar staff 1292 Mar 21 19:48 Library drwx------+ 7 slehar staff 238 Feb 2 14:20 Movies drwx------+ 8 slehar staff 272 Feb 2 14:22 Music drwx------+ 16 slehar staff 544 Mar 22 16:21 Pictures drwxr-xr-x+ 5 slehar staff 170 Jan 9 19:33 Public drwxr-xr-x+ 5 slehar staff 170 Jan 9 19:33 Sites drwxr-xr-x 27 slehar staff 918 Mar 20 16:03 bin lrwxr-xr-x 1 slehar staff 13 Feb 23 20:19 bp -> .bash_profile drwxr-xr-x 35 slehar staff 1190 Jan 10 21:28 bubw2 drwxr-xr-x 3 slehar staff 102 Feb 12 08:06 cprogs drwxr-xr-x 9 slehar staff 306 Feb 21 13:37 sandbox -rwxr-xr-x 1 slehar staff 72 Feb 18 08:29 tst
This gives you a lot of detailed information about the files.
File Types and Permissions
The first letter on the left indicates whether its a regular file “-” or a directory “d”. (or l for a symbolic link). The next group of letters record the read/write/execute permissions on each file or directory for user/group/other with the code rwx (read, write, execute) for the user, rwx for the group, and rwx for other, in a single string. For example the string rwxrwxrwx means that everyone has permission for everything, whereas with these letters replaced with a minus, e.g. rw-rw-rw- means user group and other all have read/write permission but none can execute, whereas rwxrwx— means user and group have read/write/execute permission, but other has no permissions for anything.
The next colums give the file size in k bytes, the owner, the group, the date and time of modification, and finally the file or directory name, and for symbolic links, the -> file to which they are linked. Thats about all the information the unix operating system stores about the file, besides the data it contains.
The command-line arguments to the ls command exemplify a common feature of many unix commands, a way to modify the command with extra flags like -F or -a to modify the function of the command. The flags can be provided in any order, ls -l -a does the same thing as ls -a -l, and they can even be combined as in ls -al in any order, ls -la to provide some specific desired behavior, ls -laF.
Another fundamental flexibility inherent in the design of unix was the possibility to change the behavior of the commands to suite your personal preferences. For example if you prefer the format of “ls -F” (which is the format I prefer) then you can redefine the ls command for yourself by defining an alias, like this
$ alias ls=‘ls -F’
This says any time I type “ls” behave as if I had typed “ls -F”. If you want to preserve the original behavior of ls (which is usually a good idea) it is better practice to define your own new command, for example call it “lf” to mean “ls -F” as distinct from “ls”.
Type this command into your terminal:
$ alias lf=‘ls -F’
Note no spaces either side of the=equals=sign. Now type
and see what happens. Do you get the same thing you get when you type “ls -F”?
An alias redefines a command. You can see what aliases you have defined by just typing alias without an argument
$ alias alias lf=‘ls -F’
Now lets try out the history feature. Hit up-arrow to recall the last command. If you hit Enter it will repeat the last command as if you had typed it in again. If you hit up-arrow more than once, you go back to earlier commands and execute them with Return. And you can edit those previous commands to make new ones.
For example hit up-arrow until you recall this command
$ alias lf=‘ls -F’
then hit left arrow and type “a” to edit the command to
$ alias lf=‘ls -aF’
$ alias lf=‘ls -laF’
You can also type “history” and get a history listing of all your previous commands in this session, in a numbered list, and you can recall any command from the list by typing ! and the list number.
This makes it easy to do quick trial-and-error experiments to find out what works and what doesn’t.
Now before we start exploring our directory structure, let us set up a fancy lf alias that will show the path to the directory in a ====[ header line ]==== over the directory listing. Copy and paste this command (without the $) into your terminal window and hit Return.
$ alias lf='echo =====[ `pwd` ]=====; ls -F' alias lf='echo =====[ `pwd` ]=====; ls -F'
Try it out. Does it work like this?
$ lf =====[ /Users/slehar ]===== Applications/ Documents/ Library/ Music/ Public/ Desktop/ Downloads/ Movies/ Pictures/ Sites/
Type the command “pwd” which means “print working directory” and you should get something like this:
$ pwd /Users/slehar
Now type this:
$ echo pwd pwd
The echo command simply echos back what is given to it.
$ echo `pwd` /Users/slehar
The `back ticks` (or `back quotes`) have a special meaning, they execute the command between the `back ticks` as if it had been typed to the terminal, and returns the text returned by the command. In this case pwd, when typed in as a command, prints the working directory, “/Users/slehar” which echo then echos.
Now type the command
$ lf -a =====[ /Users/slehar ]===== ./ .dvdcss/ Documents/ ../ .fontconfig/ Downloads/ .CFUserTextEncoding .jnlp-applet/ Library/ .DS_Store .serverauth.218 Movies/ .Trash/ .serverauth.363 Music/ .Xauthority .ssh/ Pictures/ .bash_history .viminfo Public/ .cups/ Applications/ Sites/ .cvspass Desktop/
and we see a lot of “invisible” files and directories whose names start with a .dot they only are visible with the -a (all) flag to ls. Among them are two special directories that appear as
The trailing/ slash/ indicates that they are directories, their real names are just . and .. respectively. “.” means this directory, the present working directory, the one we are in. And the special name “..” means the directory above us, our “parent” directory, in this case it is /User, under which we exist as /User/AGuest.
$ lf .. =====[ /Users/slehar]===== AGuest/ Shared/ andy/ gregmillington/ slehar/
The ==[ path in the header line ]== is wrong, this is a listing of its parent directory, /Users, as you can see by typing these commands
$ cd .. $ lf =====[ /Users ]===== AGuest/ Shared/ andy/ gregmillington/ slehar/
which shows the correct path in the header line. We can go one higher with the command
$ cd .. $ lf =====[ / ]===== Applications/ dev/ Developer/ etc@ Library/ home/ Network/ mach_kernel Nik Software/ net/ System/ private/ User Guides And Information@ sbin/ Users/ tmp@ Volumes/ usr/ bin/ var@ cores/
This is the root directory, “/”, the root of the whole tree. Don’t mess with these folders, this is system stuff that you should not monkey with. Lets go back to your home directory.
Type the following commands:
$ echo $HOME /Users/slehar
HOME is an “environment variable” whose value is retrieved by prepending a $dollar. You can use echo to check what the value of $HOME is, or you can use it directly in a command to go back to your home directory.
$ cd $HOME $ lf =====[ /Users/slehar ]===== Applications/ Documents/ Library/ Music/ Public/ Desktop/ Downloads/ Movies/ Pictures/ Sites/
And if you’re not back home where you started, you’ve done something wrong. If you get real messed up, just open another terminal window Shell > New Window > Terminal > Basic and start from scratch.
Well thats enough unix for the first lesson, perhaps I might be motivated to add another later.