Running Commands
This article has an accompanying YouTube video, here.
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.
The Structure of a Command
All commands on a command line have a similar basic structure. They begin with the name of a command, followed by a space-separated list of text inputs to that command, called arguments.
On Command Prompt, command names are not case sensitive. copy
, COPY
, and
CoPy
will all work just fine and do the same thing. The names of files, for
the purposes of arguments, are also not typically case sensitive, although this
can depend on the command you are trying to run. This case insensitivity is a
fairly unique quirk of Command Prompt, though, so don’t get in the habit of
relying on it. You should behave as though it is case sensitive, as any other
shell that you might eventually migrate to likely will be.
Commands
A command can be either an internal command, which is built into the shell itself, or a program on the computer (a .exe file on Windows). For our purposes, there isn’t a real benefit to distinguishing between the two, and so both will be called either “commands” or “programs” interchangeably.
The cls Command
A command is run by simply typing it into the terminal’s input buffer, and
striking the ENTER key. As an example, the cls
command is used to clear the
screen. It empties the terminal’s output buffer, and then has the shell rewrite
the prompt. It can be run by typing,
% cls
The above format will be used for stating commands moving forward (in lieu of
screenshots, except where a screenshot is useful). The %
character is a
stand-in for the prompt, and should not be typed. It simply indicates that the
text that follows should be entered into your terminal while a prompt is
present. So, you should see this prior to striking enter (on a fresh terminal),
and then this after striking ENTER and executing the command,
Note that cls
will empty the output buffer, but will not affect command
history. So you can still use the UP and DOWN arrow keys to traverse the record
of executed commands on the terminal, even after running cls
.
Command Not Found
If you enter a command which the shell cannot find, you will get the error that we saw in the last article,
'__' is not recognized as an internal or external command, operable program or batch file.
This can happen if you have not set up the program in question to be runnable from the command line (which we will discuss how to do in the section on the PATH), if you mistype the command, or if you try to run a program that you haven’t installed at all.
Arguments
In addition to the name of the command, most commands will need arguments. Arguments are a simple way of providing the command with input data, often in the form of the name of a file. If you want to make a copy of a file, for example, you will need to specify as arguments the name of the file to be copied, and the location to copy it to.
Arguments are separated using spaces. In keeping with the copy example, the
following is the command that will create a copy of the file myfile.txt
and
name that copy copyfile.txt
.
% copy myfile.txt copyfile.txt
To repeat for the purposes of gaining comfort with the notation, the above command should be typed like this,
Spaces within Arguments
What about files which contain spaces in their names? Windows allows this, but
clearly these will break the copy command. If I have a file called my file.txt
that I want to copy, the following command is not going to work,
% copy my file.txt copyfile.txt
because the copy command will see three arguments, my
, file.txt
, and copyfile.txt
.
Some commands on Command Prompt are reasonably intelligent with figuring out
spaces in filenames, but in general they will cause problems. The workaround is
to use quotation marks. The shell will ignore spaces within quotation marks, and
everything contained within a set of them will be treated as a single argument.
Thus, the command we want is,
% copy "my file.txt" copyfile.txt
One word arguments can still be surrounded by quotation marks if you’d like, it won’t hurt anything, even if it isn’t strictly necessary.
Creating and Editing a File
As a starting point, let’s use the shell to launch the notepad text editor and create a text file. As discussed above, a command can simply be the name of a program. Thus, we can launch notepad using
% notepad.exe
which will launch notepad with an untitled, blank document open.
As it turns out, you don’t need to type the extension on executable files. So it is possible to launch notepad using the following command instead,
% notepad
which is a bit easier to type.
Notepad will actually allow you to specify a filename as an argument. If the file doesn’t exist, it will create it for you. If it does exist, it will open it up. Let’s experiment with this functionality now.
Execute the following command,
% notepad myfile.txt
Assuming that you don’t already have a file called myfile.txt, you should see the following,
Upon clicking “YES”, the title bar of notepad should change to contain the name
myfile.txt
. At this point, you have an empty, named file open. Type some
random text into the document, and save it (tip: CTRL-S is the keyboard
shortcut for saving, in most applications). Then close notepad by clicking the
X (tip: ALT-F4 is the keyboard shortcut for closing a window; just make sure
you have the right one selected, first!).
Now, run the same notepad command again. You can press the UP arrow key to recall it. This time, you won’t see the pop up window asking to create a new file. Instead, you’ll see the file contents you just typed.
Be very careful. When you open up a file in notepad from the shell, you will get whatever contents that file had as of the last time you saved it. If you edit the file in a notepad window, and then open it up a second time in another notepad window before you’ve saved your changes, the second window will contain the contents, without any of your changes. Additionally, notepad won’t update the text it has open if changes to the file are saved from another notepad instance. This bit is a little tricky to explain with text alone, so please refer to the video accompaniment to this article for a demonstration.
Viewing the Contents of a File
You can readily view the contents of a file, without opening it up in notepad,
using a different command: type
. The type
command will accept as an argument
the name of a file, and will write the contents of that file to your terminal’s
output buffer directly. It is a “read-only” mode of access–you cannot change
the file’s contents, only view it. But it is convenient if you want to quickly
check out the contents of a file. You can view the contents of the file you
just created using type
with the following command,
% type myfile.txt
which should look something like this,
If you pass type
the name of a file that doesn’t exist as an argument, you’ll
get the following,
Renaming a File
The move
command can be used to change the name of a file. It “moves” the
file from its old name, to a new one. As you might expect, this command
requires a pair of arguments. The first is the current name of the file to be
renamed, and must be a file that exists. The second is the new name you’d like
the file to have. For example, we could rename our file to have a space in its
name (recalling the earlier discussion of quotation marks), using the following
command,
% move myfile.txt "my file.txt"
and then we can verify the name change by using the type
command on the old
and new names,
As you can see, after executing the move
command, the first call to type
on
the old name shows that the file no longer exists, and the second call with the
new name displays the contents of the file.
Copying a File
Copying a file will allow you to create a duplicate of an existing file. It will be a different file, but at the moment it is created will have identical contents to the file that was copied. From then on, you can change each file independently. Copies are very useful for backing up important information or for keeping a historical record of a file.
To create a copy, you can use the copy
program. It accepts two arguments,
the first is the name of the file to copy, and the second is the name to call
the copy. Let’s create a copy of the renamed file from above, which can be done
like so:
% copy "my file.txt" acopy.txt
Using type
, you can verify that both files exist, and have the same contents.
Comparing Two Files
While we’re here, let’s use another program to verify that the copy we made above
is identical to the original. The shell has a command called fc
, short for
“file compare”, which we can use to check files for differences. This program
takes as arguments the names of the files to compare, both of which must exist.
Running it on the two files, like so,
% fc "my file.txt" acopy.txt
produces the following output, indicating that the files are, as expected, the same.
However, if I edit one of the files a little in notepad and then save the
changes, fc
will notice and let me know what is different.
Deleting a File
Files can also be deleted from the command line. With that said, you should be careful with this. Files deleted from the command line will not be sent to the Recycling Bin, they will be actually deleted. So there isn’t any easy way to go back. For this reason, I generally advise against deleting files from the command line at the start, until you get comfortable with it. With that said, I’ll show you how anyway. It’s pretty simple.
The del
command is used to delete files. You can provide it with the name(s)
of the file(s) you want to delete as arguments, and it will delete them. Let’s
delete our copy from above.
% del acopy.txt
We can verify that it is actually gone using type
,
Conclusion
In this article we learned the basic structure of a command, how to provide
arguments to a command, and a variety of simple commands for working with text
files: notepad
, type
, move
, copy
, fc
, and del
. Of course, we
haven’t really gotten into how we locate and organize files, and it won’t take
long to realize that there is a very significant missing piece to the puzzle
here. Thus, in the next article, we will discuss directories and how we can
specify the location of a file on the system.