How to Use Terminal: The Basics

Glowing monitors filled with lines of scrolling green text aren’t limited to The Matrix; most IT gurus and power users prefer working with the command line over clunky graphical user interfaces because the CLI allows the user to operate directly with the system.

While the command line can seem terrifying at first, starting at the basics will allow you to see that it’s not much different from the graphical world you are used to. Learning how to use Terminal will give you a better understanding of how your Mac works under the hood, and give you the skills needed to troubleshoot any issue.

Choosing a Terminal Emulator

Before you take the red pill and jump into the complex world of bins and bashes, you’ll need to choose a terminal emulator. Like web browsers, various emulators have different features and interfaces. There aren’t many to choose from on OS X, but the default Terminal app will get the job done in a pinch. It offers horizontal pane splitting for viewing multiple inputs simultaneously, tabs, and a handful of built-in color schemes for making yourself at home.

If you want a bit more power, iTerm2 is a free emulator that includes more options than you’ll ever need. The app has all the features that Terminal.app offers, as well as enhanced pane splitting, customizable profiles, and a whole lot more that you don’t need to worry about quite yet.

Either will suffice for the purpose of this how-to, but I prefer to use iTerm2 for its additional features.

iTerm2: an alternative emulator

iTerm2: an alternative emulator

Navigating the filesystem

The most frequent commands you will use are ls (list) and cd (change directory). They are used for listing the contents of a directory and moving from one directory to another, just like Finder is used for browsing folders.

When you first launch a terminal, you’ll be plopped down in your home directory, a.k.a. “/Users/Name/”. To get an idea of how this relates to the GUI equivalent, open a new Finder window and select your name in the left hand column. You should see a few folders, including “Applications”, “Desktop”, and “Documents”.

The default Finder window

The default Finder window

Now, on the left hand column, select your hard drive in “Devices”. This location is the lowest level in the filesystem, called “root” (in command line terms, simply “/”). The root directory contains all the files that allow the operating system to operate, so it’s best not to meddle around here or you could cause all sorts of mayhem.

The root filesystem, shown in Finder

The root filesystem, shown in Finder

Listing Directories

In your fresh terminal window, type ls to list the files in your home directory. You should see “Documents”, “Music”, “Movies”, “Downloads”, and other directories that are created by default by OS X. If you type “ls -a”, it will activate the “all” flag to list everything—including files and folders that are hidden.

Listing the contents of your home folder in Terminal

Listing the contents of your home folder in Terminal

Moving Around

To jump into one of these directories, type “cd ./Foldername”. The cd command tells the computer that you want to change directories, and the “./” indicates that you want to move forward relative to your current location. Alternatives include specifying the directory using an absolute path (a path that remains the same no matter where you are at the moment). This can be done by typing the full directory path from root all the way up. For example, “/Users/Name/Documents/”.

Tip: as you are typing, hit the tab key to auto-complete (ex. “cd /Users/Name/Doc[tab]” will expand to “/Users/Name/Documents”). If your flanges are tired from all this typing, you can also drag a folder from Finder onto the terminal window and its path will be pasted on to the current line.

Changing directories in Terminal

Changing directories in Terminal

You’ve now done something useful. The first thing you usually want to do in your new directory is look around. Type ls to list the contents. Type “cd ..” to return to the directory above you (and re-read the above paragraph to make your way back!). Try moving around more to familiarize yourself with the cd command.

Listing the contents of another directory

Listing the contents of another directory

Basic File Operations

Now it’s time to perform some basic file operations like cut, copy, and delete. It’s probably best to make a test file so you don’t break anything important, so open TextEdit and create a file named “TestFile.txt” inside a new folder called “Test”. We’ll learn how to do this using the command line in the next How To.

Listing the file you just created

Listing the file you just created

Copying

To copy a file or directory, you will use the cp command. Type “cp TestFile.txt TestFile-Copy.txt” to duplicate the file. This will create a new copy of the file in the current directory, but if you want to copy the file into a different directory, use “cp TestFile.txt /Some/Folder/”.

Tip: If you are copying a directory, you will need to specify the “recursive” argument to copy the directory along with its contents (cp -r). You can now use cd and ls to view your creation, or cheat by opening a new Finder window.

Copying a file using the cp command

Copying a file using the cp command

Moving

The mv command can be used the same way as cp, but it will move the file instead of copying it. mv can also be used to rename a file by doing “mv TestFile.txt TestFile-Renamed.txt”.

Renaming a file using the mv command

Renaming a file using the mv command

Deleting

The rm (remove) command is used to delete files and directories. Of all the various commands at your disposal, rm is by far the most ruthless and least forgiving.

Only rm a file that you are positive that you want to obliterate, and if anyone tells you to run “rm -rf /” do not do it. The “-rf” argument stands for “recursively” and “forcefully”, so the prompt will not ask for confirmation. If you’ve been paying attention, you’ll know that the “/” directory is the root of your operating system. So, if you add all these options up, this command will “forcefully and recursively remove every file on your hard drive”. Not a good thing.

