Simple Introduction To Apple’s Unix Using Terminal on OS X

This post is for someone interested in learning UNIX on Mac OS X Snow Leopard, but lacks experience using a command line, or has only C:\ prompt knowledge. I will show you, simply, how to map a DOS command to a UNIX command, and to save your profile so the alias name you create is persistent.  If you are a shell scripting genius, this post is something you are encouraged to comment on and expand upon, as learning scripts and commands for OS X can be input many different ways.  It never hurts to have different suggestions.  I will try to make this at the minimum a weekly edition to my blog, and expand each article further than the next.

Let’s get started.  First, open Terminal from your /username/Applications/Utilities folder. This is the command line utility for UNIX, by default it is Bash (Bourne Again Shell.)

You will see your entire present working directory prior to the $ prompt.  The tilde (~) represents /Users/your home folder.  The standard format is computer name: pwd. You can type the simple command pwd and the shell will respond with the folder you are currently accessing.

Default Shell

Default Shell

I like a cleaner shell screen, so I type bash, and hit enter.  This opens a new bash shell, on top of the existing shell.  Next, the command clear deletes the current Terminal text and starts with a fresh bash prompt.  You end up with a Terminal screen that looks like this:

Clean Bash Prompt

Clean Bash Prompt

Here, we can start by showing the man (short for manual;  built in UNIX help pages) page for the command ls (list), which is basically the dir command in DOS.  Simply type man ls at the command line and hit enter.  You can move down the pages line by line using the down arrow key on your keyboard.  To exit the manual, simply type q.

The man page shows you the various options you can use along with the ls command, for example, ls -lia. This command is particularly useful when searching a directory for hidden files (they start with a period), as Finder refuses to display them by default:

Directory Listing With Hidden Files

Directory Listing With Hidden Files

Now, if you are used to DOS, here is a helpful way to stick with the commands you know, yet tell Terminal to run UNIX commands.  You create an alias, basically a command name you make up mapped to a system command. If you want to use options such as -lia, you need to enclose the command in double quotes.  The syntax of the alias command in bash is alias newCmdName=systemCmdName. The command in DOS for a directory listing like the one above is dir /a.  So we can map dirA to ls -lia like so:  alias dirA=”ls -lia”

Note that UNIX is also case-sensitive, for files as well as commands:

Alias DOS dir to UNIX ls

Alias DOS dir to UNIX ls

You can simplify entering commands by using the up arrow to cycle through previously typed in commands.  The history command, saves the commands you enter by line number, which you can view by typing history.  To execute the line number, type an exclamation point followed by the line number:

History Command

History Command

Now, to save this new alias dirA you created into your bash profile.  This way it doesn’t get erased when we kill the shell and quit Terminal.  You may not have a profile set up for bash in your home directory, so what you need to do is create one.  Bash will look for .bash_profile first, so it is best to name any profile you want to use on a regular basis with this name.  It is a hidden file, so you need to have the period in the file name.  For simplicity, you can create a simple text file in your favorite text editor, name it bash_profile (without an extension)and save it in your home directory.  Write the exact command on one line like so: alias dirA=”ls-lia”

Save the file, and then change the name in Terminal.  Use the command mv to change the name so it begins with a period.  Simply type mv bash_profile .bash_profile and press enter:

MV Command

MV Command

MV Complete

MV Complete

That did two things:  one, you moved the file;  two, you learned that mv is the same command as the DOS command move.  You can follow the procedure above to make an alias for the mv command, and place it on the next line in your .bash_profile, so it remains permanent for your login shell.  If you like running an “non-login” shell as I do above, you can use the command cp to copy your .bash_profile to .bashrc like so:   cp .bash_profile .bashrc

Here is a short list of a few DOS commands, along with their respective UNIX bash counterparts (pdf of list here):

DOS COMMAND

UNIX COMMAND

DIR

LS

MOVE

MV

COPY

CP

FIND

GREP, FGREP, EGREP

MEM

TOP (virtual memory use VM_STAT)

*use ctrl-c to end TOP command*

CD

CD (PWD to display current directory)

DEL

RM

That’s all for this quick lesson.  Stop by next week for more DOS examples in UNIX, and (hopefully) working with vi, UNIX’s text editor.

1 comment so far ↓

#1 Bash Profile Setup On OS X | Lowfokus on 11.07.09 at 18:02

[...] in your ~/Applications/Utilities folder. If you don’t know what that means please start with this post, as it will show you a very basic beginning to the Terminal in OS X.  Be sure you are in your home [...]