Diving Into Terminal

Take off the helmet and training wheels; let’s go.

Mary Lan
Learning Design and Technology

--

Terminal has always been one of those things to which I have a love/fear relationship. While I understand and appreciate its power and simplicity, I’m equally scared of the possibility that dropping a bagel on my keyboard will type out a fork bomb and disable my entire system.

So as I’ve begun to take off my own training wheels and getting more comfortable with the command line (CL) on Mac, I’ve compiled this list as a quick reference for those of you who want to come along for the ride. (Note that this not an exhaustive list. These are just some of the commands that I think are more commonly used.)

First, some basic conventions I’m going to use in this article — this will hopefully minimize any questions you have around semantics as you read. If you prefer, you can skip this section and the next and start with “Syntax”.

  1. I’m going to use the words “folder” and “directory” interchangeably. When you talk about a topic like CL, there are a lot of nerds out there (no offense nerds; I consider myself part of this group as well, considering I am writing a Medium article on Terminal of all things and not puppies.) who might want to split hairs on terminology. I’m not going to get into that here #TMTS (too much too soon).
  2. I’m going to use two styles to denote code examples. One will be inline, and it will look something like this: “utility -x argument”. The other will be a code block, like so:
$ This is an example of a code block
  1. Sometimes I will write a snippet example and you’re supposed to replace the example with a real file/folder/location/whatever. For example, if I say “cd <foldername>”, replace the word “foldername” with the name of an actual folder on your system. I am going to use these “< >” to denote this. Don’t type those in.
  2. I am only covering Terminal.app for Mac. Though CL terminology is pretty consistent across Linux distributions, there can be differences based on proprietary implementations. I am only going to write about what I have actually tested on own system (OS X 10.9.2). Odds are good you will get the same results.

Background

First, a very short intro on Terminal for context and grounding. The Terminal app you interact with on a Mac is considered a “terminal emulator”. It is basically recreating the Command Line Interface (CLI) Environment in a window within your GUI, the graphical representation of your operating system that you are used to interacting with. Both the CLI and the GUI are based on a Linux bash shell if you’re using OSX. What does that mean to you? It really just means that you’re using a very popular shell, that utilizes a shared syntax for CL, so when you type in something like “cd” on your mac and then on your friend’s mac, it should behave the same unless he/she has made custom modifications.

Syntax

Now, let’s start by breaking down the syntax of a simple command:

  1. The “$” symbol is a commonly used convention to indicate that the next few characters you’re going to see are Terminal commands. You don’t put the “$” into the CL prompt when executing commands. This may be obvious to some, but every noob (myself included) did this at some point, so it’s worth mentioning.
  2. A Utility tells Terminal what you want it to do. It can usually be run without Flags or Arguments. In the above example, “ls” is telling Terminal to return a list.
  3. A Flag (aka an Option, or Switch) modifies how the utility operates. It almost always starts with a dash. In the above example, “-l” is telling Terminal to return the “long listing” version of the aforementioned list.
  4. An Argument provides more information to the utility so it can act on your request. It’s basically saying “Hey, I want you to return a list of things that are on my desktop.”

Now, let’s get into some commonly used commands.

Navigating

$ pwd
Stands for print working directory. It shows you the current working directory (where you are).

$ cd
Stands for change directory. This is the bedrock of navigating inside Terminal. To change to another directory, just use:

$ cd <folder_name>

$ cd ~ (with a space between “cd” and the tilde)
Takes you back to your home directory.* The tilde is shorthand for the home directory of the currently logged in user.

$ cd / (with a space between the “cd” and the slash)
Gets you to the root directory.* The forward slash is shorthand for the root directory of the system.

$ cd .. (with a space between the “cd” and the dots)
Takes you back one level in your folder structure. So if your folder structure is folder1 → folder2 → folder3 and you are currently in folder2, it will take you back to folder1. You can go back multiple levels by typing “cd ../../..” with however many double periods separated by forward slashes as you need. (Note there are no spaces between the periods and the forward slashes.)

