Basic Terminal Commands for Beginners

We all know the scenes from the movies: cool hacker dudes in black hoodies are sitting in front of a laptop, staring at green characters and sending commands in lighting fast speed. And the final result is always reached in the last second. Is this simply Hollywood drama or is there a true core to it? With this blog post you'll get the basic idea about the terminal, the basic commands and how it might improve your workflows. Lets go!
Basic Terminal Commands for Beginners

Operating System, Shell, Terminal, Bash

At first it can feel overwhelming. There are a lot of different words used interchangeably for the same or different things.

Lets get it straight:

  1. You are using an operating system (for example macOS). This is mainly called OS for short.
  2. On the OS runs an application that you use for entering your commands. This is the Terminal.
  3. Your entered commands are interpreted by the Shell.

And that's basically it. But what is a Bash? A Bash is a specific Shell implementation. There are many more, most commonly known:

  • Bash
  • ZSH (This is the current macOS default Shell)
  • Fish

There are many more synonyms used out there: like CLI (Command Line Interface) refers to the way you interact with a system using commands — the terminal is the application that provides this interface. But with the understanding of the key terms above you're well suited to understand what you're doing. So let's dive into it!

Basic navigation

To make use of any command you have to know your way around and most importantly where you are.

To fully understand the navigation, you have to understand that your files are stored in a tree-like structure. Every folder has a parent folder and can have child files or other folders. To get an idea where you are, you can use the command pwd, which stands for print working directory.

$ pwd
> /Users/max

To navigate around you can use the cd command (named for change directory). To navigate into a folder you can simply state the name of the folder.

$ cd folder
$ pwd
> /Users/max/folder

But what are you doing if you want to navigate out of a folder? For that you have to know that every folder knows its parent folder, and every folder addresses its parent folder by two dots -> ..

Simultaneously, the current folder addresses itself with one dot -> .

So you can do the following:

$ pwd
> /Users/max
$ cd folder
$ pwd
> /Users/max/folder
$ cd ..
$ pwd
> /Users/max
$ cd .
$ pwd
> /Users/max

But what if you don't know how the folders are named in your current location? You need a command to list the files in your current directory: The command you need is called ls. And there are also some variations to it:

$ ls
> blog-entry.txt
$ ls -l
> -rw-r--r--  1 max  staff  2971 Apr 18 19:23 blog-entry.tsx
$ ls -la
> drwxr-xr-x  5 max  staff   160 Apr 18 10:10 .
> drwxr-xr-x 32 max  staff  1024 Apr 18 10:09 ..
> -rw-r--r--  1 max  staff  2971 Apr 18 19:23 blog-entry.tsx

As you can see in the examples: most of the commands have attributes which you can append. When you append -l, you can see more details of the file, and with -la you also see hidden files. And there are the mentioned dot-notated relative locations!

File Handling

Navigating around is nice, but now we want to create files and look into them!

The simplest way to create a file is to touch it! All you need to do is to state the name of your new file:

$ ls
> blog-entry.txt
$ touch new-file.txt
$ ls
> blog-entry.txt
> new-file.txt

In case you want to copy it to another location you can simply use the short form of it: cp. In this case you need to pass two parameters: the file you want to copy and the target location:

$ cp new-file.txt ../parallel-folder

Did you see what we did there? We referenced the target folder relative to our current location by accessing it via our parent .. reference.

In case you want to copy an entire folder with all its contents you need to call the copy command recursively! You can do this by passing a -r flag:

$ cp -r folder ../parallel-folder

And in case something went wrong you can always delete files or folders. You can do so by again, calling the abbreviated version of it:

$ rm new-file.txt
$ rm -r folder

Getting most out of your terminal

Thinking back to the hacker in the black hoodie: how is he able to type the commands so fast? The answer is easy: terminals allow the auto-completion of commands by pressing Tab. So when you start typing a folder name for example, it is enough to type the first letters and press Tab. The terminal completes the name in case it is unambiguous. This works for commands, folder names, file names and more.

Also there are many different Terminal applications out there: I am currently using iTerm2 with a bunch of plugins. So it can be a good idea to look around which terminal suites you best (and also which color scheme).

Use it with caution

One word of advice: terminals rarely ask for confirmation!

When using the terminal you should always be aware of: your current location, your command and the potential outcome.

Pressing Enter after entering your command can mean you're creating a file or you're deleting your entire OS.

And while I have your attention: please use caution when copy/pasting commands into your terminal! Especially when you're a beginner and/or you're not fully understanding a command.

I can recall more than one situation where I witnessed hours of fixing because of one bad command. You really don't want to be in that situation!

Where to go next?

So, you mastered the very basics of the Command Line Interface! Congratulations!

Next you should open up your terminal and start using it! Navigate around, look into folders and try to create new files with the command line. You'll see that you quickly gain performance and confidence.

Next you want to get familiar with more advanced commands like tail, ssh or curl — depending on your needs. I'm sure there will be a future blog post talking about those!

If you liked this post and want to get informed about the upcoming new ones, consider subscribing to my newsletter! Every other week you'll receive a short mail with the freshest news from the Labs!

Thanks!