Command line is one of the most powerful tools on your computer, but the chances are, you may have never used it.

In a nutshell, command line (and the command line interface where you type) provides a way for you to interact with a computer program by issuing ‘commands’ to the program in the form of text.

Your computer’s command line plays a central role in setting up and running machine learning and AI algorithms. Understanding the basics of command line will not only improve your computing skills, but will also help you accomplish your machine learning goals. Bonus points for using command line to automate boring tasks like renaming thousands of pictures.

Never heard of command line before? Read on to learn how to use command line and why it will help you level up your ML game.


🤖 Table of Contents 🤖
What is the command line?
Why you should learn to use command line
How to get started with command line
Command line essentials
Tips for using the command line
Command line and Anaconda
Further reading


What is the command line?

The command line, or command-line interface, allows you to navigate your computer and run programs by typing commands directly to your computer’s operating system. Command line is text based, which means it doesn’t display images, but rather lines and lines of text. This might feel off putting at first, but the command line allows power users to control their computer behind the scenes using text input, in a similar way to coding languages like Python.

In the case of a command line input, you use commands to make your computer do things, rather than say, the Python syntax. You also don’t need to use a mouse for navigation – all interaction with a command line can be done through your keyboard.

Command line represents your computer (folders, programs etc) without the visual interface – it’s all there, you’re just accessing it in a different way and have a lot more flexibility for some of the more complex tasks you might want to carry out.

If you want to learn how to code, install or use Python libraries, or work with machine learning algorithms, then knowing the basics of command line is an essential skill.

FYI you might also hear command line called cmd, CLI, prompt, consoleterminal, bash, or shell.

Why you should learn to use command line

One of the most important reasons that people choose to learn how to use the command line interface is that it gives you more control over your own computer, allowing you to better understand how it works. Moreover, it’s a useful skill to have. It’s great for batching and automating tasks like renaming a thousand photos at once or moving many files from one folder to another. Perfect for the kind of tasks that you don’t realize you need to do quickly and easily until you do.

Even if you’re not interested in having a better relationship with your computer, odds are, if you’re interested in machine learning or data science, you’re going to come across the command line sooner, rather than later. Pretty much everyone else (at least in the data crunching world of intelligent machines) use command line, so it’s pretty hard to avoid. Even for the machine learning hobbyist, one of the first things you’re going to need to do is install different Python libraries and the quickest and easiest way to get this done is through the command line.

How to get started with the command line

The first thing to do is open the command line interface for your computer’s operating system.

To open command line in Windows:

  1. If you have Windows 10 click on the “Type here to search” bar and type: command prompt. Typing cmd should also get you to the same place. If you have an older version of Windows click Start and then “Search programs and files” before typing “command prompt”.
  2. Click on the Command Prompt application. It will look like this:

Another way of accessing Command Prompt is by holding the Windows key while simultaneously pressing the “R” key. This will open the Run window. Type “cmd” into the box and then click OK.

To open command line in Mac:

  1. Hold down the command key and hit the spacebar until a search bar pops up and then type “terminal”.
  2. Click on the Terminal application. It will look like this:

An alternative way to open command line is to go to Applications → Utilities → Terminal.

Once your command line is open you should see a white or black window waiting for your commands. You might see some information at the beginning like C:\Users\NAME > or NAME-MacBook:~ NAME$. This is called the command line prompt, or prompt, because it prompts you to input your command (whatever it is you want your computer to do). This also lets you know where in your computer you are which is useful when you want to move between folders. Each prompt closes with a $ (Mac) or > (Windows) and a space.

When written online or in textbooks, commands are prepended by this $ or > but you don’t need to type this into your command line. You should only type the command, which starts after the prompt.

At this point you might be thinking that if you type something wrong you’ll break your computer. If you’re not used to old-school tech then the black screen with white, blinking text might be a bit off-putting at first. It certainly doesn’t look as nice as the software that we’re used to using every day. In reality, you can’t do much harm to your computer through the command line, as long as you stick with the most common commands. Commands for tasks such as moving directories, installing Python packages, and renaming files are 👍

It is best to avoid places like forums, IRC or other places where “hackers” tend to hang out so you don’t get bad advice. Also, don’t use commands if you don’t know what they are going to do. Just saying.

Command line essentials

On Elle Knows Machines there are only a handful of commands that are used regularly. These are mostly around PIP installing Python packages and moving between different directories, however, here’s a list of some of the most commonly used (and useful!) commands that you can use in command line:

Command (Windows) Command (Mac) What it does
cd pwd Show the current directory
cd cd Change directory
dir ls List out directories or files
mkdir mkdir Make a new directory
copy cp Copy a file
move mv Move a file
[CMD] /? man [CMD] Help
exit exit Close the command prompt

Some commands differ based on the operating system so make sure you use the right command for your OS.

Let’s take a look at a few of these commands in closer detail.

Current directory

First things first. Let’s find out where we are. If you’re working in command line it probably means that you’re typing in all sorts of different commands and it’s possible that while doing this you got lost. If this is the case there’s a simple command that will print your current directory and let you know exactly where you are.

Windows

> cd
C:\Users\ElleKnowsMachines

Mac

$ pwd
/Users/ElleKnowsMachines

To navigate to other folders type:

Windows

> cd Desktop
C:\Users\ElleKnowsMachines\Desktop>

Mac

$ cd Desktop
ElleKnowsMachines-Macbook:desktop ElleKnowsMachines$

In this instance we navigated to our Desktop.