$ ls
Used to list the folders and files of whatever directory you’re in currently.

*Note: The difference between a “root” directory and a “home” directory is that the “home” directory is actually a subdirectory of the “root” directory. Said another way, the “home” directory is the root folder for a specific user while a root directory is the root folder for the entire system (e.g. your computer), which may have multiple users.

Dealing with Files and Folders

$ cp
Used to copy a file. To create a new file in the same directory as the existing file with a new name, use:

$ cp <file_name.txt> <new_file_name.txt>

To create a new file in a different directory as the existing file, use:

$ cp <file_name.txt> … <target_location>

$ mv
Used to move a file to a new location, like so:

$ mv <file_name.txt> … <target_location>

If you want to move a file into a folder structure several layers deep, use:

$ mv <file_name.txt> … <folder1>/<folder2>/<target_location>

You can also use $ mv to rename a file, like so:

$ mv <file_name.txt> <new_file_name.txt>

$ mkdir
Used to make a new directory. You can make a folder at a time, or multiple folders (in the same directory) all at once with “-p”.

$ mkdir <folder1>
$ mkdir <folder1>/<folder2>
$ mkdir -p <folder1>/<folder2>/<folder3>/<folder4>/<folder5>

$ rmdir
Used to remove a directory, permanently. Note that this will only remove empty folders. If you have files in them and want to wipe out the whole thing, you can use “rm” (below).

$ rm
Used to remove a folder or file, permanently. Use with caution. Note that this command can be used to remove folders with files in them; whereas “rmdir” cannot.

$ touch
Creates a new empty file.

$ touch <new_file_name.txt>

$ cat
Short for catenate, a synonym for concatenate. You can use cat to join two (or more) files, like so (note that there is a “>” symbol in between the two file names that you will want to type in.):

$ cat <file1.txt> <file2.txt> > <new_combined_file.txt> 

Additionally, you can also use cat to make a new file based on an existing file, like so:

$ cat <file_name.txt> > <new_file_name.txt> 

Cat can also just display the contents of a file. So let’s say you have a text file called “HW.txt” and the contents are “Hello world”. If you typed “cat HW.txt” you would see the text “Hello world” returned back to you.

$ less
Displays the contents of a file, but within another viewport. You can type “q” (the letter Q) to get out of this. When I asked a programmer friend what this view was, he said it was “like VIM”. I don’t know anything about VIM, so I asked what that was. His answer was such a typical programmer answer that it forced me make this, then move on from the question.

Opinionated programmer answers your question, but only kinda…

The “opposite” of “less” is apparently “more”, which will show you the contents of the file in your terminal window. I don’t really understand how that’s opposite, but okay. It seems similar to what “cat” does when presented with a single file argument, but it doesn’t do any of the concatenation. Now this is where my understanding of this topic is clearly not up to snuff to explain it to anyone else, so I’ll just end this section with the assumption that if you’re reading this article, you probably won’t need to know the nuances yet anyway. So, sufficed to say, for all practical purposes, “You can do $more with $less.” (Nerd humor courtesy of @davidbrear04.)

$ curl
A command line tool for getting or sending files using URL syntax. So to get the source code for a site, you would type:

$ curl <www.sitename.com>

and it would return the source code into your terminal window as a default. If you want to save the source code as a new file, you would type (with a lowercase letter “o”):

$ curl -o <file_name.html> <www.sitename.com> 

Searching

$ find
Finds files or folders. You can use the wildcard symbol (*) to search for something for which you only have a partial name, like so:

$ find <*.txt>

$ grep
Searches within files. Stands for “globally search a regular expression and print”. If you want to find a word or string in a file, you would type this:

$ grep <search_term> <target_file.txt>

Note that search terms are case sensitive. To make it ignore case, add the “-i” (ignore) flag, like so:

$ grep <search_term> -i <target_file.txt>

If you want to get results that do not contain something, add the “-v” flag to give you matches that do not have the search term specified, for example:

