Navigate OS X trough the Command Line (Part 1)

Command Line?

Those of you out there who have explored your Macintosh machine enough to look in the ‘Other’ folder in your applications may have seen it: that intimidating application called TERMINAL with the minimal black rectangle graphic as its icon. If you’re concerned about the security of your files, you may use Time Machine. If you live to burn CDs or have lots of different hard drives, then you may have used Disk Utility. Maybe you even like Windows enough to install it on your Mac and have used Bootcamp Assistant to do so. But when have you ever had to use Terminal?

For most of you keen and intelligent readers, the answer to the above question is easy: NEVER. Terminal is what’s called a ‘command line’ and most people never ever have to use a command line. However, what you have used is Terminal’s more attractive cousin: Finder.

The Finder, in my humble opinion, is such a perfect file viewing and organizing program that most people don’t even realize that’s all it’s doing. You have files and folders on your Desktop and you have your Documents and your Pictures and you can copy and paste and everything just works and is always where you want it to be. In the Finder, you never have to worry about directories and recursion!  For that reason, I suggest Finder is perfect at what it does: making the somewhat complex system of files and directories in the Mac operating system simple and easy to navigate.

So what does all this have to do with the TERMINAL? Well, the Terminal does everything that Finder does… and more!  As we’ll see through a few very simple examples, Finder and Terminal both do the same work just in slightly different ways.

So why use Terminal at all?

Great question, thank you for asking! There are many reasons for using the Terminal over the Finder, most of which go well beyond the scope of this article. A few key reasons to use the Terminal are speed and efficiency.

Every computer has a processor and a set number of transistors which can perform calculations. Great, what does that have to do with anything? The mathematical calculations performed by your computer’s (limited) processor create everything your computer does; that includes all the pretty stuff it shows on the screen.  With the Finder, you can have a lot on the screen: windows, icons, folders, your Desktop, etc. The Terminal, as I mentioned earlier, does the same work as the Finder but, as you’ll see soon, does it with a lot less stuff on the screen.

For this reason, the Terminal will use less of your computers limited processing power and will make your machine run faster and will make it capable of running more programs at once. On my old and ailing MacBook, I used the Terminal as a permanent substitute for the Finder in order to just allow the whole thing to run.

The Terminal also allows you to do some special things which the Finder doesn’t. For one, you can ask it to show you all the processors your computer is doing, or you can edit basic text documents, or enter the super secret parts of your computer that Apple hides in the Finder. You can also delete your entire hard drive by typing only a handful of characters. As Uncle Ben once said, “with great power comes great responsibility.”

Why Mac has a Command Line and a Brief History of Unix

Unix? This may sound a little strange at first but I promise it will all connect eventually.

Unix was a computer operating system which was developed by AT&T in the late 1960s and early 1970s (yes, that AT&T). Compared to the other operating systems at the time, Unix was a total revolution in computer capability and security. Unix allowed for different users with different accounts and passwords. Starting to sound familiar?

Throughout the next three decades, Unix spawned many operating systems, all sharing the same emphasis on security and task-oriented proficiency. They also all share very very similar command lines and terminal commands. In 1991, Linus Torvalds released the Linux Kernel to the world and birthed the amazing and wide-reaching Unix-like operating system which became Linux. In 1989 another Unix-like operating system called NeXTSTEP was brought into existence by businessman Steve Jobs’s company, NeXT.  By 2001, NeXTSTEP would be refined into Mac OS X by Apple following Jobs’s re-admission to the company.

Because Linux and Mac OS X are both based off of AT&T’s Unix operating system, they share most terminal commands. For this reason, knowing how to navigate OS X through the Terminal will give you a serious edge when using Linux.

Those of you wondering where Microsoft Windows fits into this: it doesn’t. While Mac OS X and Linux were based off the enterprise-oriented Unix, Windows was based off a modified version of an operating system called 86-DOS which was written by a small company called Seattle Computer Products. 86-DOS was turned (almost overnight) into MS-DOS which later went graphical and became Windows. DOS was never meant to access the internet and was primarily intended for use in home computers by enthusiasts; Unix, on the other hand, has had no trouble bearing the blunt of enterprise in today’s internet-based world. Explains a few things, doesn’t it?

