Learning the Terminal on the Mac – Part 2 – The Shell and Paths

In the first article we learned how to launch Terminal and get started using the shell. Also we covered some basic commands for moving around, including cd, pwd and ls.

In this action-packed episode, we are going to take a closer look at directories and paths. Usually. I would try to cover material in a bit more breadth before going into depth on a particular topic. However, an understanding of paths is important foundational knowledge, and will be very useful for our next topic, basic file manipulation.

Before diving into paths and directories, I want to share a little more background information about the shell, and also some tips on how to get yourself out of trouble.

About the Shell

This “shell” thing we have been talking about is actually a kind of program. There are several different shells available on the Mac, but the most popular and the default is called bash. Bash stands for “Bourne-again Shell”, was created over 20 years ago and it is still being actively developed[1].

Due to it’s long history in the UNIX world, bash sometimes isn’t the most user-friendly piece of software. Sometimes you can make a typo that puts your shell into a state you don’t understand. Fortunately, all hope is not lost.

Getting out of Trouble

You’re probably familiar with some of the common Mac OS X keyboard shortcuts, such as ⌘S (Save), ⌘P (Print), and ⌘Q (Quit). Most keyboard shortcuts in the graphical part of Mac OS X use the ⌘ (Command key) modifier. These shortcuts are also used in Terminal.app for quitting, closing windows, etc.

However, remember that Terminal.app loads a shell, bash, inside of it. Bash uses a completely different set of keyboard shortcuts, which use the Control key. This key is usually labeled “ctrl” on your Mac keyboard, and uses ^ as its symbolic representation.

If you know only one control sequence in the shell, than it should be ^C. Without going into too much detail, this keystroke is similar to “cancel” or “escape”. If you end up in some mode or program that you don’t understand, you can often get back to the prompt with just ^C.

Perhaps it is best explained with a haiku:

Lost or stuck in bash?
Prompt gone, output out of hand?
^C saves you

Then again, maybe not. Time to move on to our Feature Presentation: Directories and Paths.

Directories

As a Mac user you should already be familiar with using folders in the Finder. Well, a directory is just the more shell-oriented term for a folder, and since we’re working in the shell, we’ll talk in terms of directories most often.

Special Directories

There are a few “special” directories that we should touch on before going further.

The top-level directory in your Mac’s hard drive is referred to as / or the root directory in shell terms. All other directories are descendants of the root directory. It’s easy to navigate to / and see what is there, using cd and ls.

lt2-ls-root.png

The next special directories we need to mention are . (dot) and .. (dot dot). The directory . is just an alias for your current directory. If you type cd . at the prompt, essentially nothing will happen. The purpose of the . directory might not be immediately apparent, but will be because especially useful when we discuss file manipulation.

The .. directory is similar to ., except that it refers to the directory “above” your current directory, also known as the parent directory. Typing cd .. from your home directory will move you up to the /Users directory, and typing it again will put you in / (the root).

These directories will actually be shown in the output of ls if the -a option is specified:

lt2-dot-and-dotdot.png

Astute readers may be wondering about the effects of cd .. if your current directory is /. Well, fear not. You will not accidently change into some forbidden directory, whose contents, once seen, may not be unseen. When in root, cd .. essentially does nothing.

The final magic directory is ~. This is simply a shortcut to your home directory. Every user has one and only one home directory. On Mac OS X, all users’ home directories are in /Users directory. Also you may notice that your prompt shows ~ when you are in your home directory, rather than the full name of the directory (for basic prompt description, see the last article).

lt2-tilde.png

