Terminals / consoles / command line for absolute beginners

Introduction to using the commandline

Terminals / consoles / command line for absolute beginners
Since we just use a keyboard now this little fella can get a little rest (image by Pixabay on Pexels)
This guy even colored his terminal matrix-green (image by Christina Morillo on Pexels)

You’ve probably seen the Matrix-like screens of a terminal before. Maybe some guy in a movie, wearing a black hoody, sitting in a dimly lit room, grunting “I’m in” hacking the Pentagon or something like that.

The reality, sadly, isn’t that cool. On the other hand the ability to work with a terminal is an invaluable tool in our toolbelt. Whether you are working as a developer or data scientist; a lot of software we use on a daily basis depends on the terminal. Some familiar ones include:

  • Installing Python packages with pip or creating virtual environments
  • Building images and running containers with Docker
  • Working with git
  • Executing programs
  • Building your code

If you are a developer or a data scientist and want professionalize, writing production-ready code then you’ll need to be able to use the terminal for some tasks. In this article I’ll explain what a terminal is, what it does, why it exists and how you could use it to make your life easier. At the end you’ll understand what a terminal is and how it works. You’ll be able to navigate your file system, execute programs and even create files!

Before we start

If you are unfamiliar with any of the terms in this article please consult this article for an overview of most things in programming. In addition: in this article I’m using the word terminal. You can also use console or command line; they are synonymous. Lastly I’ll use examples that pertain to Windows but the principles are the same in MacOS or Linux.


What is a terminal?

Your computer works with commands. If you click on Windows Explorer then the computer receives a command that tells it to open Explorer. It then opens the program and displays it on the screen. The buttons on your screen, the folders you see and your desktop are just an interface for these commands. You don’t have to write commands if you can just double click on chrome.

The most famous terminal of all (image by Markus Spiske on Unsplash)

Sometimes, though, you need to work with a computer that has no desktop interface. Think e.g. of a server that runs your database. A terminal allows you to control the computer without the need for an interface that helps you formulate your commands; you can just type them directly. We’ll see an example after we open up a terminal.

Opening a terminal

Computers with an interface always have a terminal. Let’s check out how it looks! Search your system for “terminal” and I’m sure you’ll get a hit → click it (in Windows it’s called command prompt, in other systems it’s most likely called terminal. See that big, black box with the white text and the flashing underscore that just popped up? That’s your terminal. Don’t worry, typing something here won’t break your laptop. Let’s find out what it does.


Working with a terminal

In a terminal you can write commands for the computer to follow. It allows you to do everything (and more) that you normally can do with an interface. In stead of clicking on the button to start Chrome you can also give the command start chrome (if you have chrome installed). You can even call start chrome https://mikehuls.medium.com to open a specific page.

In Windows explorer you can double click in folders to open that folder up and see its content. Navigating from folder to folder is also possible in a terminal:

  • cd change directory
  • dir show folder content (ls in MacOS and Linux)

Now we’re going to put these two commands to the test: open a terminal and execute the following:

  • Go to root with: cd c:/ (or on MacOS/Linux cd /)
  • Check out the contents with dir (MacOS/Linux ls)
  • Choose one of the directories (e.g. users) and type cd chosenfoldername
  • Check out the contents again with dir (MacOS/Linux ls)

You’ve just navigated from your root folder to a child folder! But what if we want to go back? In windows explorer there’s a convenient back button. We can use cd .. to go ‘up’ one level. Calling that command in c:/this/is/a/folder will navigate to c:/this/is/a.

One last thing is that we can navigate multiple folders at once. If you are in your root directory you can simply call cd c:/a/very/deep/path. To navigate 4 folders deep in one command! Notice that this is the same as typing the path in the Windows Explorer toolbar.

Typical terminal user (image by cottonbro on Pexels)

Executing programs

Okay, so we’ve learnt that double clicking a folder in Explorer is the same as executing cd but how do you execute a program? First we’ll need a program to execute. Let’s install Python from the python website.

Once it’s installed you should be able to execute python — version. This is already an example of executing a program. We’ve executed python and asked it to give us back its version. Let’s take this a bit further; let’s create a small script in Python and execute it in the terminal.

  1. Create a file at c:/applications/python/test.py
  2. Open it with a text editor (Sublime / Notepad++ / vscode or just notepad) and give it the following content:
    print(“Our script is working!”).
    Make sure you save it.
  3. Navigate to the folder containing the script with
    cd c:/applications/python
  4. Executing dir (or ls if you’re on MacOS or Linux) should show you our newly created test.py file
  5. Execute python test.py. This command tells the machine to execute our file with Python. We should see the return (“our script is working!”) in the terminal! (we can also call this script from another location but then we have to specify the entire path: python c:/applications/python/test.py).
Running a python script from our terminal (image by author)

We’ve successfully executed a python program from our terminal!

Bonus: creating files in windows

In the code above we manually create the python file (test.py). Blegh! I don’t want to use a mouse! Let’s learn how to write simple files (more elaborate files definitely require a text editor).

Check out the ‘echo’ command. It just echo’s what you just typed by printing the text in the terminal: echo Hello I’m just echoing you. Now check out this command: echo print(“created this from the cmd”) > test2.py. It tells your laptop to echo print(“created this from the cmd”) and put it into a file called test2.py (that’s what the > is for). If you now dir you’ll find that you have another file in your directory. Execute it with python test2.py to see what it does!

System paths

Nice to know: how does the machine know to execute Python with just the Python command? This is solved by our PATH variable; a setting in Windows that lists which executable (i.e. program) to call when a certain key is provided.

If we call python test.py windows checks the path variable for a key called “python” and then passes the arguments (test.py) to the executable it found (most likely something like C:\Program Files\Python39\python.exe). The Python installer created this path variable on installation.

Conclusion

I hoped a made the terminal a little less scary and your life a little easier! I hope this article was clear but if you have suggestions/clarifications please comment so I can make improvements. In the meantime, check out my other articles on all kinds of programming-related topics like these:

Happy coding!

— Mike

P.S: like what I’m doing? Follow me!

Join Medium with my referral link — Mike Huls
As a Medium member, a portion of your membership fee goes to writers you read, and you get full access to every story…