Learning the Terminal on the Mac - Introduction and Moving Around

June 30th, 2008 Andy Mroczkowski

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!

Posted in Learning the Terminal, Mac, Terminal | 1 Comment »

WWDC Comedown

June 15th, 2008 Andy Mroczkowski

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 :)

Posted in Apple, Cocoa, programming | 1 Comment »

What I’ve been up to…

April 17th, 2008 Andy Mroczkowski

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. 

Posted in Apple, computers, neatreceipts | 1 Comment »

Welcome to the #1 ranked Andy Mroczkowski site

February 17th, 2008 Andy Mroczkowski

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.

Posted in Web, blogs, google | 2 Comments »

FastMac MacBook Pro Battery

February 16th, 2008 Andy Mroczkowski

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.

Posted in Apple, computers | No Comments »

Interview and Preview at MacMost

February 14th, 2008 Andy Mroczkowski

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

Posted in Apple, macworld, neatreceipts, video | No Comments »

smbd party!

February 2nd, 2008 Andy Mroczkowski

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.

Posted in Apple, computers | No Comments »

MacBook Air at Macworld

February 2nd, 2008 Andy Mroczkowski

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.

Posted in Apple, computers, macworld | No Comments »

AppleCare Treasure Hunt

January 26th, 2008 Andy Mroczkowski

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…

Posted in Apple | No Comments »

Keynote, ‘Cloverfield’ Style

January 23rd, 2008 Andy Mroczkowski

Heres some shaky low-quality video I nabbed at Macworld.

I have more photos and videos I want to share, they’re all on my mini, and SMARTReporter says that its drive is about to die. They’ll be up once I get the data out of harm’s way.

P.S. I haven’t actually seen Cloverfield. Any good?

Posted in Apple, macworld | No Comments »