If you’re going to do any programming, you’re going to want to be able to move around in your computer’s terminal, also called the command line or the shell. Read on to learn what this is and why it’s useful.
1. Open your Terminal
2. What is the Terminal?
The exciting empty box you see when you open up the Terminal (also called the command line or the shell) is your direct line into your computer’s operating system. You can use it to navigate around folders, manipulate files, mess with the guts of your system preferences, and basically just do all kinds of fun stuff inside your computer. The biggest difference between the command line and other programs on your computer is that you don’t point, click, and drag your mouse; you enter commands as text.
The last line of text you see in your terminal should be a username followed by a dollar sign ($). That’s called the command prompt. It means your computer is waiting for you to talk to it!
3. Talk to your computer
Your terminal speaks in a language called Unix or shell script. When you use Unix, you can do a lot of the things you’d normally do by clicking and dragging, but a lot more slowly and labor-intensively. Just kidding, sometimes it’s slower and more labor-intensive, but sometimes it’s faster; it just depends on what you’re doing.
The first thing we need to do is figure out where we are. When you open your terminal, you’re dropped somewhere in your computer’s file system (usually the user’s home directory, designated by your username). To figure out where you are type:
pwd
and press return. pwd stands for print working directory, i.e., “Tell me where I am.”
4. Reading a path
Your computer returned some text showing you your location. If you think about your files on your computer, they form a sort of upside-down tree: the root is your home folder, and the branches extend all the way through multiple folders into all the individual documents you have on your computer.
The path your computer shows you works kind of like a URL: It starts at the root and then moves into subsequent folders. The name of each subsequent folder is preceded by a backslash (/). So in the path here, I’m inside a folder called miriamposner, which is inside a folder called Users, which is inside the main, root folder on my computer.
5. Figure out what’s inside the folder you’re in
6. Move to your Desktop
Let’s move into the Desktop folder, since that makes things easy to find. Type
cd Desktop
and press return. (Tip: If you type the first few letters of the folder, followed by the tab key, the command prompt can auto-complete the name of the folder.) cd means change directory.
By the way, if you ever need to move up the tree, you can do that by typing
cd ..
and pressing return. cd ..
means move up one level.
7. Make a text file with MAGIC!
8. Read your text file in your terminal
Back in your terminal type
cat hello.txt
(Tip: You can use autocomplete here again by typing the first few letters of the filename and pressing the tab key.) cat
technically means concatenate, but you can also use it to just read the contents of a file.
Your terminal should display the contents of that file.
9. Create a new file on your Desktop
10. Join those two files together, why not?
Let’s combine those two files! Type
cat *.txt > hellogoodbye.txt
This time we’re using cat
for its intended purpose, concatenating (or smooshing together) files. In *.txt
, the asterisk (*) acts as a wildcard, meaning any characters are allowable, as long as they’re followed by .txt. So this expression will concatenate all text files on your desktop and output them (>) to a new file called hellogoodbye.txt.
11. OK, that was fun
Working in the terminal is fun once you get the hang of it, and it’s really useful for things like combining files, moving things around, and converting file formats. Some weirdos even tweet from their terminals, but that’s pretty extreme.
Here’s a Unix cheat sheet. Using the cheat sheet, see if you can figure out how to:
- delete hello.txt
- rename hellogoodbye.txt
- create a new folder on your Desktop
- move (the file formerly known as) hellogoodbye.txt into that new folder
If you’re intrigued by all of this, maybe you want to know more. UCLA’s library of Lynda videos includes a video called Unix for Mac OS X Users, which will teach you everything you ever wanted to know (and more!) If you prefer learning from a book, our library subscribes to a free-to-you ebook called Learning Unix for Mac OS X.
When you’re done playing with your terminal, move on to the Python tutorial.