Lesson 1: First Steps with the Command Line
When Apple announced the release of Mac OS X, many Mac users were stunned: here was a new operating system based on the venerable Unix, which, they feared, would call into question the Mac’s legendary ease-of-use. Mac users have long been staunch supporters of point-and-click interfaces, and Unix, or so they thought, was the exact opposite. Since Mac OS X presents a graphical user interface atop its Unix foundations, there is no need for worry – seasoned Mac users can feel right at home (after some adjustments) with the new interface, and not even know about the Unix underpinnings that make Mac OS X run.
Not everyone was apprehensive, however. Many Mac users, especially those who have worked in large businesses or educational institutions, have for years used Unix variants, or flavors, such as one of the many Linux distributions. They were delighted to discover that they could use their Macs and still wield the power of Unix (specifically, FreeBSD 4.4, one of the oldest and most stable versions of Unix). They can have access to the myriad command-line tools available for Unix and the powerful programs provided as standard equipment in Mac OS X, such as the Apache Web server, numerous network utilities, a compiler and more. And they can still benefit from Apple’s easy-to-use graphical user interface.
Unix is reputed to be complex – its cryptic commands are said to offer a steep learning curve and not be accessible to "the rest of us." While this can be true – some Unix commands are like a foreign language – the command line can also be simple, useful, and powerful.
The goal of this article (the first in an occasional series to be published in TidBITS over the next year) is to present a brief example of how you can use Mac OS X’s command-line interpreter, the Terminal application, to run a few simple commands, and see exactly how they work. You will discover, if you follow this easy tutorial, that Unix doesn’t bite.
Opening Terminal — Terminal is the program Apple includes in Mac OS X to provide the interface between the commands you type and the operating system. Terminal is a "dumb" program – it does little more than pass information on to a shell (another program that interprets these commands) and display the results of these commands.
Start by opening Terminal, which is located in the Utilities folder of your Applications folder (or, to use the Unix convention, /Applications/Utilities). Double-click the Terminal icon. The Terminal window displays, showing something like this:
Last login: Mon Nov 25 16:03:03 on ttyp1 Welcome to Darwin! [Walden:~] kirk%
This text tells you several things:
The first line shows the date and time of the last login, followed by the terminal device ("ttyp1") being used.
The second line is the Message of the Day. The default message of the day for Mac OS X (as of version 10.2) is "Welcome to Darwin!", Darwin being the name of the Mac implementation of BSD Unix. (Like most things in Unix, you can easily change this text; we’ll see how to do that in a later article.)
The third line is the prompt. It first shows the localhost, or computer, that is being used: in this case, my Mac’s name Walden. The current directory, or folder, is shown after the colon following the localhost name. When you open a new Terminal window, the current directory is by default your Home folder, represented by the ~ shortcut. The final part of the prompt is the short name of the currently logged-in user, kirk. (Obviously, your localhost and user name will look different than mine.)
The prompt indicates that you can type commands. If Terminal is working on a command or displaying certain processes, you don’t see a prompt. If this is the case, you can always open a new Terminal window to type commands; you can open an unlimited number of Terminal windows, called sessions, at any time.
Typing Your First Command — Now that we’ve gotten through the basics, you’re ready to type your first command. Let’s start with echo, a simple command that displays what follows the command name in the Terminal window.
(Note: in the rest of this article, the commands and text you are to type are shown following the prompt sign, %, but without the localhost name and user name. You don’t need to type the prompt text; just type what follows the % sign.)
Type the following:
% echo Hello!
and press Return or Enter. Terminal displays the above line (the prompt and the text you type) as you type it. The echo command writes an argument to the Terminal’s standard output; in this case, the Terminal window itself. After you press return, it displays the following line:
Your Mac just said hello to you! Now, you can go even further – after all, you don’t know to whom it was saying hello. Try this command:
% echo Hello $user!
My Mac says:
All commands use a specific syntax – for simple commands, this is often just:
Arguments are additional information required for some commands; other commands run with no arguments. In the line of text you typed above, the command was "echo" and the argument was the text you wanted Terminal to display.
That was easy, wasn’t it? You have just used the command line under Mac OS X. The echo command is certainly simple, but it demonstrates basic command syntax and how Terminal works. (You’ve probably noticed that, after the above text, Terminal displayed a new prompt on the following line. This is a way of showing, as mentioned above, that the previous command has completed.)
You can now try typing the echo command with other text if you want.
Reading Directory Contents — Let’s see some more things you can do with Terminal. In the following series of commands, you will:
- create a new directory,
- examine the contents of the directory,
- create a new file,
- tell your computer to write some text in that file,
- read the file,
- then delete the file and directory.
The Terminal prompt shows that you are in your Home (~) directory. Let’s see what’s in this directory. If you recall from looking at the Finder window, Home contains a few folders. Type the following (the first letter is lowercase L, not the digit 1):
The ls command lists the contents of a directory. Terminal displays something like the following:
Desktop Library Music Public Documents Movies Pictures Sites
This is a list of everything in your Home folder (it may be different depending on the contents of your Home folder). However, this list doesn’t tell you which of the above items are files or folders. You can find out by typing the following:
% ls -F
Terminal displays this list:
Desktop/ Library/ Music/ Public/ Documents/ Movies/ Pictures/ Sites/
The -F is an option for the ls command; it is also case-sensitive: -F is not the same as -f. Options tell certain commands to do things in a slightly different way. This option tells Terminal to display a slash (/) immediately after each pathname that is a directory, an asterisk (*) after each executable (application), etc. The slashes here show us that these are directories. If any of the above items were files, there would be nothing after their names.
Creating a New Directory — Now, you’re going to create a new directory called Test. Type the following:
% mkdir Test
The mkdir command makes new directories. Let’s check to make sure this directory has been created by repeating the ls -F command, which shows:
Desktop/ Library/ Music/ Public/ Test/ Documents/ Movies/ Pictures/ Sites/
There it is: Test/, the directory that you just created.
Now we are going to move into that directory, using the cd command:
% cd Test
The cd command changes the current working directory. After running this command, the prompt changes to show that we are now in the Test directory:
As you’ve already learned, the ~ is a shortcut for your Home directory, and the slash means that the following directory resides inside the Home directory.
Creating a New File — Let’s now create a new, empty file inside the Test directory. Type this:
% touch testfile
The touch command is typically used to update file access and modification times, but it can also create a new file; the argument, testfile, is the name we’re giving to the file.
Let’s check to make sure the file was created. Type:
% ls -F
which should display the following:
Remember that the -F option for the ls command shows a / following a directory; it shows nothing for files. So we now have a new, empty, file called testfile sitting in the Test directory, just waiting for data to be put into it.
Writing Text to a File — Since our file is doing nothing, we might as well write something to it. How about writing Hello [username]! in this file? To do so, we can use the echo command that we learned above. Type the following:
% echo Hello $user! > testfile
This command tells Terminal to echo the text Hello [username]! to the file called testfile. Let’s check and make sure it worked. There are several commands that display the contents of your files; one of them is cat. Type this:
% cat testfile
Terminal should display:
But since we only see this in the Terminal window, it doesn’t give the same impression as when we open a document window in an application. Let’s see what this file looks in a graphical application. Type:
% open .
(Make sure you type open, then a space, then a period.)
This tells the Finder to open the current directory (the . is a shortcut for that) in a new window. You should see a new Finder window, entitled Test, with a file inside it called testfile.
Double-click the testfile icon, which should launch TextEdit and display a window containing the text "Hello [username]!"
Quit TextEdit to close the file, then switch back to Terminal by clicking its icon in the Dock.
Deleting Files and Folders — Now that we have finished our brief demonstration, we need to clean up a bit. We don’t need to keep that file and folder, so let’s delete them.
Warning! The command line is not without certain risks. Unlike when you work in the Finder, some tasks you carry out are absolute and cannot be undone. The command I am about to present, rm, is very powerful. It removes files permanently and completely instead of just putting them in the Trash. You can’t recover files after deleting them with rm, so use it with great care, and always use the -i option, as explained below, so Terminal asks you to confirm deleting each file.
Your prompt should look something like this, showing that you are still inside the Test directory you created earlier:
Type the following:
% rm -i testfile
The rm command removes files and directories, in this case the file testfile. The -i option tells Terminal to run the rm command in interactive mode, asking you to make sure you want to delete the file. Terminal asks:
Type y for yes, then press Return or Enter and the file is removed. If you wanted to leave it there, you could just type n for no, or press Return.
We should check to make sure the file is gone:
After typing ls, you should just see a prompt. Terminal doesn’t tell you that the directory is empty, but it shows what’s in the directory: nothing.
Now, move up into your Home folder. Type:
% cd ..
This is the same cd command that we used earlier to change directories. Here, the command tells the Terminal to go up in the directory hierarchy to the next directory (the .. is a shortcut for the parent directory); in this case, that is your Home directory.
Type ls again to see what’s in this directory:
You should see something like this:
Desktop Library Music Public Test Documents Movies Pictures Sites
The Test directory is still there, but using rm, it’s easy to delete it by typing:
% rm -d -i Test
The -d option tells rm to remove directories. When Terminal displays:
Type y, then press Return or Enter. (If you didn’t remove testfile, as explained above, the rm command won’t delete the directory because it won’t, by default, delete directories that are not empty.)
Make one final check to see if the directory has been deleted.
Desktop Library Music Public Documents Movies Pictures Sites
Summing Up — If you worked through this brief demonstration, you successfully typed commands in a Terminal window using the Unix command line. You created a directory (folder), created a file, wrote text to it, then deleted the file and the directory – and all with some very simple commands. Here’s a brief summary of the commands you used:
echo: displays arguments to the standard output; in the first example, this was the Terminal window, in the second example, it was an empty file.
ls: lists the contents of a directory (or folder).
mkdir: makes a new directory.
cd: switches to a different directory.
touch: creates a new, empty file (among other uses).
cat: views files (among other uses).
open: lets you open files or folders in the Finder.
rm: removes files or directories. Use with care!
Although you haven’t accomplished anything extraordinary so far, you can see that using Terminal isn’t that complicated. All it requires is a bit of time to learn the different commands and their arguments and options. But if you move ahead slowly, learning as you go on, rather than trying to memorize dozens of commands, you’ll soon find that you are not only comfortable with the command line, but that you can do things that help you save time and give you much more power.
In the next installment in this series, you’ll learn all how to move around in your computer’s file system. I’ll build on some of the commands presented here (cd, ls, mkdir, and others) and present many new, useful commands.
[Kirk McElhearn is a freelance writer and translator living in a village in the French Alps.]
PayBITS: Did this article help you dip your toes into Terminal?
Consider supporting Kirk with a few bucks via PayPal!
Read more about PayBITS: <http://www.tidbits.com/paybits/>