The Command Line Interface for Beginners

Tony Staunton
Python Code Academy
6 min readJan 19, 2022

If you want to grow as a Python developer, and learn to use other programming tools, then working with the command line interface (CLI) is a necessity. Unfortunately, beginners can find it difficult to get started with the CLI due to fact that you need to type commands rather than point and click. It can be intimidating at first, but a little guidance, and practice can go a long way.

In this article, we take a dive into what the CLI is all about, why it’s important, learn some key terminology, and play around with popular commands. By the end of this article, you’ll have a much better understanding (and less fear) of what the CLI is, and how to use it.

What is the Command Line Interface

Firstly, it has many names, “Command Line Interface” (CLI), or “Command Line”, “Command Prompt”, or on a Mac “the Terminal”. At a simple level it is a gateway to your computer without using the common point and click navigation of your mouse. You are basically navigating your computers files and folders, and performing certain tasks without moving or clicking the mouse. The CLI is a purely text-based application that can be used for various file handling processes such as accessing files, deleting files/folders, creating files/folders, viewing files in a folder and much more.

Why you need to Learn CLI

As a Pyton programmer, many of your tasks will be performed on the CLI. Git, a popular version control system, was designed primarily to run on the command line. When you run code, be it Python, Java, or C, the code can be run from the command line.

The CLI allows for the swift manipulation of files, and performance of certain tasks with simple commands. Indeed, many experienced programmers prefer to use the CLI as it offers quick access to their file system without having to remove their hands from the keyboard to operate a mouse. This may seem like a small increase in productivity, but you will find that your productivity actually increases with less mouse usage.

The CLI is also used to troubleshoot cloud systems and applications. Heroku, a cloud application platform, provides customers with a CLI, giving them more control over their hosted applications. By having access to a CLI, customers also get a more robust understanding of what’s going on with their applications, making it easier to detect and fix errors. Google, Amazon and Microsoft also provide command line tools to access several of their services.

Terminology

If you are going to be working regularly with the CLI then it’s important to understand some key terminology.

  1. Shell: The shell is simply the application that is used to reference the CLI on your computer. On Windows, the shell is called the command prompt. Whereas, on a mac, the shell is called the Terminal.
  2. Prompt: The prompt is an indication that you are free to enter a command. As the name suggests, the Prompt, prompts you to do something. It appears seen when you open up the shell, or just after you have run a command on the shell.
  3. On windows, you can identify a prompt with the > symbol. Mac operating systems use the dollar $ or percentage % symbols to signify a prompt.
  4. Command: A command is an action you want the CLI to take.

Opening the CLI on your machine

Depending on your operating system, there are slight differences as to how CLI commands are typed. If you are using Windows, do not expect all of the same command to work for a Mac, and vice versa.

Windows

Go to the search button on your computer, and type ‘Command Prompt’ or ‘cmd’. Click on the first application that is listed. When the Shell has opened you’ll see the prompt symbol > with a cursor signifying that the CLI is ready to take your command.

Mac

Go to applications. Then click on Utilities and find the Terminal application. When the application opens you’ll see the $ or % with a cursor signifying that you can enter your command.

Writing your First Command on the Shell

Once you have your CLI up and running, you can type a command. For your first command try typing ‘whoami’. After typing the command, press Enter. You should get the name of your computer returned. This command will work for both Windows and Mac operating systems.

Other Useful Commands in CLI

There are lots of commands that you can use when working with files. Below I’ve listed the ones that you’ll most frequently use as a developer.

Find your current working directory

This command will return the path to the directory that you are currently working in:

  • Windows: cd
  • Mac: pwd

Listing all the files in a directory

This command will list all sub-directories, and files in your current working directory:

  • Windows: dir
  • Mac: ls

Creating a new directory

This command will create a new folder in your current working directory:

  • Windows: mkdir
  • Mac: mkdir

The mkdir command allows you to add optional information called an argument. In this example, the argument would be the file path in which you want the folder to be created.

  • mkdir <path_of_directory>

If no argument is defined, the new file is created in the current working directory.

Change the working directory

This command is used to change the directory you are currently working in to another. Again, this command takes an argument that will tell your CLI what directory to move to:

  • Windows: cd <path_of_directory>
  • Mac: chdir<path_of_directory>

Pro tip: This is one prompt that you will find yourself using again and again.

Clearing your CLI screen

There will be times when you have run several commands on your CLI and you have no idea where you are. When this happens it is helpful to clear your screen and have a nice clean CLI to work on:

  • Windows: cls
  • Mac: clear

Delete a file or folder

When you need to delete a file or folder. Use this command:

  • Windows: del
  • Mac: rm

There are lots of other commands to learn. What I’ve listed above are just some of the most common. From your CLI type ‘help’, and you’ll see a list of other available commands to use.

Options — a way of customizing your commands

When you enter a command on your CLI, you can add more information to the command by entering an option. The option is typically placed right after the command or argument and is separated by a space. Options can also be called switches or flags.

An option often begins with a hyphen (-) or a double hyphen ( — — ). Here are some common options you can use.

  • -a: shows all the hidden files.
  • -C: list entries based on columns
  • -S: sort file based on size. From largest to smallest.
  • -l: shows who has permissions to the files.

One thing to note here is that you can use more than one option at a time. If you want to list all the files in a directory, you use the DIR (typed lowercase) command for windows. Then you also want to ensure hidden files are listed, you can add the -a option. Finally, you want to see who has permission to the files, so you use the -l option. The entire command would look like this:

  • Windows: dir -a -l
  • Mac: ls -a -l

Generally speaking, your command line should look like this if you entered the command, option and argument.

Source

Some additional tips

  • When you are typing an argument (could be the file name in a directory), you can type in the first few letters of the file name and hit the tab button. The CLI automatically completes the file name for you. This will speed up your work rate and help you avoid typographical errors with file names.
  • When you run a command that keeps on running, you can press CTRL+C to cancel the command.
  • When you want to retype a command that has been entered previously, use the up and down arrow of the keyboard to toggle between previously typed commands.

Summary

You’ve learned what the Command Line Interface (CLI) is, and the basics of working with it. Knowing how to navigate the CLI is an extremely important skill, and one that will stand to you as you become a professional Python developer. No matter what type of development you are doing, web, backend, data science, knowing the CLI will help to set you apart.

I hope you found this article useful.

Tony.

--

--