Since you can’t use your mouse in the command line, knowing how to navigate between directories is essential. It’s also one of the most commonly used commands.

If you get really lost you can always return to the root directory (the topmost folder in a folder hierarchy). Type:

Windows

> cd /
C:\>

Mac

$ cd /
ElleKnowsMachines-Macbook:/ ElleKnowsMachines$

From the root directory you can begin to navigate to other folders on your computer, although you’ll need to drill down the hierarchy of directories. So, for instance, if I want to navigate from the root directory to my desktop I’ll need to tell the command line to work its way through the following folders: C:\Users\ElleKnowsMachines\Desktop

Pro tip: if you type cd D (aka the first letter of the folder you’re looking for) and then hit the tab key, the command line will fill in the rest of the name for you. If there’s more than one folder starting with that letter, keep hitting the tab key until the folder you want is listed.

List files and directories

Alright, let’s say you find yourself in a folder and you want to know if a specific document is also in this folder.

Windows

> dir
Volume in drive C is OS
Volume Serial Number is BC27-A85B

Directory of C:\

30/07/2016  01:53  <DIR>      eSupport
03/12/2017  21:41  <DIR>      meta
12/04/2018  01:38  <DIR>      PerfLogs
16/11/2018  12:04  <DIR>      Program Files
19/01/2019  14:51  <DIR>      Program Files (x86)
15/06/2018  21:03  <DIR>      Users
05/01/2019  17:39  <DIR>      Windows
        1 File(s)           8,002,687 bytes
       11 Dir(s)       141,922,238,464 bytes free

Mac

$ ls
Applications
Library
Network
System
Users
Volumes
bin
cores
dev
...

With this command the command line will print a list of all files currently in that directory.

Make a new folder or directory

It’s really easy to make a new folder or directory from within the command line.

Windows

> mkdir newfolder

Mac

$ mkdir newfolder

As you can see, this command is short for “make directory”. You might be wondering what a directory is.

A directory is a folder – they’re the same thing. When you have folders within folders, or directories within directories, you have what’s called a “path”. A path is a set of directions that you give your computer, telling it where you want to put something in the tree of folders (directories) that make up your computer’s hard disk. It’s like saying “first folder A, then folder B, and then folder C – yeah, put this file in folder C.”

Pro tip: pressing the up arrow and down arrow on your keyboard will cycle through recently used commands which is a real time saver if you want to reuse a command.

Tips for using the command line

If you’re new to command-line interfaces, you might find that the command line works a little differently to what you’re used to

For instance, if you tell the command line to execute a command and nothing happens then that means your command was probably successful. In general, the command line does not have a loading bar or other indicator of completeness. If the computer can not execute your command it will tell you with an error message. No error message means it’s probably doing what you’ve told it to.

Sometimes things take a long time to process so be patient with your computer. If you do need to cancel a command (after several minutes of waiting) typing Command/CTRL + C will end the command. Again, this might take a few seconds to execute.

Likewise, if you do receive an error message, that’s ok.

Read through what the error message is telling you and try to understand the logic of why the command may have failed. The first thing to check – always – is whether you’ve spelled and typed everything correctly. Typos are one of the easiest ways to receive an error message.

A really common way of installing Python packages and other types of software from the internet is to use the command line. Sometimes when you do this, your computer may ask for permission to carry out a task or your anti-virus might want to scan the files before installing them. If the files that you’re trying to install are from a reputable website, this is totally fine. It just means your computer’s security system is working properly.

Pro tip: The CTRL/CMD key is your friend when entering commands in the command line interface as it can help you move around quickly when typing or navigating. For example, press CTRL/CMD + A to move to the beginning of the line. Press CTRL/CMD + E to move to the end of the line.

Finally, not all commands are finished by simply submitting them: some require further inputs from you after hitting return. In case you should ever be stuck in the middle of a command and want to abort it, hitting CTRL + C will cancel that command. While this is safe to do in most situations, please note that aborting a command can of course leave things in an unsteady state

Command line and Anaconda

Up until now we’ve been talking about your computer’s native command line – Command Prompt or Terminal, however, you can also use Anaconda’s package manager Conda with command line commands to do things like install Python packages and run Python scripts.

If you plan to use Anaconda for machine learning or data science (which you probably should) then this means you can install all Python packages in a single place (or environment), which won’t interact with other things on your computer.

Say, for instance, you use Anaconda to install Tensorflow, the popular machine learning library, then you could use Conda to install the necessary machine learning Python packages into the same place as Tensorflow. This means that a Tensorflow dependent algorithm is then able to find all packages that it needs to run properly in one place.

Learn more about Anaconda (including how to install it) here.

If you already have Anaconda installed here’s how to open Conda:

Windows
From the Start menu or Search Bar, search for and open “Anaconda Prompt”

Mac
Open Launchpad, then click the Terminal icon.

Pro tip: the command conda list is one of my most used Conda commands because it tells me exactly which packages are installed in my Conda environment.

Looking to learn more about Conda? Check out this useful Conda cheat sheet.

Further reading

The point of this guide is to introduce you to the basics of the command line so when you come across a task that requires you to use command line (which, when working with machine learning algorithms will be often) you’ll feel confident that you can

a) find the command line

b) know which parts of a command to enter into the command line interface and

c) move about in your computer’s directories. If you can do these things then odds are you’ll be able to complete 90% of command line related tasks.

Looking to learn more about machine learning? I got you. Check out this guide and then start with this algorithm.

RELATED POSTS