PDA

View Full Version : Getting Started with the OSX Terminal and Bash



keldon
08-27-2008, 11:35 PM
For the sake of keeping information about various commands and system features separate, unless otherwise noted I am solely referring to the flavor of Unix that is contained within Apple’s Mac OSX. That being said many if not all of the BASH commands and file structures are platform and architecture independent, the only major differences will be the location of files within the directory tree and the order in which they are used to determine the settings and behavior of the local environment.

A shell is simply a program that is designed to take commands from the user and then translate them into instructions that the computer can understand. This can be as simple as asking the computer to list the files in the current directory or as complex as compiling the source code for a new program; it isn't hard to understand and can be a very powerful tool. Average users may not find it of much value, but if you do a lot of programming, or are curious about OSX from a systems point of view then you will most likely become very familiar with it. I will be sticking with the default shell, known as the BASH shell. There are many places to turn if you want more information on the history of other Unix shells, such as here (http://www.softpanorama.org/People/Shell_giants/introduction.shtml), so I won’t go into that.

To enter the shell you start a program called Terminal which can be found in Applications -> Utilities. If you are already familiar with a Linux terminal or konsole then you should be able to start using this one with little problems. The same basic commands apply but there are differences to the directory structure between OSX, Linux, and BSD, more on that later. If you have never used a terminal program before, don’t worry because they are quite simple. The commands may seem obscure but if you take things slowly and use the resources provided here and within OSX you will have no problem accomplishing any task you need to.

http://bitbenderforums.com/users/keldon/article_images/pic1.png

When Terminal first opens up you are presented with what is called the command prompt. The top line displays the last time that you logged into the terminal. It does this for many reasons, most of which are no longer necessary and don’t concern us here. Under that line you should see a line with your computer’s name, some symbols, and a blinking cursor. This is where our story begins.

Now would be a good time to explain a few things about the OSX directory structure. Like Windows, and other multiuser operating systems before it, OSX creates a home directory for every user. It is here that it stores your music, movies, homework and other files that are needed or created by you. Unlike most other versions of Unix or Linux, no root account or directory is created initially, you can do this later manually if you choose. The person who set the computer up is the system administrator and they have root or super user privileges. That being the case, care must be taken when working in a shell so as not to damage the system accidentally. Also until a root account is set up the su (http://www.linfo.org/su.html) command will not work, you must use sudo (http://linux.about.com/od/commands/l/blcmdl8_sudo.htm) instead. At the command prompt this location is displayed with a ~. This means you are currently in your home directory.

Lets start off with a few simple commands to get you moving around. The first command can be a real help if you are constantly changing directories and moving files from one location to another, pwd, it stands for print working directory. If you type it now you should should see the following.

http://bitbenderforums.com/users/keldon/article_images/pic2.png

This is where you are currently in the file structure. The first / represents the base of the directory tree. The Users section is where all the home directories for the various users are kept and keldon in this case is the current user. You can only go up or down from here so let’s give it a try.

Where can we go though? It always helps to know where you are going before you take off so lets try out another command, ls. This stands for list, and it does just that, it lists all the files and directories in the current directory. It should look something like this.

Here we see the name of my computer and a tilde telling me that I am in my home directory. Don’t worry if your prompt looks different from mine, we will get into changing that later on when we talk about environments.

http://bitbenderforums.com/users/keldon/article_images/pic4.png

Now use the cd command to change to a different directory, for example, Documents. To do this we simply type cd Documents. Be careful of the case of the words. A default installation of OSX is not case-sensitive but all versions of Unix and Linux are. Its a good idea to get into the habit of treating everything like it was, that way if you go to another system you can avoid mistakes. Now if you type pwd you will see that your working directory has changed to /Users/<yourname>/Documents. To return back to the directory you were at previously you type cd ... This brings you back up to the parent directory. If you ever get lost and and want to return to your home directory typing cd by itself will bring you there. You are now free to move about the system at will.

This is all well and good but how do you actually do anything with these files and directories? When you want to create a directory you use mkdir <directoryname> and then to remove that directory you would use rmdir <directoryname>, that’s all there is to it. There is a catch though, the directory has to be empty, this command will not allow you to delete a directory that has any files or directories in it. In order to delete an entire directory structure you would use the command rm -r <directoryname>. A word of caution though, this will delete all contents recursively starting at the highest levels, so make sure you know where you are in the directory tree. Files are deleted using the same command, just type rm <filename>.

If you are ever in doubt about what a command does or have any questions about it you can look at its manual page. In order to do this simply type man <command>. For example, if you want to see what switches or modifiers are available for the ls command then you would type man ls at the prompt. To return back to the prompt you simply hit q for quit. The last command I want to show you before we get into more advanced topics is clear. This simply clears away the screen and leaves you with a fresh command prompt. This can be helpful when trying to scan the output of a command and you are getting confused and lost with information from previous commands.