How to use a terminal
Tags: cli, beginners, programming, commandline
Published: 2021-11-11 20:01:45

In this article, I’ll go over the fundamentals you need to understand the terminal. The aim of this article is not to explain all the possibilities, rather it is to give you a foundation from which you can build on.

> The commands I will use in this article work on MacOS and other Unix base operating systems. However, the same principles should apply to any other command line interface \(e.g. Windows\).

## What is a terminal?

You may be already familiar with the graphical user interface \(GUI\), that is one way of telling your computer what to do.

The terminal is another. It is mostly text\-based and can feel more primitive, but it is also often more powerful.

To open your terminal, you can just search for an application called _**terminal**_ and open it. It will likely look like a blank box, with a cursor and the name of your computer

![](https://stephenafamo.com/blog/store/media/Screenshot%202021-11-11%20at%2020.24.25.png "Screenshot 2021-11-11 at 20.24.25.png")

It can look quite scary but it really isn’t. Let’s jump into it and run a few commands.

## Running a command

To use a terminal, we type out a command and _press_ _enter_ to send it as our **input**, then the computer runs the command and sends out an **output**.

A very common command is `ls`. This will list out the contents of the current directory.

![](https://stephenafamo.com/blog/store/media/Screenshot%202021-11-11%20at%2020.24.34.png "Screenshot 2021-11-11 at 20.24.34.png")

That may look a little familiar, that is because it is the same as this:

![](https://stephenafamo.com/blog/store/media/Screenshot%202021-11-11%20at%2020.28.29.png "Screenshot 2021-11-11 at 20.28.29.png")

Another command you will likely use very often is `cd`. What that does is to **change directory**. Meaning, if we want to look inside the desktop folder, we would type `cd Desktop` and then _press enter_. We can then list the files inside the Desktop folder by using the `ls` command.

![](https://stephenafamo.com/blog/store/media/Screenshot%202021-11-11%20at%2020.37.53.png "Screenshot 2021-11-11 at 20.37.53.png")

To see some more details about the files, for example when the file was last modified, we can run `ls -l`.

![](https://stephenafamo.com/blog/store/media/Screenshot%202021-11-11%20at%2020.38.06.png "Screenshot 2021-11-11 at 20.38.06.png")

Now that you’re getting a feel for it, let’s break it down.

## The anatomy of a command

Commands are _generally_ made up of 3 parts.

* The name of the program, for example, `cd` or `ls`.
* Arguments. These are any extra values passed to the program, for example, we passed **Desktop** as an argument to `cd` when we ran `cd Desktop`.
* Flags. These are normally used to modify the behavior of the command. You will be able to identify them because they are usually prefixed with one or two hyphens. For example, we pass the `-l` flag to the `ls` command to make it print more details when we ran `ls -l`.

At this point, you may have some questions:

## How do I know which commands are available?

If you want to learn how to do a new thing, you can search for it through google. I assure you there will be tons of examples and recommendations.

The commands you will interact with most often are not too many, and you will likely get the hang of those pretty quickly. Here are a few

* `touch`: Used to create a new file
* `mkdir`: To create a new directory
* `rm`: To delete files of directories
* `cp`: To copy files or directories
* `mv`: To move\(i.e. rename\) files or directories

It is possible that you will install other command line programs on your computer, such as `git`, `homebrew`, `docker` and so on.

## How do I know the arguments and flags to pass to a command?

Most inbuilt programs come with a manual that you can access using the command `man`, which is fittingly a command\-line tool itself. For example, to view the manual of the `ls` command, we would run `man ls`.

![](https://stephenafamo.com/blog/store/media/Screenshot%202021-11-11%20at%2020.52.50.png "Screenshot 2021-11-11 at 20.52.50.png")

A convention among most external tools is to use the `—help` flag to display the help for the command. As an example, see `git`:

![](https://stephenafamo.com/blog/store/media/Screenshot%202021-11-11%20at%2020.54.39.png "Screenshot 2021-11-11 at 20.54.39.png")

## What to do next?

Google, Google, Google\!

Search for how to do something, and try it out. If you encounter an error, search for the error and figure out what is wrong.

Don’t try to memorize things. The commands you use frequently will become second nature pretty quickly, and the ones you don’t you can always find when you need it.