Command Options

In addition to wildcards, there is another special kind of argument that you can provide a command, called an option, that will allow you to tailor the functionality of that command. Options are always designated by a special character, followed without spaces by another character. Command Prompt commands typically use a / character to indicate options. Other programs, particularly those coming from a Unix heritage (like those you’d find normally on Linux) use - or --.

Using a Simple Option

One thing you may have noticed about the dir command is that is displays a lot of information about the files in the directory. What if you only wanted to see the list of files, with no further details? Well, you can specify the /B option to tell the command to do just that!

Generally speaking, the syntax for a command, including options, is as follows,

% cmd opts args

where multiple options (here called opts) and arguments (args) are listed separated by spaces following the command (cmd). So, if we continue the above photograph example, with our working directory set to C:\Users\doug\Documents\photographs, we can see a bare list of the filename contents of our November-2020 directory using,

% dir /B November-2020

or a bare list of the contents of our programming directory with,

% dir /B ..\programming

Be careful here with slashes. Options are specified using a forward slash / and parts of a pathname are separated with a backslash \. If you mix these up, you will see errors, or unexpected behavior.

Options with Arguments

To further convolute things, some options can accept arguments of their own. The argument to an option will directly follow the option itself in the command’s argument list, and will be separated by spaces like normal.

A very useful example of this is using the dir command to search for the location of a file by name. This can be accomplished using the following command,

% dir /S filename

Here, filename is an argument to the /S argument, and represents the thing to search for, not the directory to run dir on, as we are used to. As another note, this command will only search the working directory, and any sub-directories of it. So if you want to search your entire C: drive, say, you need to set C:\ as your working directory prior to running the command.

As an example, let’s say we wanted to find where the file notepad.exe is located on our computer. We can locate it by using the following commands,

% cd C:\
% dir /S notepad.exe

This command will take some time to execute, and will find all instances of the specified filename on your C: drive. So it will likely return many results. As always, once you find the version you care about, you can terminate the command with ^C. Here I terminated it after the first hit,

Conveniently, this option supports wildcards. So you can search for all files with names matching a certain criteria. Here, for example, I search my home directory for all files with “photo” in the name,

% cd C:\Users\doug
% dir /S *photo*

There are a lot of hits; only the first few are shown. All the photo files from the above wildcard example show up at the bottom of the list.

Getting Help

Each command supports a number of options to modify its functionality. But, where can you find out this information? Technically, you could always search for the information on the Internet, but there is a better way. Each command has documentation of its functionality baked in. You can access this documentation in one of two ways, either through the help command, or through an option on the command itself.

For example, if we wanted to see a list of the supported options for the move command, we can try

% help move

If help doesn’t work for a particular command, try invoking the command with the /? option, like

% dir /?

and if that still doesn’t work, then you can try the Unix style option for help, which is

% command -h

Decoding the Help Text

The help text can be a bit esoteric until you get used to reading it. Let’s decode the synopsis of move, which is,

MOVE [/Y | /-Y] [drive:][path]filename1[,...] destination

With the exception of options (which we know are preceded by /), any text appearing here should not be entered literally. It is a description of what should go there. Text contained in square brackets [] is optional, and can be left off. So, skipping the [/Y | /-Y] for the moment, the next bit of the synopsis is a schematic representation of a pathname. Notice that the drive letter and path are optional, but the filename is not. The [,...] bit indicates that you can specify multiple filenames to move. And finally destination is not in brackets, and so is required.

The first part of this synopsis includes another important aspect of this shorthand description: [/Y | /-Y]. We can see the square brackets, indicating that this bit is optional, and we recognize /Y and /-Y as options. What about the | character (often called a “pipe”)? This character indicates that the two options listed within the square brackets are mutually exclusive: you can only use one or the other. So it would be invalid to attempt to use both at once, like

% move /Y /-Y file1 file2 file3 destination

Conclusion

In this article, we saw how we can extend the utility of commands through the use of options, and we learned how to get information about a command through either help, or the /? option.

In the next article, we will discuss the concept of a variable–which can be used to configure the shell, or to have it remember important information.

Previous
Next