Now, crawl back out from under your desk and carefully remove your test file using “rm TestFile-Renamed.txt”.

Tip: you can alternatively use “rm -i” to “interactively” delete the file so the prompt will ask for confirmation before executing.

To delete the folder, use “cd ..” as you learned earlier to back out of the Test directory (you can’t delete a directory you are inside of!) and then use “rm -rf Test” to nuke it. Poof! The folder and all of its contents are now gone.

Deleting a file using the rm command

Deleting a file using the rm command

Conclusion

Now that you’re a certified hacker, feel free to poke around the terminal a bit more and test out different arguments for basic commands. Every CLI tool has a manual page linked to it that can be accessed using the man command (ex. “man ls” or even “man man”). It takes a lifetime of practice to master the command line, so get cracking! Check back soon with part two of this series for more Terminal tips!


  • Jason

    If you go into Terminal you’ll be sooorrrryyyyy. KEEP OUT!

    • noko

      Typical ignorant Macfag

    • http://ndrw.me Andrew

      He must’ve fallen for the rm -rf /

      DON’T DO IT ^

    • Daisy

      what do you mean by that? I looked at terminal and i dont understand it.

    • Jacob Jordan

      Y not go into terminal?

    • Jacob Jordan

      What does it do?

  • MikeB

    “an Terminal Emulator”? Ouch!

  • Damian

    What the color scheme are you using in this tutorial? It`s amaizing.

    • http://www.twitter.com/rewtraw Trey Trawick
      • Donny

        I tried it, but only the output has color, how the author color the input?

      • Damian

        Please can You share your .zshrc or how to set this arrow in prompt?
        Are you using oh-my-zsh?

      • Hawk

        Yeah, how do you get the command line to colorize? Is it a feature of zsh or is there a way to do it for bash?

  • jacques

    Why typing “cd ./Documents” when you can type “cd Documents”? The “.” is implicit and should be used when launching stuff (“./myscript”).

    • http://www.twitter.com/rewtraw Trey Trawick

      Either works, but you can also do “cd ../Folder” to move relative to the parent directory, so I like teaching the “./” method because it starts new users off on the right path. (pun intended!)

  • Donny

    I love the author’s color schema of iTerm, how can I do that?

  • LaurentR2D2

    I use TotalTerminal which is great since I male it appear
    on top of my screen with a simple shortkey.

  • Robert Cordes

    Thank you for the article! I’m just starting to use the command line, so this is really handy ;)

  • A. C.

    Because both Linux and OS X are close step-brothers, they have a great deal in common on the command line. Here’s a free book that covers more than most readers of this column need, but you’ll find just chapters 2 & 3 very helpful: The Bash Guide for Beginners, and it can be found online here:

    http://www.tldp.org/guides.html

    Another good reference, not a tutorial, is on the same page, the GNU/Linux Command-Line Tools Summary. A very brief summary of just a few of the most useful command line tools.

    I’ve found the command line most useful with ssh for connecting quickly with another computer, and with sftp, a quick, secure way to move files back and forth between two unix-based computers (there are a couple of decent GUI front ends for sftp). Very rarely, once or twice in the twelve years I’ve been using OS X, I have been able to connect to a Mac via the terminal and ssh when, for some reason, the GUI locked up. I’ve been able to shut down the offending program, or force a clean restart.

  • Hawk

    “info bash” also gives you an introduction and explains various features of bash.

  • Cookie503

    I’m a computer science student and at first I was sceptical regarding using a shell terminal to work my way through files and editing. That was one year ago. The Terminal, is the first thing I open once the computer is up, I can’t emphasize enough how much power, speed and efficiency it grants you. It worth learning to use it.

  • Pingback: Get To Know Your Utilities Folder: 7 Great Apps You Should Be Using | Design City

  • http://twitter.com/_ndrw Andrew

    Just thought I’d make the note that you guys haven’t written Part 2 and 3 and 4 of this guide yet. ;)

  • Olli

    Very nice. Enough small steps to simply read it quickly and understand. Thanks!

  • Computeruser

    I’ve been poking around the Terminal (and used our advice to test iTerm) and whenever I type something incorrectly, do something not allowed, or command to do something not possible, it comes up with

    -bash: hello: command not found

    Could anyone tell me what the “-bash” means?

  • http://www.imgur.com Richie

    UNICORNS

  • Gino

    This doesn’t work at all. What is “Tratium” anyway? and how came I can type “~” and it comes up with this same thing over and over, even if I didn’t type a real file name?
    “Ginos-Dad-iMac:~ ginosmoviestation$” IT DOES THAT EVERY TIME! Someone please explain why this is happening, it’s really annoying.

theatre-aglow
theatre-aglow
theatre-aglow
theatre-aglow