Archive for the 'Mac' Category

Navigating files and folders

If you're new here, you may want to subscribe to my RSS feed.

Each time you open Terminal, you begin in your Home folder. To confirm this, type pwd and press Enter. The command pwd tells you where you are (i.e. your “Present Working Directory”). Any commands you type there will by default occur in this folder (unless you refer to some other directory explicitly.)

After opening Terminal and typing pwd on my computer, it returned /Users/richard.

To navigate to a new folder, use the cd (”change directory”) command. For example, to switch to the Applications folder, type cd /Applications. You can then type ls to list all of your Applications.

To move deeper into the hierarchy, use cd and the name of the folder. For example, if I’m in /Users/richard, I can type cd Movies to move into the Movies folder. I’ll then be in /Users/richard/Movies.

To move back a folder, use two dots (..). For example, if I’m in /Users/richard and type cd .. I will then be in /Users.

Directories, Files, and Path Names

In Unix, folders are called directories. Files are still files. The location of a file is called its path. The very top folder (or bottom folder depending on how you think about it) is called the root directory. This root directory is the one you see when you double click your hard drive. It contains the Applications, Library, System, and Users folders.

Here is how you refer to various parts of the file system in Unix:

  • The forward slash (/) refers to the root folder.
  • A path name beginning with a slash (/) is an absolute path. For example, the path /Applications refers to the Applications folder, no matter where you currently are. To see a list of Applications, type ls /Applications. It doesn’t matter where you currently are because it’s an absolute path.
  • A path name beginning without a slash is a relative path. For example, if you are currently in your Home folder, the path Documents refers to the Documents folder under your Home folder. While in your Home folder, type ls Documents to see a list of your documents. If you were to type ls Applications (without the slash), you’d likely see an error since you (probably) don’t have an Application folder inside your Home folder, and the path is relative to where you currently are.
  • The tilde (~) refers to your Home folder. (It’s also an absolute reference, despite not having a preceding slash.) To see what’s in your Home folder, type ls ~. To see your own Documents type ls ~/Documents. It’s an absolute reference that works from anywhere. For me, the path /Users/richard/Documents is the same as ~/Documents.
  • You can use the tilde with someone else’s username to refer to their Home folder. For example, to see what’s in Mary’s Home folder, type ls ~mary or to see what’s in John’s Documents folder, type ls ~john/Documents.

You should now be able to refer to any path on your hard drive. Did I leave anything out?

How to get help with Unix commands: the “man” command

When discussing what a Unix command is, I mentioned that you can alter the function of a Unix command by using flags or arguments. For example, to list all the files in your current folder, you use the ls command. To reverse its order you add the “r” argument: ls -r. But how do you know which arguments to use?

Unix has a built-in manual called the man system. Each Unix command comes with a manual page which you can access by typing man and then the name of the command. For example, to get help with the ls command, open the Terminal and type man ls.

Each manual page shows the name, synopsis, description, examples, and more for that command. You can use the up and down arrows to scroll through the entry. To skip Forward a page hit “f”; to skip Back a page hit “b”. When you’re done reading the manual page, his “q” to quit.

The man command is one of the most important commands for learning and using Unix. I use it daily.

On a side note, these manual pages are also available on the Internet, including at Apple.com: Mac OS X Man Pages

Back to business

My two weeks in Brazil were awesome but I am also glad to be back. If you’re joining us for the first time, this site is all about using Unix (Terminal) on your Mac. If you need some background, start here:

Two week hiatus

I’m very excited for FreeMacUnix.com. I’m looking forward to having an outlet for discussing how Unix can make every Mac user more productive. I sincerely enjoy this.

Unfortunately for my baby, I’m taking a two week trip to Brazil. I’ll have only occasional access to email (I’m not taking my Powerbook) so I won’t be able to post on FMU until I return.

The first posts I’ve written have given some necessary background. Please join me at the beginning of October for more details on how Unix can make you a better Mac user. See you then.

What is a Unix command?

A Unix command is like an application, but it usually performs a much simpler task. There are Unix commands to show the contents of a file, to sort, to filter, and even to browse the Internet. Every Apple computer comes with hundreds of Unix commands that each do something unique. Contrast that with the dozens of applications that come with every Mac, some of which perform very complicated workflows.

Mac applications have Preferences for selecting how you’d like them to run. Unix commands, on the other hand, don’t (usually*) have any preferences or persistent settings. To specify how a Unix command runs, you use what are called flags or arguments. These flags or arguments are specified each time you run the command.

