Get a free year on Tuts+ this month when you purchase a Siteground hosting plan from $3.95/mo
It may seem like the kind of geeky realm that only hackers are fit to inhabit, but the OS X Terminal is a powerful and versatile method of interacting with your computer that offers advantages to users of all skill levels. It’s quite a departure from what you might be used to though, so we’re here to help you take your first steps.
Navigating the Terminal: A Gentle Introduction.
The Terminal: Less Ominous Than it Sounds
Today we’re talking about some of the basics of Terminal navigation: how to move through your file system, create and delete files. But what is this Terminal thing anyway?
Way back in the old days of computing, before graphical user interfaces (GUIs) became the norm, the only way to use a computer was through a so-called command line interface. These simple text prompts allowed computer users to key in their commands using a language that was more familiar to the computer, and encouraged us to have a more solid understanding of how the machine interprets the metaphors with which we ask it to perform tasks for us.
For those who value performance and a streamlined communication path to the computer over a fancy graphical interaction system full of layered metaphors and skeumorphism, the command line is king.
Since the GUI revolution, you might expect that command line interfaces would have vanished, but that is far from the case. For those who value performance and a streamlined communication path to the computer over a fancy graphical interaction system full of layered metaphors and skeumorphism, the command line is king.
In OSX, the command line interface is called the Terminal, and I’m going to walk you through some baby steps that should help set the stage for future tutorials and independent exploration.
What is it Good For?
It’s first worth addressing why you might be inclined to use the Terminal in the first place; for many, it seems like nothing more than a relic of the early days of computing, or something that only offers benefits for hackers and developers.
While the Terminal is by no means a necessary tool for the casual Mac user, it does offer a surprising number of uses to the layperson, and it’s worth learning the basics.
You can think of using the Terminal as something akin to whispering directly in your computer’s ear rather than yelling at it from a distance; it’s a more intimate way of interacting with the system that affords you a lot of power. As a consequence, it pays to know what you’re doing so that you don’t accidentally damage your machine.
Step 1: Accessing and Configuring the Terminal
Let’s start with the basics: accessing the Terminal. To get started, you can either open the Terminal by navigating to Applications -> Utilities and finding the icon, or you can save some time — and get used to typing! — by hitting the Spotlight shortcut
Command + Spacebar and typing ‘terminal’.
Once Terminal is open, you’ll be presented with its default view. In essence, what you’re looking at is the equivalent of opening the Finder and navigating to your User directory. The part on the left displays your current directory, or location in the filesystem, and the input area on the right with the blinking cursor is known as the prompt because it’s prompting you to tell it what to do.
Welcome to the Terminal, looking spiffy in its "Homebrew" colour scheme
If you’re not a fan of the default colour scheme and window size, you can easily modify it to suit your preferences. It’s always nice to feel like a hacker when doing this sort of thing, so if you hit "Command+," to bring up the preferences page, you can pick a different theme (and customize it to your heart’s content) via the Settings tab.
For this tutorial, I selected the “Homebrew” preset and toyed with the typeface and opacity options until I found an ideal look for my system.
Step 2: Your First Command: Listing Files
An easy task that you’ll be performing a lot is finding out what files and folders exist in the given directory. Type ‘
ls’, which is Terminal shorthand for “list”, and you’ll see a chart of everything in that directory. ‘
ls’ is an example of a Terminal command, and now you’ve learned your first one! Commands are the instructions that you give to your computer via the Terminal, and they tend to share a basic syntax that you’ll come to understand by the end of our tutorial.
Let’s say we’d like to access something in our Documents folder. To accomplish this, we need to do what’s called changing directory. It’s the same as double-clicking to open a folder in Finder. To perform a change of directory, we simply type ‘
cd’ followed by the name of the folder we’d like to dive into — in our case, ‘Documents’.
Similarly, if you want to double-check where exactly you’ve ended up, simply type ‘
pwd’ — which stands for Present Working Directory — and Terminal will show you a clear path to your location.
Note that there’s always a space between the command and the argument, or subject of your command. This is true of all the separate elements in a Terminal command.
You've already learned a few key commands!
Step 3: Command Parameters
Terminal now indicates that we’re in our Documents folder. We’re going to have a look at all the files in this folder, but this time I’ll introduce you to the second half of Terminal command syntax: the parameters, or options. Let’s type ‘
ls’ again to list files in the directory, but this time we’ll append ‘
-a’ to the command before hitting Enter. This option is used to tell the computer that you want it to show you all files in that directory, including the hidden system files.
Revealing hidden files in a directory is something you may need to do fairly frequently, and now you know a way to accomplish it much faster than if you were using the Finder! Better still, you now understand the basic format of a Terminal instruction: a command followed by its optional parameters, and finally its argument.
The format of a Terminal instruction consists of a command followed by its parameters, and finally its argument.
Command parameters, like '-a', activate options for the command and vary from task to task.
Step 4: Opening & Editing Files
Say we found a file in our Documents that we’d like to edit. To open it, you simply type ‘
open’ followed by the name of the file. If your file name is long and you don’t feel like typing it all out, you can take advantage of Terminal’s auto-complete feature by typing the first few letters and then hitting the Tab key. I’m going to open this ‘ExampleFileWithALongNameWhyOhWhyItNeverEndsIDontEven.txt’ file.
We've opened our text document, and the auto-complete saved us typing the filename
The file has opened in my default application for that filetype, in this case TextEdit, and I’m free to work with it as I please. But what if I want to create a new text file? Typically I would have to open TextEdit first and then create it from there, but in the Terminal this couldn’t be simpler.
Step 5: More Navigation Commands
I want to create a new text file on my desktop, but you’ll notice that my Terminal is still showing the Documents directory. There are several different ways I could get where I want to be.
I could use the ‘
cd ..’ command to move back up one level in the filesystem — back to my User folder in this case — and then use ‘
cd Desktop’ to get there. I could also simply use ‘
cd /Users/Mathazzar/Desktop/’ to get there immediately from wherever I am, though that involves typing out the full path to the directory.
You will need to replace the ‘
Mathazzar’ in that command with whatever your username is for all similar steps in this tutorial.
The final and quickest method is to use a relative path command, which looks like this: ‘
cd ../Desktop’. Because both the Documents and Desktop folders share a common parent directory, we can use this so-called relative path command to quickly navigate between them.
Sometimes, if you need to work with a file or directory that’s buried deep or has a very long and complicated name, you can save yourself some typing by writing your command and then dragging & dropping the target file or folder into the Terminal window from Finder.
Just drag & drop a file from Finder to automatically include the path to it
Step 6: Creating Files
However you choose to make your way there, navigate to your Desktop. The command for creating a file is called ‘
touch’, so if we wanted to create a text file called bananas, we would type ‘
touch bananas.txt’. You’ll notice that the file has now appeared on my Desktop, ready for editing.
One thing that the Terminal isn’t incredibly intuitive about is handling spaces in file or folder names. If we wanted to call the file “banana muffins.txt” instead, you might imagine that we would type ‘
touch banana muffins.txt’. Go ahead and give it a try.
Working our way to the correct command for creating our recipe file
As you’ll see, Terminal interprets this as you wanting to create two separate files; one called “banana” and the other called “muffins.txt”. This teaches us that Terminal can create multiple files quickly using a single command. To clarify what we mean, we’ll re-type the command, this time putting the file name in quotation marks: ‘
touch “banana muffins.txt”’.
By the way, you can create many types of files like this, not just text files. If I needed a Word file to save my genius banana muffin recipe into, I could simply type ‘
touch “banana muffins.doc”’ to get started.
Step 7: Copying Files
Now let’s do some cleaning up, shall we? Since we know how to create files, it would help if we could delete them too. But because we’ve learned the value of backups, we’re going to make sure to preserve our previous banana muffin recipe first. I want to keep “banana muffins.doc” safe in my Documents folder, so I’m going to copy it there using the ‘
The syntax for this command is straightforward: ‘
cp’ followed by the file name —
“banana muffins.doc”, and remember to put it in quotation marks — followed by the path to the directory you want to save it in. In our case, the full command looks like this: ‘
cp “banana muffins.doc” /Users/MacTuts/Documents/"’, or, using a relative path command, ‘
cp “banana muffins.doc” ../Documents’. You can use Finder to double-check that it worked.
Step 8: Deleting Files
The command for deleting a file is ‘
rm’ and it’s one you’ve got to be careful with since you don’t want to accidentally delete anything you didn’t intend to. There is no Trash Bin in the Terminal world.
You don’t want to accidentally delete anything you didn’t intend to. There is no Trash Bin in the Terminal world.
Let’s start by removing that orphaned “muffins.txt” file we’ve got lying on our Desktop. The command for this is simply ‘
rm muffins.txt’. Just to be on the safe side, you can use the ‘
-i’ parameter to toggle interactive mode, where the Terminal will explicitly confirm your deletion command before executing it, in this case asking us if we want to “
remove muffins.txt?” We can then answer ‘
yes’ or simply ‘
y’ to proceed.
That still leaves us with some files. Rather than deleting them manually one by one, we can take advantage of Terminal’s intelligence to save some time. Since they all begin with the word ‘banana’, we can use the asterisk, known as the wildcard character, to ask Terminal to find and delete all files in the current working directory with file names that contain the word “banana”.
The '-i' option allows us to protect against accidental deletion
Type in the following command: ‘
rm -i banana*’ and hit enter. Terminal will automatically find all the desired files and prompt you to delete them. If you’re feeling confident and want to save time, drop the ‘
-i’ option to execute the command immediately.
Step 9: Making Folders
You’re doing great so far, and there’s just one last set of basic tasks that I want to teach you in this tutorial. Because we’re busy people with many files, it would be helpful if we stayed somewhat organized. Thus, instead of leaving that poor “banana muffins.doc” backup file out in the open in our Documents folder, we’re going to give it a new home inside of a folder called Recipes.
First thing’s first: we need to get to our Documents folder. If you’re somewhere deep in the filesystem and don’t feel like typing ‘
cd ..’ a bunch of times to get back out, you can use the convenient ‘
cd ~’ command to zoom back to your User folder. From there, we simply need to type ‘
cd Documents’ and we’re good to go.
Now, if we type ‘
ls’ again to remind ourselves of what’s in there, you’ll notice that there is no Recipes folder. Looks like we’re going to have to create one! Luckily, this is as easy as typing ‘
mkdir Recipes’. Once you hit Enter, the folder is created for you by this ‘
mkdir’ command, which means Make Directory.
We can create directories just as easily on the desktop too
Step 10: Moving Files
Now we need to get the recipe into this new folder, but rather than copying it over and deleting the original, we can simply move it. Type ‘
mv “banana muffins.doc” /Users/MacTuts/Documents/Recipes/’ to do this.
You can also use the ‘
mv’ command to rename a file if you like, so if we ‘
cd’ into our new Recipes folder, we can use ‘
mv “banana muffins.doc” “amazing banana muffins.doc”’ to rename the file.
mv’ command is used both to move and to rename files.
Achievement Unlocked: Baby Steps in Terminal
If you’re feeling overwhelmed, don’t worry: the Terminal is a completely different paradigm for computer interaction, one that takes time to get used to. We’ve only just scratched the surface, but if you’re interested in learning more, stay tuned for future tutorials where we dig a little deeper.
We’ve only just scratched the surface, but if you’re interested in learning more, stay tuned for future tutorials where we dig a little deeper.
In the meantime, you can read up on the syntax we’ve explored today and learn about the various parameters for each command by typing ‘
man’ — short for Manual — followed by the command name. ‘
man ls’, for example, will show you the usage options for the list command. Once you’ve read through it, simply hit ‘q’ to exit the manual and return to the prompt.
Of course, you’ll be seeing many more Terminal tutorials right here, but now that you’ve gotten a basic sense of how things work, we encourage you to do some exploring and try working with the syntax until it becomes second nature.
Soon, you’ll be ready to start tackling tasks that truly show off the Terminal’s power. In the meantime, don’t hesitate to leave your thoughts in the comments — we’re happy to help!