UNIX
Other, better sites
It turns out that a lot of other people have put together this sort of page and they are much better than this page, so here are some links I found at the OrborosX web site that you may find useful.
UNIX for Mac Users
This page was initially going to be a pretty short collection of UNIX commands and some simple explanations, but it keeps growing longer. I apologize if it seems like just a lot of boring text, but hey - we are talking about UNIX here. When I talk about UNIX in MacOS X, I’m for the most part referring to using the Terminal to run command line programs. UNIX also manifests itself in the permission settings that are attached to every folder and directory. There are also a lot of text based configuration files, which is not Mac like at all. Mac users expect graphical interfaces for adjusting settings, not editing a text file.
In this page, I hope to help Mac users who want to run IRAF but are newcomers to UNIX some of the basics of the command line and of how unix works. This is just one page, so it’s not comprehensive. There are many books available on UNIX and I suggest you purchase one if you’d like to learn about the UNIX side of MacOS X more completely.
Tools
So, OS X may be UNIX but it’s still a Macintosh too. So, first off I think you should have a decent text editor. vi is not a Macintosh text editor. UNIX gurus may like it, but no one can argue it’s Mac-like. For the newcomer, it’s clear that you should grab a copy of TextWrangler
You also need to use the Terminal. Terminal.app is located in the /Applications/Utilities folder. You should keep this in your dock, if you use the dock. It’s your window into the command line side of MacOS X.
Another very useful program would be ManOpen. This is a graphical program that allows you to read the UNIX man pages in an real OS X program. It’s much nicer than having to type in the Terminal. If you ever grow curious about certain UNIX programs, you can always read its man page.
You can also checkout The IRAF Button, my Applescript studio GUI launcher which can launch IRAF, xgterm, ximtool, ds9, x11, etc.
Starting a Terminal Session
So, when you first run Terminal.app you encounter the shell prompt. The unix shell is a program that’s running which inteprets whatever commands you type into actions, be it running programs or moving files, etc. MacOS X uses a default shell with is called tcsh. It’s pretty good, but UNIX gurus have their own preferences for other non-”C” shells, like the bash, the bourne shell, zsh, and on and on. If you’re a newcomer to UNIX, tcsh will serve you fine.
Experiment a little in the Terminal. If you’ve never used DOS or UNIX before, then you probably have little or no expecations of what the Terminal does. Try typing these commands at the prompt, followed by enter.
% ls -al
(This lists every file in the current directory, and shows other info on them. Try it with and without the -al, also try it just with -a or just with -l. See the difference?) Also, note that .. means the directory that the current directory is inside. One period, that is to say . - that’s just the director you’re working in. The so-called “present working directory” is shown by
% pwd
This show the present working directory. When you open a new Terminal window, this is will be your home directory.
Each directory you are working with in the Terminal as a corresponding folder in the Finder. The terms folder and directory are really interchangeable. Do this-
& cd Desktop
% ls -al
The cd command changes your present working directory to your Desktop folder. Now look at your actual desktop. Every file and folder listed in the Terminal should be also on the desktop of your Mac. (The exception being your hard drives, which are shown there by the finder but are not actually in your desktop folder.) Quite the contrary, your Desktop folder is located in /Users/you/Desktop on your boot drive.
You can also use wildcards in many unix commands. Say you want to list all the files in a directory that start with an s. You can do this:
% ls s*
Find everything that ends in txt.
% ls *txt
Use of the asterisk in a wild card works in virtually every unix command that accepts an argument.
Running UNIX programs and the PATH variable
The key concept here is your path. Unix programs are run by typing their name at the unix prompt. Type “pico” on the command line. Out pops a crude text editor. Hit control-x to quit.
User installed programs like IRAF and X11IRAF are not always put in the same places as programs installed by the OS itself. Type where pico in a terminal. It will show you were the program pico is located. It’s in /usr/bin. Now, if you go to the root of your hard drive in the finder and look for a usrfolder you won’t find one. That’s because the Finder is set to ignore it; because the files inside are all unix-stuff that you don’t want to move around or double click on in the Finder.
Here lies a key point: UNIX programs when seen in the Finder do not look like applications. If you download my updated Jaguar and later xgterm binary of mine and try to double click on it, nothing will happen. Or worse, it’ll probably try to open it in a text editor, which is not what you want.
In order to run unix programs the shell must know where to look. Type “echo $PATH” in the terminal. The echo command simply displays the text of whatever you type after it. In this case, it’s showing you what the enviromental variable PATH is set to. This is where the shell will look for programs. If they are not in any of those directories, they aren’t going to run. The different directories are separated by a colon.
Now, one way to run a progarm that’s in your present working directory is to do this.
% ./myprogram
Putting a period and a slash before the command tells it than it’s in the current directory. The period is an abbreviation for the entire path to wherever the present directory is. Typing ./myprogram on your desktop is like typing /Users/you/Desktop/myprogram, which would also work.
Anyway, the simpler way is to install programs in special directories, like /usr/local/bin, which is where the IRAF installers I have made install ds9, xgterm, ximtool, etc. But you need to make sure this directory is in your path.
Going back to when you echoed your path, if /usr/local/bin and /usr/X11R6/bin/ aren’t in your path, then there is trouble since programs are installed in these locations and you will want to be able to run them.
You can add these items to your path by editing or creating a .cshrc file in your home directory. You can edit this file if it exists with BBEdit, using its open Hidden feature. Since the file starts with a period, it is invisible in the Finder, or with a regular ls command. Insert towards the end a line that looks like this.
setenv PATH "${PATH}:/usr/X11R6/bin/:/usr/local/bin/"
That adds those two directories to the current path. You may also want to add a line like this
setenv MANPATH "/usr/man:/usr/local/man/:/usr/X11R6/man:/usr/share/man"
so you can find the man pages for the programs. You notice the different syntax, that completely resets the MANPATH variable, ignoring whatever it was in the first place. IE may not be displaying that first line correctly, it should look like ${PATH}
In Bash you want to edit or create a .bashrc file, and then add a line like this
export PATH=$PATH:/usr/sbin/:/usr/local/bin
similar syntax to change the MANPATH to what was shown above for csh.
What is the MANPATH? Try typing man pico in the Terminal. (Or if you have installed ManOpen and its command line helper program, you can type openman pico. You’ll see the manual page for pico, which can contain a lot of useful information. Well, not for pico so much but for other utilities, programs, etc. However, much like the shell looking for programs, the man program has to know where to look for the manual pages.
The .cshrc file only is read every time you start a new terminal. If you want it read into your active session, type
% source .cshrc
This will work if you’re in your home directory. Your home directory has an abbreviation, from any directory you can always type say…
% cat ~/.cshrc
The cat command just displays the contents of the file to your Terminal screen, but the ~ character refers to your home directory. In fact if you type echo ~you’ll see that it’s /Users/you
UNIX Permissions and Owners
UNIX was conceived as a multi-user operating system, which is to say that many people would use the same machine, drives, etc. So, every file and folder in UNIX is assigned an owner, a group and also three sets of permissions. You can see these settings when you type the ls -l command.
drwxr-xr-x 562 root wheel 19108 Dec 6 16:30 bin</pre>
Here we have an example of the permission, owner, and group listings for /usr/bin. The owner is root, the group is wheel, and those letters tell you something about the permissions. The first letter d just says that it’s a directory. The next three are permissions for the owner, which is root. The rwx means that the owner can read write and execute the directory. Executing a directory I think means you are allowed to make it your working directory. The next three letters are for the group. r-x means members of the group have read and execute permission, but not write. The next sent of three letters, which is also r-x means that “others” have read and exceute permission, but they can’t write either.
You can alter the permissions of a file with the chmod command. Let’s look at an example file.
-r--r--r-- 562 marcosh admin 19108 Dec 6 16:30 file
This unfortunate file can only be read by the owner, user, and the group. Say we want to let everyone be able to write to it. We can issue this command
% chmod oug+w file
Do you see how that syntax works? oug+w means “For the owner, user, group add write permission. Then you put the file name afterwards. Now if you list the file again you’ll see the change. Every user can write to the file now.
-rw-rw-rw- 562 marcosh admin 19108 Dec 6 16:30 file
Similarly you can change the owner of a file with the chown command.
% chown iraf file
However if you try that it will be rejected, because you don’t have permission to change the ownership. Do this you must invoke a very powerful and important command. sudo Sudo lets you issue a command as the “superuser” or root. The superuser can do anything it wants to any file. So when you type
% sudo chown iraf file
you can expect something to happen. It will actually ask you for your password, and this will only work if you are logged in as an administrator. The first account OS X made when you set up your machine is an admin. After you sucessfully issue the sudo chown command the file then has a new owner.
-rw-rw-rw- 562 iraf admin 19108 Dec 6 16:30 file
Moving and copying files at the command line
This is probably what you are going to need the command line for most, since you may have to install things manually now and then. The commands you’ll need are mv and cp. These move and copy files respectively. mv can also be used to rename a file. Here’s how it works
% sudo mv xgterm /usr/local/bin
Now presuming that you are working in a directory where xgterm is, this command will move it into /usr/local/bin. You also note that we have to use sudo in this case. That is because /usr/local/bin is owned by root, and normal users do not have permission to move files into it.
The cp command works in a similar fashion.
% cp file1 ~/Desktop
This will copy file1 from your working directory to your desktop. You can also copy files from other directories to your current directory.
% cp /etc/hosts .
This copies the hosts file to your current directory. It’s a text file, you can open it in BBEdit. There are a host of strange text configuration files in /etc.
tcsh problems in Panther and Tiger
Starting with Panther, MacOS X uses the Bash shell by default. You can switch back to tcsh but the default configuration for tcsh is lacking. Fortunately, the needed files are in /usr/share/tcsh/examples/ so I wrote a shell script and a fixed csh aliases file. Put these in the same directory and then run the script with
% sudo csh fix_tcsh_panther.csh
Also, be sure to add /usr/local/bin and /usr/X11r6/bin to your PATH in whichever shell you choose, per the PATH instructions above.