Arguments are specified with one dash and a letter (e.g. -a -b -c), or two dashes and a word (--help.) Sometimes certain settings or parameters can follow an argument (-u "stevejobs" or --user "stevejobs".) Whenever you use multiple single-letter parameters, you can combine them (-abc is the same as -a -b -c.)

Here’s an example. The ls command lists the files in a folder. Open Terminal, type ls, and hit Enter. You’ll see a list of the files and folders in your Home folder. Each name is listed one after another.

If, instead, you’d like to see all the files listed in columns, type ls -l and his Enter. You’ll see all the files listed in one column. It will also show the dates the files were created, and other information. This is the “long” format, as specified by the l flag.

You’ll notice that the list of files is in alphabetical order. If you want to reverse it, you can use the r flag. Type ls -l -r. The files will be listed in reverse alphabetical order. Since you can combine single letter flags, ls -lr will produce the same result. Most of the time the order of the flags doesn’t matter either, so ls -rl also produces the same result.

the ls command

What is a shell?

Hershey Shell photoThe term shell can refer to any interface to a computer (in the sense that the shell is the outermost part, the part people interact with.) But more commonly, shell is another way of saying “command line interface” or Terminal.

On Unix systems, there are actually multiple shells — bash, sh, csh, tcsh, and many others. The shell you use determines how you interact with the command line interface, but they’re all similar. The default shell on Mac OS X is bash, or “Bourne Again Shell”. Bash is the one we’ll be using.

To find out what shell you’re using, you can type echo $0 into Terminal and push Enter. It should return “bash”.

There isn’t much practical knowledge to be gained by knowing about the different shells, but I just didn’t want you to be surprised if you heard the terms shell or bash.

Further reading:
Wikipedia: Shell
Wikipedia: bash

Is anything unclear? I’ll explain!

What is the Terminal?

You’ll find Terminal in your Applications/Utilities folder. It’s through the Terminal that you access the command line interface of your Mac. In the same way that the graphical user interface (GUI) lets you operate your computer with a mouse, the command line interface (CLI) lets you operate your computer through typed commands. The graphical user interface is certainly easier to learn, but the command line interface can be very powerful! For instance, you can easily move a file to a new folder by dragging it with the mouse. But if you want to copy all the MP3 files anywhere in your Home folder, it’s must faster to use the command line, or Terminal. (You’ll learn how to do this.)

Terminal preferencesAs long as you’re going to start using Terminal regularly, you might as well make it at home. You can drag the Terminal to your Dock for easy access. Also, you might want to change the look of Terminal by going to Window Settings in the Terminal menu. Switch the drop down menu to Color, then change the color to your liking. I personally like White on Black with a little transparency, but you can do whatever you like.

You might also want to switch to the Display menu and choose a different font and font size. When you’re done, click Use Settings as Defaults.

If you really want to get fancy, you can also access the command line interface through a 3rd party application like iTerm. iTerm adds some niceties that Terminal doesn’t have, such as tabbed windows. I know people that really like iTerm, but I personally just use Terminal.

Further reading:
Wikipedia: Terminal Application
Wikipedia: Text terminal
Wikipedia: Graphical user interface
Wikipedia: Command line interface

Is anything unclear? I’ll explain!

What is Unix?

Unix logoUnix is a computer operating system that was developed in the 60’s and 70’s at AT&T’s Bell Labs. It was designed as a robust, multi-user, multi-tasking operating system and has historically been popular on large mainframe systems at universities and governments.

Since then, many variants have descended from the first Unix, including Linus Torvald’s college project Linux. Unix was originally spelled “UNICS”, an acronym for Uniplexed Information and Computing System.

Mac OS X is built on a version of Unix called FreeBSD, which means that every modern Mac inherits the strength of a powerful, time-tested operating system. FreeBSD is widely regarded as one of the most secure operating systems. Apple combines this with a beautiful interface. Mac OS X is like the engine of a classic muscle car in a shiny, modern body.

Further reading:
Apple: Solid as a Rock
Wikipedia: Unix
The Unix family tree (diagram)

Is anything unclear? I’ll explain!

Welcome to FreeMacUnix!

Terminal iconI’m excited to launch FreeMacUnix, a website all about using Unix on a Mac. You may have noticed the Terminal application in your Utilities folder and wondered what it was. Through Terminal you can access the powerful Unix operating system that powers every Mac. On this blog I’ll teach you what Unix is, how to use it, and how Unix shortcuts can make your life easier. I’ll start at the very beginning — no prior knowledge required — so I hope you’ll join me!