Getting Started with Terminal: An Introduction


If you ever wanted to hack the mainframe, using macOS’s built-in command line might be appealing. The command line interface, or CLI, is a powerful, text-only method for interacting with your computer. On macOS, it’s accessed through an application called Terminal. If you want to get started working with Terminal, here’s a quick Terminal introduction to get you on your way.

Opening Terminal

terminal introduction

Terminal is found in the /Application/Utilities folder on your Mac. It can also be accessed through Spotlight or Launchpad with the Terminal keyword.

When you open the application for the first time, you’ll just see a white window with some monospaced text on it, like the one below.

The first line tells you the time on your last login (which will be the current time) and the location of that login. That’s not really important for what we’re doing. The second line is where the money is.

Here, you’ll see the name of the computer that your logged in on. In my case, it’s rstr-MBP. Then, you’ll see a colon (:) and a tilde (~). This is the directory that your currently working in. The tilde represents the current user’s home directory, located at /Users/[username]. After that, you’ll see the user name you’re logged in with (in this case, it’s alexander) followed by a dollar sign ($).

After all that in a black box. That box is your cursor, sometimes called the command prompt. Here, you can start typing commands.

Our first command: ls

Most Terminal commands are a little cryptic. A lot of them are short abbreviations, which save time and keystrokes.

The first command we’ll look at is ls, which stands for “list.” The command lists all the files in your working directory.

To use the command, simply type ls. Then press the Enter key to submit the command.

You’ll receive by a list of all the files and folders in your home directory, which is the current working directory.

Modifying commands: Flags

The ls command by itself is not spectacularly useful, especially when you’re used to Finder. So what if it can show you a list of files? It’s harder to use, and it doesn’t have icons or anything.

This is where command modifiers come in. These modifiers are called “flags” and extend the functionality of a basic command. They’re preceded by a hyphen (-) and are typically one letter.

Let’s use the -l flag, which stands for “long format,” to extend ls. We’ll type ls -l into Terminal and press Enter.

What we get is a whole bunch of info.

Let’s break things down a little bit.

 

We’ve got a series of columns here, and each column contains different bits of information.

The first column is the file or directory’s permissions. These describe what kind of users can see and modify the files and folders.

The second column shows the number of “hard links,” which are a type of connection to a file or folder.

The third column shows the owner of the file, and the fourth column is the “group” of the file (normally “staff”).

The fifth column shows the amount of 4096-byte “blocks” a file occupies, which is a measure of the file or directory’s size.

The sixth column shows the last date the file or directory was modified.

And finally, in the seventh column, we see the file or directory’s name.

This is a lot of great information all at once, and we wouldn’t be able to see it this clearly in Finder. We’d need to pull up the Info window for each file individually, which would be annoying. Here, we see everything we need at a glance.

Other ls Flags

There’s a bunch of other really useful flags for ls. Favorites include the following:

  • -a, which shows all files in a directory, including hidden files
  • -h, which changes block size to a “human readable” file size, denominated in bytes.
  • -S, which sorts results by file size (note the capital S)
  • -t, which sorts by modification time

If you want to use multiple flags, you just string them together. For example, if I want to view the hidden files in a folder in long format and with human readable file sizes, I would use ls -lah.

Changing Directories: cd

If you want to change your working directory, you’ll need to use the cd command. cd stands for “change directory.”

You’ll use this command by first typing cd and then the path to a directory. A path is the route to a file folder, showing all the folders you need to go through. Folders are separated by forward slashes (/).

For example, if I want to navigate to the Extensions folder, I would type cd /System/Library/Extensions and press Enter.

terminal introduction

I’ll see that my working directory also changes to “Extensions” to reflect my change.

terminal introduction

Tips and tricks for cd

cd uses a lot of shorthand for navigating directories quickly. For example, a single forward slash (/) indicates the “root” directory, which is your system’s boot drive. As mentioned before, the tilde (~) indicates the current user’s home directory. A single period (.) indicates the current folder, and two periods (..) indicate the parent folder of the current working directory. The parent folder is the folder that contains the working directory.

But what if you want to navigate to a folder with a space in the name? If I just type in cd /Library/Application Support, I’m going to get an error.

terminal introduction

There are two ways to handle this. You can put the entire path in quotes (cd "/Library/Application Support") or you can put a backslash before the space (cd /Library/Application\ Support).

That backslash is particularly special. It’s called an “escape character” and it indicates that whatever comes after it should be handled specially. In this case, it indicates that the space is part of the file path, and not a space between two different commands or arguments.

If I want to change my directory back to my home directory, I can just type cd and press Enter. That will automatically return me to my home directory.

Other Commands

There are dozens of other useful Terminal commands. Now that you have a handle on the very basics of how Terminal works, you can start to expand your knowledge by seeking out other commands. Fan favorites include cp, which copies files, rm, which deletes files, mv, which moves and renames files, grep, which searches for information, and find, which locates files.

To find out how these commands work, you can type man (short for “manual”) followed by the name of the command. For example, if I want to find out how cp works, then I’d type man cp and press Enter.

This shows me the “man page” for cp, which describes everything I need to know about how cp works. When you’re done, press the Q key to quit out of the man page.

You might also like:

10 Tips for Using Terminal Like a Pro
5 Fun Terminal Commands To Try Right Now

Featured Image by Jason Scott (Flickr: IMG_9976) [CC BY 2.0], via Wikimedia Commons


Like it? Share with your friends!

Alexander Fox

One Comment

Your email address will not be published.

Send this to a friend