$ grep <search_term> -v <target_file.txt> 

Searching within a file
Say you’ve used “less” to open up a file and you want to search for the word “banana” in that file, you’d type: “/<searchterm>” and it will highlight all instances of that word. Use “n” to go to tab to the next result, and “N” to tab back to the previous result.

Getting Help

$ man
Gets you access to manuals/helper docs. Syntax would be “man <utilityname>”. This is helpful when you need syntactical help, but these references are an adventure in and of themselves. Clearly no Content Strategists were involved in the writing of these guides.

$ apropos
Sometimes you don’t know what you’re looking for exactly. In this case, “apropos” helps you figure out what “man” pages are, well… apropos.

Getting Serious

$ sudo
This is basically like taking off your training wheels, helmet, knee pads, epidermis, and diving naked into the ice fishing hole that is CL. Sudo stands for “Super User DO”. Your password is required to use this. Sometimes it’s unavoidable if you want to install something, but just use it with extreme caution.

Getting Silly

$ say
Gets Terminal to talk to you. I had way more fun with this than I should admit. Try:

 $ say <"The rain in Spain falls mostly on the plains">

You can even change the voice.

But there’s actually a practical use for “say”. For example, you could output a text file to an audio file and have it read to you. DIY audiobooks! For a quick tutorial on how to do this, go here. For a great free source of ebooks to try this out with, check out Project Gutenberg.

Using Flags

This is where things get a little hairy. Apparently, the same flag might work slightly differently based on the utility it is being called to modify. Use the “man” pages to help you figure out which flags to use for a specific utility. Note they are case sensitive, so a “-a” might behave differently than a “-A”. Not a big deal if you’re used to this, but something to watch out for if you’ve been case-promiscuous in less strict languages.

Tip: You can join flags. For example, “-af”.

Shortcuts

  • “ctrl+c” closes whatever Terminal is doing. I.e. if a process is running and you want to stop it.
  • “ctrl+l” clears the terminal screen. Ahhh, clean.
  • Type in “!!” to repeat the last command. You could also press the up or down arrows to cycle through last/next commands. This is a great time saver and is called “command history”. I believe it remembers up to 2k. I haven’t tried.

Other Tips

  • Is Terminal case sensitive? The answer is Yes and No. E.g. typing “q” will execute a quit command the same as typing “Q”, but this is because Terminal.app is doing some work for you to make case sensitivity less strict. There is a way to switch this off for Terminal if you wish (some people like the enforced discipline).
  • To search through your history, use “ctrl+r”. Thanks Curtis Hatter → for this tip.
  • You can run several instances of Terminal, either in new windows or new tabs. This is helpful if you are running something that is taking a while and want to do something else simultaneously.
  • Sometimes it will appear as if Terminal won’t let you do something, like remove a folder. E.g. if you put in “rm <somefolder>” it might respond by saying <somefolder> is a directory, while not deleting it. You can bypass this with the “-r” flag, so: “rm -r <somefolder>”. Terminal does this as a safeguard for you. In those instances, you just have to be more explicit about your intent, but there’s really nothing that you can’t do in Terminal if you really wanted to.

Liked it? Recommend it, add a note, or tweet it. It helps the content reach others and gives me that happy feeling. =) Thanks for reading.

Resources and References:

Special thanks to @davidbrear04 for contributing to and helping me proofread this article.

Getting to Know the Command Line

The Linux Command Line Beginner’s Guide
The kindle version is only $0.99 and it’s really good. An interesting read. (← Something I never thought I would say about a book on CLI.)

Learn CLI the Hard Way by @zedShaw

http://en.wikipedia.org/wiki/Cat_(Unix)
If nothing else, worth a chuckle about “cat abuse”.

http://en.wikipedia.org/wiki/Less_(Unix)

http://en.wikipedia.org/wiki/CURL

http://en.wikipedia.org/wiki/Grep

A-Z guide to the Command Line

--

--