Introduction to Command Line
These articles have accompanying YouTube videos, here.
Most of the average computer user’s interactions with their system takes place within the confines of a graphical user interface (GUI). This is the familiar landscape of windows, buttons, menus, and textboxes, that most of us inhabit the majority of the time that we are using a computer. However, there are certain tasks that either cannot be done in this environment, or that can be done more efficiently on the command line.
Although you can function on a computer without it, the command line provides a more efficient mechanism for completing a lot of common tasks. While this is especially true for more technical tasks like programming or systems administration, the command line can be a useful tool even if your use case is a little more “normal”. With a bit of know-how, even tasks as mundane as creating backups, extracting zip archives, or downloading files from the Internet can be performed quite quickly and easily using a command or two.
This short tutorial will introduce you to the core concepts of working in a command line. It is my hope that, irrespective of your particular use case for a computer, you will be able to extract some value from this exercise.
Because the majority of desktop computers run Microsoft Windows, this series uses Command Prompt. This is to reduce the barrier to entry, as anyone with a Windows computer can open the program and follow along with no additional setup whatsoever. The majority of the concepts discussed are universal, and so should be directly transferable to more sophisticated command line environments such as bash or PowerShell.
If you’re running Linux and want to follow along in Command Prompt, you can do so using wine. Install the wine package for your distribution, and then use the,
$ wine cmd
command from a terminal to enter a Command Prompt shell.
I’m not aware of a way to get Command Prompt running on MacOS, so if you use a Mac and don’t have access to a Windows or Linux machine, this may not be the best tutorial for you.
Chapters
-
The Command Prompt
In order to discuss the command line, we are going to need a command line environment to use. This article introduces core concepts and vocabulary pertaining to the Command Prompt. If you are a detail oriented person, you may be best served skimming this article now, and then returning to it later for more detailed reading once you have a little more context.
-
Running Commands
Now that we know a little bit about how the command line works, let’s learn how to use it to complete a variety of simple tasks. A common starting point is to use the command line for interacting with text files, and so that is what we will do next. In this article, we’ll discuss the basic structure of a command, and then see commands for creating, editing, viewing, renaming, copying, comparing, and deleting files.
-
Directories and Pathnames
Our discussion of file commands was missing a very important aspect of the command line: our system doesn’t just include every single file thrown together in one directory! We must next learn how to deal with pathnames and directories.
-
Wildcards
Frequently, we will want to apply the same operation to multiple files. For example, we may want to move all files of a certain extension to a different folder. Rather than applying these operations one-at-a-time, most shells allow you to use a tool called a wildcard to specify multiple files at once.
-
Command Options
In addition to accepting filename arguments, many commands will accept additional special arguments, called options, which can control their behavior. In this article, we discuss the concept of an option, various conventional syntaxes for specifying them, and how to access documentation on what options are available for a given command.
-
Shell Variables
It is not uncommon to have a particular value that you’d like to use repeatedly during a shell session. Rather than constantly retyping this value, Command Prompt supports the creation of variables, which can be used to store information for the duration of a given session, or even to do simple arithmetic directly in the shell.
-
Environment Variables
Every process running on your computer has a set of variables associated with it, called an environment, which it inherited from the process that spawned it. These so-called environment variables can be used to both pass information between programs, and to configure their behavior. We’ll take a look at a few notable ones here.
-
Input/Output Streams and Redirection
Up until now, all of the commands that we’ve used have accepted input directly from you via command-line arguments. While this is sufficient for many tasks, it does greatly limit the amount of larger-scale data processing that you can do.
-
Filters and Pipelining (coming soon!)
There are a wide variety of commands which will read a file from standard input, perform some processing or manipulation of that file, and then write the results to standard output.
-
Command Reference Guide
This document lists the Command Prompt commands discussed in these articles, along with their arguments and what they do. Arguments contained in square brackets are optional. Several of these commands accept more than three optional arguments, but only up to three are listed.