Command Options and Wildcards

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. We also discuss wildcards, a tool for quickly specifying multiple files to use an input to a command.

A Brief Introduction to 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. This allows you to set up a general template for the “type” of filename that you want to use as input (or sometimes output) to your command, and then apply the command to all files that match that template in one go.

Unfortunately, the wildcard capabilities of Command Prompt are rather… deficient… compared to other options. On Command Prompt, it is up to each individual command to take care of wildcards, rather than the shell taking care of it automatically (as is the case in Bash and PowerShell). However, the basic use cases for wildcards are, happily, accounted for by most of the Command Prompt commands that can take advantage of them, and so Command Prompt will be sufficient to introduce the concept. Know that what we discuss in this section is a mere taste of what is possible.

Matching all Files with a Certain Extension

Let’s consider the following situation. We have a directory that contains a variety of files of a few different types, such as the following,

We may want to create individual folders for each type of file, and move the appropriate files into the appropriate folder. Obviously, in this case, there aren’t that many files to consider. But you might well imagine a situation where we have many dozens, or even hundreds, of files that we’d like to relocate. The same principles will apply.

Let’s start by simply creating the directories we’d like to organize our files into. We’ll make one to store the C code (.c and .h), one for R code (.r), and let’s combine Python (.py) and Lua (.lua) into a directory for scripts. We can make all these new directories with one call to mkdir, like so,

Now, we can use the move command to relocate our files to the appropriate place. However, notice that we have two different .lua files. This means that we would, presumably, need to use two different commands to accomplish this task,

% move reallycomplexscript.lua scripts
% move helloworld.lua scripts

Which already seems like a lot of work–to say nothing of a more complex case having several dozen files.

This is where wildcards comes to the rescue. Rather than specifying each file individually, we can refer to them all in one go. In effect, we can issue a command that says “move everything with a .lua extension into the lua folder”. The command is,

% move *.lua scripts

The * in the command is called a wildcard. What this command says is to take all of the files that end in .lua, no matter what the rest of the filename is, and do something with them. As shown here, it has the desired effect,

Wildcards in both Source and Destination Arguments

Some commands will support wildcards in both the source and destination arguments. For example, what if we suddenly realized that we had made a mistake, and that our supposed Lua scripts were actually written in Perl, and thus ought to have a .pl extension, rather than a .lua extension? After firing the intern who no doubt made the error, how can we fix it?

Move won’t work here, unfortunately. Attempting to specify a wildcard in both source and destination arguments results in an error,

But, Command Prompt actually has a purpose built rename command, that can be used for bulk renaming of files like this. Note that rename does require the files to be in the working directory, for some reason, in order to work.

% cd scripts
% rename *.lua *.pl

Specifying All Files in a Directory

You can also use a wildcard to specify all files within a given directory. For example, if we decided that we wanted to pull those newly renamed Perl scripts back into our programming directory, we can do the following (with our working directory set back to the programming directory)

% move scripts\* .

Multiple Wildcards in a Single Argument

You can also include multiple wildcards in a single argument. For example, a common use case of this is manipulating photographs, which often contain a date in the filename. Consider the following directory of jpeg images,

We can move all of the photographs taken in November of 2020 into the appropriate directory with a single move command,

% move *_11-*-20* November-2020

It is worth taking some care with wildcards. It’s very easy to make a mistake with them. For example, the following move command may appear very similar to the one above,

% move _*11-*20* November-2020

But, it contains a bug. Can you see what that bug might be?

Answer

The second wildcard will match all of the photographs taken in November 2020, however it will also match photographs taken on the 20th of November, regardless of the year, such as photo_11-20-19.jpg.

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 -.

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 mutiple options (here called opts) and arguments (args) are listed seperated 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 seperated 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 argument list, and is seperated 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 preceeded 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.

Now, 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 discussed using wildcards to allow commands to process multiple files at once. Then, we saw how we can extend the utility of commands through the use of options. Finally, 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 an environment variable–which can be used to configure the shell, or to have it remember important information.

Previous
Next