Getting Started with the Terminal

The first thing to do is to, is you haven’t already done so, fire up Launchpad, open up the folder labeled ‘other’ and click on the Terminal icon. It should fire up and look something like this:

Terminal Window

So what to do now? I would recommend also opening up a Finder window right next to your Terminal window to help decode what’s being displayed in the Terminal.  With both Finder and Terminal open, you will want to do two things (in any order). First go to Finder and select from the ‘Go’ menu at the top the screen ‘Home.’ Now, on your Terminal, type in ls and hit return. You should see something not too dissimilar to this:

First Screen

Now take a look at what’s being displayed in each window.  Does it perhaps look like what’s shown on the Finder is also being listed in the Terminal? Sure, there are no icons and things look very different but everything has the same name! The reason for this phenomena: everything is the same! The Finder and Terminal are showing you exactly the same thing. What is this thing? That is going to require a slight conceptual leap.

How Files and Folders are Organized on OS X

People will often describe the vast groups of files and directories (a directory is just a slightly more techy name for folder) as a tree. However, that is nowhere near cool enough for me. I suggest, before we get into the nitty-gritty of talking about file systems, you watch this video. When the guy washes the dirt off of the ant-hill cast, you will see how the whole thing extends from a central stem and then grows lots of smaller, more localized tendrils.

This is very similar to how the directories are laid out in your computer. You have one big directory which contains every other directory. This big directory is called the ‘root directory.’ The root directory is often indicated by just ‘/’. As we delve deeper into the navigating the command line, you will find the slash shows up a lot in directory names.

If you were to look in the root directory, what would you see?  You can take a look in two ways: first by typing into your command line “ls /”. Your computer reads this as, “list the contents of the directory named /” or “root.” An absence of anything after the “ls” will just list the contents of whichever directory you are currently in.

What’s the second way to see the root directory?  It is here I will introduce our second command: cd.  You can see the root directory by typing in “cd /” and hitting return, then typing “ls” like we did before.  Your terminal window should look something like the picture below. For the purpose of comparison, I have also included a Finder window showing the contents of the root directory.Demo 2

So what did we just do when we typed in the cd command? Cd is named very appropriately and is the command for change directory. Remember from the beginning of the article that a directory is just another name for a folder. For cd you simply have to type in the command followed by the name of the directory you want to change into. With the cd command, you can surf from folder to folder without ever leaving the Terminal window or touching the mouse.

What we Have Learned and How to Use it

So now we know how to open up the Terminal, change directories, and view their contents. That’s great, but I am going to go out on a limb here and say that 100% of the time you use the Finder to open something up.  Can you open from the Terminal?  Yes, you can!

So how can we open files from the Terminal window?  Type the word open followed by the name of the file you would like to open. Super easy!

Let’s say, for the sake of demonstration, that you wish to open iTunes to listen to some Kraftwerk. We can navigate to our Application folder by using the cd command.  If you’re one step ahead of me by now, go ahead and type into your Terminal window: cd /Applications/

Once we’re in Applications, we have the option to list the contents of the directory by typing in ls (just to make sure has not gone missing). If you take a look at all the contents of the Applications directory, you may notice that things here look a little different; every application has a file extension of .app. Don’t be alarmed, Apple knows very well that very few people need to see applications with the .app extension and hides it by default in the Finder to make things look a bit more simple.  The in the Terminal window is, in fact, the same iTunes that you may come across in the Finder or in your Dock.

Now that we have iTunes in our sight, we can type in: open and it will open right before your very eyes; the same way it will if you open it from the Finder or Dock.

So Now What?

Now you can do some basic navigation through your computer on your Terminal. With cd, ls, and open you can do most of the things you would want to do on Finder. If you practice your typing, you may find that some tasks are just inherently quicker in the command line. I, personally, do not care for using a computer mouse and the Terminal allows me to do everything I need without ever taking my hands from the keyboard.

Additionally, the Terminal as an applications requires far less processing power than the Finder. If you end up like me and turn the Finder off (I will show you how to do that eventually), you may notice a decent increase in your computer’s performance.

As we’ll see in part 2 of this article (soon to come), there are also some excellent tasks you can use the command line for that you cannot use the Finder for.  Those are soon to come…