Introduction to the Command Line

One skill we will require as we move towards building servers in node is some dexterity with your computer’s command line (CLI = command line interface )– or terminal. The command line is a text based interface for your computer. It operates below the graphical interface you are used to using daily. In this context, typed commands and syntax (code) perform common tasks like looking in folders to less common programming tasks like creating node projects.

Unfortunately, this is one area where there are big differences between OSX and windows systems in terms of commands and processes. At the basic level though, we should be able to navigate the differences. There is a section below dedicated to getting a nix shell running on windows.

Isn’t the Command Line dangerous?

The command line is a powerful. Like all powerful tools it can be very useful and if used incorrectly very damaging. The command line will not ask you if you are sure you want to delete something, it will not confirm your actions — like any other line of code it will just do it. As a general rule one should not go around the internet finding and running random command line instructions. If you are not sure what it does think twice before you run it. I trust some sources implicitly; such as other instructors in this program, Tom Igoe’s tutorials, Shiffman’s Code Train series. Others I double check and make sure I am going in a good direction when doing something new. And, yes, it is possible to delete sections of a hard drive or all of one from the command line. But I can do that with the GUI too. I won’t be showing you how to do that.

So, given all that, why are we using it? The command line adds a lot of flexibility to your work flow and with some common sense it will provide you much more benefit than risk. It should be respected, not feared.

For this course we will mostly be installing node packages in local folders. We should not need superuser permissions for most (any?) of this work. We will not be using the command line to create batches of file system operations, nor will we use it to delete files (if needed, at this stage, stick the GUI for these sorts of things.)

Local, User , Global

As indicated above we will largely be working locally. By that I mean inside a folder that holds your Node project. Many online tutorials will follow other workflows, including global installations. Global can mean for every user of the computer or just the current user of the computer depending on context.

In general, I will avoid global installs when we are starting out. These means some things will need to be download more often that is absolutely necessary, but it helps keep our projects in single folders. There is nothing wrong with global installs and as you gain experience and confidence you will find many reasons to use them.

I mention this because you will search for your own resources and find tutorials that use slightly different workflows to achieve the same outcomes. You need to try and understand when a tutorial is using a slightly different path than ones you are familiar with.

Let’s get to it.

Ubuntu Shell on Windows

Different operating systems have different kinds of access to the command line. Unix systems and OSX have several flavours of command line — these flavours are called shells (i am simplifying — those of you versed in these spaces, bear with me). The standard in OSX for a long time was bash. Newer OSX uses zsh.

You can check out Shiffman’s work flow videos (also OSX biased) for much more in depth customization and discussion of various tools. The videos cover a range of tools not just command line, but they do have some good customization tips.

Windows users OPTIONAL install

David Bouchard and Mark Argo inform me that Windows users can install Ubuntu bash on Windows. It takes roughly 2GB of hard drive space. Instructions can be found here:

https://ubuntu.com/tutorials/install-ubuntu-on-wsl2-on-windows-10#1-overview

I am on OSX so have not done this but I trust David and computers more than I trust myself — so I consider this a strong recommend. I understand it is straight forward.

Opening Terminal or Command Prompt

In OSX you will find terminal in Applications –> Utilities –> Terminal. You can search for it using Spotlight. You may find it easiest to add it to your dock as we will need it frequently.

In windows there are several ways to open the prompt — I won’t pretend to know them all — but this may help. The quick version, press the start button and look for “Command Prompt”, or simply write “cmd” in the search field (source).

Quick Comparison of Basic Commands

Your whole computer can be operated from the command line — so this is a very rudimentary intro. You will find dozens of tutorials online for your particular OS.

Here is a starting list of the basic commands we will be using. You can check out this more comprehensive list as your skills grow.

I have tested all of the OSX commands directly — Windows commands have been confirmed for me by others, but I have not tested them directly. ( If you have suggestions I will add them).

TaskOSX/UnixWindows
change directorycd targetcd target 1
print current directory pwdcd 2
list files in directory ls dir
clear window clear cls
use app to open item app itemapp item3
auto complete TABTAB
last command UP_ARROWUP_ARROW4
start node nodenode
get node version node –versionnode –version
get npm versionnpm –versionnpm –version

Notes ::
1cd drag and drop from finder / windows explorer
2cd on its own in windows prints current directory
3,4 — this needs to be reconfirmed – confirmed by Anita (nA-2022)

Video :: Simple Command Line Tasks

Going Further

[Getting started with Node](add link)