There is one important stipulation when using ~. It does not work inside either single (') or double (") quotes. This is due to the way ~ is implemented in the shell. All the other special directories mentioned in this section have no problems with quotes, however. Look to the escaping and examples sections for how to deal with ~ in paths.

To review:

  • /
    • The top-level directory of your computer’s hard disk.
    • Usually called root or slash.
  • .
    • The current directory.
    • Usually called dot.
  • ..
    • The directory that contains the current, or parent directory.
    • Usually called dot dot.
  • ~
    • Your home directory.
    • Usually called tilde.
    • Won’t work inside quotes!

Paths

We’ve already worked with paths a little bit in the last article with the cd command. Now we will talk about them in a little more detail.

The path separator on Mac OS X is / (called a slash or sometimes forward slash).

Kinds of Paths

There are two kinds of paths, relative and absolute. Relative paths reference files from your current directory, while absolute paths specify the complete location of a file or directory.

This is I how would change to the Downloads directory of my “demo” account using an absolute path.

cd /Users/demo/Downloads

And this is how I would change to the Downloads directory of my “demo” if I was already in my home directory (~).

cd Downloads

Both these commands get me to the same directory, we just got there by a different path.

lt2-ls-downloads-abs-1.png
lt2-ls-downloads-rel2.png

Above, we worked with paths to directories. Well, paths to files work the same way. I could open a file called “Notes.txt” on my Desktop using the absolute path:

open /Users/demo/Desktop/Notes.txt

or a relative path (shown from my home directory):

open Desktop/Notes.txt

Escaping

The shell gives special meaning to certain characters in order to implement all its features. If you are familiar with web programming, this is similar to how URLs are percent-ecoded.

The most notable of these characters is probably the space. The shell uses spaces to separate commands from options and arguments. However, this is a problem if the argument you want already has a space in it. This occurs in many files and folders on Mac OS X, like the “About Stacks.pdf” file we used in the last article.

Special characters are escaped using the \ or backslash character. This can be confusing since this character is used as the path separator on Microsoft Windows. It’s probably best if you can just wipe Windows from your mind entirely (zing!).

To escape a space in a file or directory, with prefix it with a \, like so:

open Documents/About\ Stacks.pdf

Alternatively, you can quote the entire argument, using double quotes ("):

open "Documents/About Stacks.pdf"

Finally, we don’t need to escape the entire path, just the part that actually needs the escaping:

open Documents/"About Stacks.pdf"

Actually, single quotes (') can be used instead, but they have a slightly different meaning. We’ll discuss the meanings of the different types of quotes soon.

Also, note that if you use tab completion the shell will be smart enough to do most of the escaping for you. If you’re hungry for more, you can read more about escaping in the Escaping topic in the Advanced Bash-Scripting Guide.

Path Examples

Here are a few examples of how to put all this knowledge together:

cd .. Go to the directory one level above
cd ../.. Go to the directory two levels above
cd ../../ Go to the directory two levels above (same as previous, extra /’s are ignored)
cd ~ Go to your home directory
cd ~/Documents Go to your Documents directory
cd ~/.. Go to the directory one level above your home directory (Users)
cd ~/Library/Application\ Support Go to your Application Support folder using escaping
cd ~/Library/"Application Support" Go to your Application Support folder using quotes (don’t quote the ~)
open ~/Documents Open your Documents folder in Finder
open . Open the current directory in Finder (see, the . is useful)

Wrap Up

We now have enough knowledge regarding paths to provide a solid foundation going forward. Next up, we’ll be talking about copying, moving, and deleting files. If you have questions, comments, or suggestions for future articles, please note the comments section below. I believe it will accommodate your needs.

Learning the Terminal on the Mac – Introduction and Moving Around

The is the first of hopefully several short articles describing the basics of using the terminal in Mac OS X. This is aimed at absolute beginners who have never used a command line interface before. The first few installments are likely to be very screenshot heavy, because the command line has a rather steep learning curve. Every little bit helps.

First, a bit of motivation. If you have no interest in learning to use the terminal, then I probably can’t convince you otherwise. However, if you are slightly curious, here are a few reasons why it might be worthwhile (and fun!) to start tinkering with it.

The terminal…

  • can be faster for some tasks.
  • exposes advanced Mac OS features.
  • opens up access to a wealth of open source software.
  • lets you transfer your skills to other Unix-like operating systems.
Now, just a little bit of terminology before we get started.

Terminal.app, often referred to as just the terminal, is a Mac OS application that wraps an interactive command line interface to your computer called the shell. This article and the ones that follow will really be focusing on the shell, and not Terminal.app itself. These same lessons should apply if you are using another terminal application, such as iTerm or the one built in to Coda. Much of the information can also be transferred to other Unix like operating systems, but we will be using some Mac specific commands as well.

Moving Around

If you haven’t already, open Terminal.app. it’s in the Utilities folder inside your Applications folder, or just type “Terminal” into SpotLight. Once it launches, you should see a window like the one below. I have annotated that section called the prompt. This is where we type commands for the shell to interpret. The information in the prompt itself isn’t relevant quite yet so don’t worry about it.
terminal-prompt.jpg
First, let’s determine where we are. Similar to Finder, your shell is always focused on a directory. This is called the current working directory, or just current directory or working directory. There is a command pwd that prints the working directory (hence its name: print working directory). Type pwd at the prompt and press <return>.
tt1_02-pwd.png
My account name is “demo”, so the shell started in my home directory, which is “/Users/demo”.

Now that we know where we are, let’s see what else is here. The command to list the contents of a directory is ls (think “list”). This is also one of the most commonly used commands. Type ls at the prompt and you should see a list of the contents of your home directory. My “demo” account looks like this:

tt1_03-ls.png
For the sake of completeness, this is how my home directory looks in Finder. All the same folders are there.
tt1_04-finder.png
Let’s move into our Documents folder and see what’s there. We will use another extremely common shell command cd to change the current directory.

The cd command takes the directory to change to as an argument. Arguments follow the command and are separated by spaces.

Type cd Documents and press <enter>. Make sure you capitalize Documents. The shell is case-sensitive.

tt1_05-cd.png

You may have noticed that the prompt changed. The default prompt looks fairly cryptic at first, but it’s actually very simple. The first part before the colon is the computer name or hostname. It may seem like this is superfluous information – of course you are on your computer. But as we’ll see in future articles, this will come in handy. The second portion is the name of the current working directory. Next, we have our current user name, which again will be more useful in the future. The final $ is standard in many shell prompts. It denotes that you are a standard user, as opposed to a root or super user, in which case you would see a # instead.

tt1_05a-prompt.jpg
You probably have a lot more than just one file in your Documents folder, so the contents will scroll off window. This is fine for now. We’ll talk about controlling pagination and output flow soon.

So now we can see all the names of the files, but nothing else. Let’s say we want to see more information akin to the list view in Finder. We’re particularly interested in the size of the files.

In addition to arguments, many commands also take options. They are usually prefixed by the - character, but it can vary. I happen to know that ls has an l option that prints out files in long format. Type ls -l.

tt1_07a-lsl.jpg
Now we see several pieces of metadata associated with our files. There is some permissions information, the file size in bytes, the file’s creation date, and finally the filename. File size in bytes isn’t very intuitive to most people. We tend to think about in kilobytes (K), megabytes (M) and gigabytes (G). Fortunately there is another option, h, that makes the output of ls a little more human-readable.
tt1_08-lslh.png
That’s better. So now let’s say we want to open a Document. We can do this from the shell with the open command. Type:

open "About Stacks.pdf"

The quotes are important (we’ll learn why later), and again don’t forget about the case sensitivity. If you don’t have “About Stacks.pdf”, you can pick another file. The file will open up in your default application for that kind of file.

tt1_09-open.png
Let’s go back to our home directory. A quick way to do this is just type cd with no arguments.
tt1_10-lshome.png
By now you now may be a bit curious about other options for the ls command. (If you’re not, just pretend).

The man command lets us view the manual or man page for almost any command. To display the manual for ls just type: man ls

tt1_11-manls1.png
Use the up and down arrow keys to scroll the page, or space to advance a whole page at a time. Browse around for a bit. When you’re done, type q to quit.

Colorized output sounds interesting. Let’s give that a try.

tt1_12-manls2.jpg
tt1_13-lsG.png
Hooray, pretty colors. By default, it will color directories in blue and executable files in green. This is of course is all configurable. See the man page for more information.

Completion

Up to now we’ve working with relatively simple paths and filenames. However, you may already feel that typing out the full names of files seems somewhat onerous. There is a shortcut known as tab completion that greatly improves path entry at the command line. It is actually a fairly simple technique, so don’t let the seemingly long description that follows scare you. Read over the demo scenario, watch the demo movie that shows the steps in action, and then try it out for yourself.

To illustrate how tab completion works, we’ll set up a simple scenario. We want to view all the custom Preference Panes that are installed for all users of the system. However, we’re not exactly sure where they are stored. We’ll use the ls command and tab completion to efficiently explore the file system.

At the prompt type the following, but don’t press <enter>.

ls /

Now press the <tab> key once. Nothing should happen. This is because it needs more information before it can complete the next part of the path for you. To see all the possibilities, press the <tab> key a second time.

You should see a list of all the files and directories in top-level or root directory of your hard disk. The Library folder seems like a likely place for the Preference Panes to live.

You’ve already typed ls /, and you shouldn’t have pressed the <enter> key yet. Type a L but still don’t press <enter>. You should have the following on the prompt:

$ ls /L

Now press the tab key once. Nothing should happen. This is because it needs more information before it can complete the next part of the path for you. To see all the possibilities, press the key a second time.

You should see a list of all the files and directories in top-level or root directory of your hard disk. The Library folder seems like a likely place for the Preference Panes to live.

You’ve already typed ls /, and you shouldn’t have pressed the key yet. Type a L but still don’t press <enter>. You should have the following on the prompt:

$ ls /L

Now press the <tab> key. If you haven’t any other directories in the root of your disk that begin with capital L, the shell should automatically fill in “Library” for you. Magic!

You prompt should now look like this:

$ ls /Library/

Press <tab> twice (still no <enter>) to show the contents of the “/Library” directory.

The “PreferencePanes” directory looks very promising. Let’s see what’s inside.

Type Pref (the first few letters of “PreferencePanes”) and press <tab> to let the shell complete the rest. Again, don’t press <enter> yet. The shell should complete your path so it looks like:

$ ls /Library/Preference

But notice that the path now says “Preference” and we want “PreferencePanes”. The shell could not complete the path fully because it had more than one match for a folder that begins with “Preference”. To see all the possible matches, press the <tab> key.

We know see that it has matched “Preferences” and “PreferencePanes”. We want “PreferencePanes”. In order for the shell to complete it for us, we need to give it the next letter of the path we want. So we type P and press <tab> again.

Now our prompt should look like:

$ ls /Library/PreferencePanes/

This is the path we want. Now, finally, press <enter> to view the contents. I have the Growl and Flip4Mac Preference Panes installed for all users. Yay.

Here is a demo movie of the same process.

That may have seemed like a lot of work, but tab completion is really a huge time saver. Experiment with it whenever you are typing a path. You’ll soon find that it becomes fairly natural and saves a ton of keystrokes.

One final note on tab completion. You may notice that it sometimes puts weird slashes in the filenames. Take our “About Stacks.pdf” file as an example.

tt1_14-space_escape.jpg
The shell is doing what is known as escaping. We’ll touch on this much more in the future, but basically it’s a way to tell the shell what you really mean. In this case, our file, “About Stacks.pdf” has a space in the name. Remember earlier we said that arguments are separated by spaces. In order to tell the shell to treat the name of the file as one, we need to escape the space. This is why we put the name of the file in “” (double quotes) earlier.

Wrap-up

Up to now we’ve gotten a little background about the terminal and the program that runs inside it, the shell. You should now be able to move around with some efficiency, list files, and open them up. There is still a lot more to learn, but this is a solid start. The next article will cover some more common shell commands. In the meantime, feel free to explore, and if you have any comments or suggestions for future articles, please leave a comment!

WWDC Comedown

It’s been a full two days since WWDC ended, which I think is enough time for any residual effects of the Reality Distortion Field to subside. I thought I’d share a little bit about my first WWDC experience.

In summary, it was well worth it for me, and I’d go again. I can see how seasoned vets may tire of it eventually, but at this stage in my Cocoa (Touch) development career I felt it was invaluable. It was slightly different than I expected, but in a good ways:

Misconception 1:

The only first-hand account of previous WWDCs that I heard made it sound like it was an all-night turbo-nerd code-a-thon (not that there’s anything wrong with that). Also, every sort of conference I’ve been to in the past has been sorely lacking in the wall-socket department. I didn’t want to be the chump with the dead battery. Actually, I really didn’t want to be the chump with the dead battery.

(Idea Bubble!)

My attack to the potential power problem was three-pronged (subconsciously inspired by the male AC connector, perhaps).

  • Carry a Power Squid. This way, if I found a socket, but all the plugs where taken, I could just attach my Squid, and then more power for everyone! Squid Boy will save us all!
  • Bring a spare battery. I ordered one especially for the trip, and had it delivered to my hotel.
  • The final contingency plan: a second laptop. That’s right I also lugged my I-can’t-believe-it-still-works 12″ PowerBook with me. Its unlikely that both my previous power access strategies would fail, but this also covered the catastrophic failure of my main laptop.
Misconception 2:

I bumped into a Cocoa programming glitterati and asked him what he thought of this WWDC. He said

There are big years, and little years. This is a little year. 
I didn’t agree. It was a little year for the Mac, but it was a huge year overall, thanks to, you guessed it, the iPhone. How often will an entirely new platform be launched? Not too often, I reckon. I guess it still wasn’t big in terms of unveiling new information since we’ve had the iPhone SDK for some months now, but this was the first time all these iPhone developers, new and old, got together.

In fact, I began to envision that in-progress iPhone apps would be everywhere. Developers would be skipping around the halls of Moscone, gleefully sprinkling beta versions and licenses. Although my own iPhone app experience was very limited (though non-zero), I didn’t want to be left out.

So around 11 PM the night before a 7AM flight, I decide that it would be a Good Idea to upgrade my iPhone to the latest development firmware. What could go wrong? Well apparently, enough to keep me up until almost 4 AM. Pro-tip: read instructions.

I was sleep deprived, but I was ready.

Reality

First, the power. There were power strips everywhere. There was one on every table in the lounge. They routed power into the main seating areas, so you could charge up during a session and not pay attention to the speakers. One day, I even found a power outlet in my soup. Just kidding.

Basically, you had to try to run out of juice. And if you did, they had a special battery charging station. The extra battery got zero use, though I did throw down Ole Squiddy once and made a friend in doing so.

Next, the ‘iPhone Apps For All!’ scenario didn’t pan out. Sure I saw a few demos here and there, and spent zero time in the iPhone lab, but in general, it didn’t seem like people were spending much time hocking their wares.

Nor did they seem to be huddled around furiously coding. In fact, most people, myself included, seemed to try to meet people, absorb information, get a few questions answered, and have fun doing it all. I thought it was great. For me, the best part was socializing with the community. I’m pretty new to the Cocoa scene, so it was really nice to meet all the people I’ve been twitter-stalking. Some where even nice enough to follow me back. I would go as far to say that I made a few new, hopefully lasting friends in the past week.

So as I indicated on my WWDC exit survey, the best thing about WWDC was meeting other Mac developers. The second and third were interacting with Apple engineers and the technical sessions.

Talking with the engineers in the labs was extremely valuable (thanks Image Capture team!), and chatting it up during the extra-ciricular activities was just fun. It made me realize that although Apple’s engineers are definitely smart, but they’re not arrogant, unapproachable, coding geniuses. In fact, they were extremely friendly, and basically, humans just like me. Stunning.

The technical sessions were good, but not great. I got what I expected for most of them, which is way better than I’ve done at any one sort of conference. Also, I was very impressed by the baseline public-speaking and presentation skills of the engineers. I don’t know if Apple just hires good communicators, or trains them, or what, but the quality of end result was high.

Oh, and the sleeper hit of WWDC 2008? The super-nice staff. They gave the whole experience a very pleasant, friendly feel. Bravo, you black-shirted army. Bravo.

WWDC also inspired me to kick it up a bit. Though its against my policy to comment on future blog postings, I can say that I hope more will be coming “soon”. And one more thing…

Save me a spot in line July 11th :)

What I’ve been up to…

The frustrating thing about most projects I’ve worked on in the past is that they’re so technical or specialized or intangible that I can’t even describe to friends and family what I’ve been spending all my time on.  Not so this time around.  If you want to see what I’ve been working on (and you have a Mac), you can: 

PRE-ORDER: NEAT Receipts for Mac Advance Release  

Congratulations to everyone on the NEAT Receipts Mac team. 

Welcome to the #1 ranked Andy Mroczkowski site

One upside to having a name that contains consonant combinations previously thought to be impossible, is that is easy to get to the top of the Google search results.

FastMac MacBook Pro Battery

My original MacBook Pro battery went from providing a reasonable 2 hours of use to less than 10 minutes practically overnight. Also, this happened only weeks after the one-year warranty on my battery expired. Attempts to finagle a replacement battery failed.

Once bitten, I wasn’t all that thrilled about forking over $129 to Apple to replace something that I felt was defective in the first place. But what choice did I have? My search for alternatives only turned up one. Fortunately, that option was a good one.

I’ve been running on a FastMac TruePower MacBook Pro Battery for a month now, and everything is happy so far. FastMac’s battery features:

  • (allegedly) more capacity
  • $30 less price
  • same integrated charge indicators
  • almost identical appearance to the original
There is one feature that they don’t mention, at least not explicitly. The FastMac battery is two ounces lighter than the stock Apple battery. Two ounces might not sound like much, but I noticed it almost immediately. Also, I challenge you to come up with another, non-destructive way to shave weight off a MacBook Pro. Actually, I will revise that. Destructive ways are fine as long as:
  • its not my laptop
  • you post pics
I waited a month before sharing my experiences, because if I didn’t my experiences would have been “Uh, it works”. To gather some actual quantitative data, I’ve been using coconutBattery (which is a cool, and free).
 

Picture 1.png

Not bad, Battery. Keep up the good work.

Interview and Preview at MacMost

Quick one: I just noticed that the MacMost video interview and preview of NEAT Receipts for Mac has been posted.

smbd party!

I happened to look at Activity Monitor earlier today and noticed a “few” smbd processes running

smbd-small.jpg

 

I figured it was just a fluke and so I just restarted. However, all those smbd’s were not deterred that easily. They’re back, and in greater numbers:

$ ps waux | grep smbd | wc -l
148
I’m not even sure where to start on this one. I google’d for ‘mac too many smbd’ but didn’t turn up much. Hopefully they’ll get bored and move on.

MacBook Air at Macworld

Better late than never I guess.

Also, viddler is hosting the video this time. I heard about via TUAW and so far it seems like a nice service.

AppleCare Treasure Hunt

SMARTReporter (which is free and highly recommended), has alerted me that my Mac Mini’s internal hard disk is knock-knock-knockin’ on heaven’s door.

The good news is that the mini was purchased with AppleCare. The bad news is I don’t remember exactly when it was purchased, and the extended warranty may have expired.

I recall looking up warranty information on Apple’s site before, so I head over to http://www.apple.com/support/ and start poking around. Here’s a promising link:

applecare_register_and_view.png

“View AppleCare Agreements” sounds like exactly what I want. I log in, and behold!, I can see that I have two warranty agreements; one for the mini, and the other for my MacBook Pro. However, there is no indication if the agreement is still active or not. One could assume that only active agreements are listed, but I wanted to get the actual expiration date. The computer was purchased roughly three years ago and for all I knew the AppleCare agreement was expiring today.

So I continue to rummage around on the Apple Support site and get nowhere. Time for Plan B. After some googlin’ I come across a forum post which points me to https://selfsolve.apple.com/. I enter the mini’s serial number find that I’ve still got a month of coverage. Yippee.

But why wasn’t this information available directly once I logged into the Apple Support site? Why did have to google for five minutes to find this rather basic information? Frustrating, but at least my little saga had a happy ending.

So in summary, if you want to…