BluaP Mac Support
Mac Nieuws BluaP Mac Support Weblog Software Mac Games Nieuws & Downloads WiFi / Airport / 802.11
Te Koop Aangeboden (en gezocht) Mac Knowledge Mac Tips Unix Mac Links

Introduction to Darwin, part 2

Return to Command Line Interface
So you've never used a Command Line Interface (CLI) before. As we've stated before and you've most likely noticed by now, the mouse doesn't work. Everything you do, you have to do by entering commands at the prompt.

What are commands?
A command is something you type in at the prompt (that's where the cursor is) and execute by hitting the RETURN key. Written in a book, they look something like this:

% tar -xvf bash.pkg.tar

"%" stand for the command prompt. It probably looks something like "[localhost:~] <username>%" but they usually just shorten it to % to let you know what shell you're in. The Bash shell looks something like "bash-2.03$" so a command listed in a book that was written for the bash shell will look like:

$ tar -xvf bash.pkg.tar

Thus depending on the symbol in the printed command, you can tell what shell that command is supposed to be entered in. You will not need to type this symbol when typing in commands.

"tar" in this case is the command we are executing.

"-xvf" are switches that tell you how to execute the command.

After these come carious other information such as paths and file names that the command needs to know in order to complete what you are doing. In our example "bash.pkg.tar" is the name of the file we are decompressing. Commands use different formats. What format and what information you need to use can be found in the man file for that command.

Return to man pages
If you've already looked at a man page and you're not familiar with CLIs, they may seem a little strange to you. If you type...

% man tar

... you'll get the man page for the tar command. NAME and DESCRIPTION should be pretty well straight forward. For SYNOPSIS you're going to see:

"tar [-]{crtux}[befhmopvwzHLPXZ014578] [archive] [blocksize] [-C directory ] [-s replstr ] file1 [file2...]"

... which could be a little confusing. This is the format that the command needs to be entered in. Items that are in plain words like "tar" and "file 1" are items that have to be typed in. Of course, if you want to enter the tar command, you're going to have to type "tar" and you are going to have to tell it which file you want to tar, that would be "file 1". Items in between brackets [ ] and {} are things that you may or may not use depending on what exactly you want to do with them. Most of these, such as the string of random characters {crtux}, are flags. It also shows the order that the commands, flags and information need to be typed in. So if your commands require an archive, you're going to have to type it in before the file name.

What are flags?
Flags are modifiers to the command. They come after the command itself and usually have some symbol denoting their existence. In this case, it is the "-". So if you wanted to uncompress a file rather than compress a file with tar you would have to use the switch "-x". The command would look like this:

% tar -x filename

To decompress the bash.pkg.tar in the project in this tutorial correctly, we used the x, v, and f flags. Thus the command could be typed either of the following two ways:

% tar -x -v -f filename


% tar -xvf filename

Some other commands.

% builtins
This command gives you a list of all the commands built into the tsch shell. You can read the tcsh man pages about them.

% find <directory> -name <filename> -print
This will give the path to the file whos name you type in that is found in the directory you provide. Find has a man page and is capable of performing many functions such as searching by owner, date, or by using wildcards.

% mv <file> <target path>
This is the command that moves a file from one place to another place in the directory structure. This works the same as if you had dragged and dropped a file from one folder to another. mv has its own man page.

% rm <file>
This is the command that erases the file all together just like you had put it in the trash and emptied the trash in the GUI. rm has its own man page.

% mkdir <directory>
This commands creates a directory, just as if you had used the New Folder command in the GUI. It has its own man page.

% copy <file> <new file>
This creates a new file exactly like the original without moving or altering it.

What is the directory?
I think that navigating the directory structure is going to be the hardest thing for Mac users to get used to. They're used to having separate windows open, working with files and folders, and being able to drag and drop them. They can still have multiple windows open, but the files and folders are going to act different and dragging and dropping is why GUIs were created. The way Mac users are going to have to think about it is that each terminal window, actually the command prompt, is a folder. You don't get to automatically see files in them, you can only work in one folder at a time, and all your actions are going to have to be done through various hermetic commands.

So, on your Mac, you've got different volumes filled with folder and files. Folders, of course, can be nested. The same is true in the CLI, but you have to get used to looking at it a different way. Directories and folders can be considered the same thing. When in CLI they are known as directory, and when in a GUI they are known as a folder.

When you open your terminal window, you'll notice that the prompt has the [localhost:~] right before the user name of the person who logged in. "localhost" is the computer you are currently connected to. "~" is the path to your current directory, which begins as your default directory. Your default directory is a directory with the same name as the logged in user in the Users folder. Simply put, you are now looking at a folder with the same name as your login.

If you type "ls" you will see all the other directories and files in that directory. Do this on your computer and you should see three other directories, Documents, Library, and Public.

If you want to go up a level in the directory structure, type "cd.." and you will pop up one level. This doesn't have a good equivilant in Mac OS 9 unless you're looking for a file and click on the hard drive image in the search window. In Mac OS X, there is a really good equivilant in the back arrow next to the drop down menu that tells you which folder you're in. Thus if you type "cd.." it would be equivilant to closing the folder you were looking in and opening up the folder that that folder was in. This should put you in the Users folder. Your prompt should look like [localhost:/Users]. Whenever an account is made in OS X a directory should be made in the Users folder with their name as the title. if you type "cd.." again your prompt will be say [localhost:/]. This is the root directory. This is similar to opening up a window to your hard drive in Mac OS X.

By now you should be noticing that all the paths in Darwin use slashes, / , to denote directories, starting with one as the root. This is very similar to the URLs you type into a web browser. Windows machines use backslashes, \ , and Mac OS 9 and earlier used colons, : , to show this. So the path "/Users/mac/library/stuff/teach.txt" would be a text file in a folder named "stuff" in a folder named library in a folder named mac in a folder named "Users" on your main harddrive. In Mac OS 9 this would have looked like "Macintosh HD:Users:library:stuff" if you had Got Info on the file, teach.txt, and looked at the "Where:" statement.

If you want to go down one level into a directory in your current directory, type "cd <directory name> and you will go into the lower directory. This is the same as if you were in a GUI and double clicked on a folder. While in your default directory, type "cd library" and you'll notice your prompt will display "[localhost:~/library]". The path of the directory you are currently looking at on the computer "localhost" (meaning 'this computer') is "/Users/<login>/library".

Typically, you're going to be doing a good deal of this in Darwin. Most commands work on files located in the directory you are currently located in. If you want them to work on files elsewhere, you'll have to use the proper switches and write in the required paths. No browse windows here, you're going to have to know where everything is to work with it before you issue the commands.

More projects

How to make aliases.
You use an alias to create commands you want but don't have. Some Unix people hate having commands that are more than one or two characters long, or get tired of typing in long strings of flags that are used often. Therefore, you can use the alias command to create a new command for a sting of commands. Let's go back to our old friend tar. most users are never going to "tar" things, they're going to be "tar -xvf" things. So if we get tired of typing all that in or have trouble remembering all the flags, we can create a new, easy to remember command to use in its place, like "untar". Type the following:

% alias untar tar -xvf

Now instead of having to type "tar -xvf" you can just type "untar".

If you want to undo this operation, you can type:

% unalias untar

Now the command has been removed and if you try typing it, you'll get "untar: Command not found."

The alias command can be found in the tcsh man pages on page 19. The format is "alias <new command> <old command>" and for unalias is "unalias <alias to be deleted>".

Learn your Text Editors

% vi
% emacs
% pico

More *nix text editors. We looked at PICO last tutorial. These other two are also provided with Darwin and you will run across references to them in *nix literature. Read the man pages (especially for VI, PICO has no man page) and play around in them so next time you talk to a *nix geek you can argue the virtues of your favorite text editor.