Basic Directory Commands

This article has an accompanying YouTube video, here.

In the last article, we saw a variety of programs that we can use to access and manipulate text data within files. However, we did leave out one significant piece of the puzzle. To demonstrate this, let’s show an example of how a simple application of that which we’ve already discussed goes wrong.

Absolute Pathnames

First things first, let’s create a text file to manipulate. But, let’s specifically save this file to our documents folder. To accomplish this, launch notepad with no arguments, and then do file->save as, and specify your documents folder, as shown here,

Now, from the command line, try using type to see the contents of the file. You should see the following,

So, what gives? We definitely have created a file with this name. If you open file explorer and navigate to Documents, you can see it staring right at you.

The fact of the matter is, it is not actually enough to specify the name of a file when you try to access it. You also need to tell your computer exactly where that file is located within your directory structure, starting from the bottom.

If we instead try to type our file using the following,

% type C:\Users\doug\Documents\example.txt

you will see that it all works again. What we typed here is called an absolute pathname. example.txt is the filename, and C:\Users\doug\Documents\ is the path to the file.

Relative Pathnames

But wait, you may well ask, how did we manage to run all of those commands in the last part without specifying an absolute pathname? And, beyond this, typing out these long pathnames for every file will quickly become a pain.

In truth, the last article contained a convenient lie of omission. While it looked like we were typing in naked filenames, the shell was actually using pathnames, just like we did manually above. It’s just that these were relative pathnames: the shell filled in the first bit of the pathname automatically given the context in which the command was executed.

The Working Directory

Every program running on your computer has a special property called a working directory. This is most easily seen by using file explorer. When you launch file explorer and open up a folder, the working directory is prominently displayed in the address bar, indicated in the following screenshot. The pane below this displays the contents of the working directory. If you type a different directory into the address bar, you can change the working directory, and thus the contents of the panel.

Returning to the Command Prompt, we also have a working directory. By default, it is displayed as part of the prompt. It is C:\Users\doug in all of my screenshots so far. This particular directory has a special name, it is my user’s home directory.

We can specify the location of a file relative to the working directory by using a .. So, as an example, my file.txt from the last article can be accessed using,

% type ".\my file.txt"

where . represents my working directory of C:\Users\doug and the \ is the character used by Command Prompt to separate components in a pathname. If we simply take my working directory and paste it into that command in place of the ., we get an absolute path of,

% type "C:\Users\doug\my file.txt"

The form containing the . above is called a relative path, as in a path to the file relative to my working directory.

With this in mind, we can access the file in our Documents folder above like so,

% type .\Documents\example.txt

which is a little more convenient than the first example, using an absolute path.

Accessing files in this manner is so common, that most shells have a built-in assumption that any straight filename is contained within the working directory, and so they are treated as relative paths. For example, the command

% type "my file.txt"

will automatically be converted to

% type ".\my file.txt"

and then to

% type "C:\Users\doug\my file.txt"

and so it is rarely necessary to type .\. Note as well that if a component of a pathname contains a space, the entire pathname should be put in quotation marks, not just the part containing a space.

Displaying the Working Directory

Technically, the working directory is always on display as part of the prompt, at least by default. However, it is possible to change this prompt to contain different information. If you were to do this, you’d lose access to this means of seeing what the working directory is. So, what we really want is a command that will write the value of the working directory to our output buffer for us.

On Command Prompt, the command to do this is cd, with no arguments. cd is a very useful command that does a few things, as we’ll see in the next section.1

The following command will display your working directory,

% cd

As you can see, it should match the prompt.

Changing the Working Directory

If you are going to be doing a lot of work in a different directory, it is convenient to switch your working directory. This will save a lot of typing.

The cd command can be used to do this. If you provide cd with the pathname of a directory, it will switch your working directory to that one. For example, if you wanted to switch your working directory to your Documents folder, any of the following three commands would do the job,

% cd Documents
% cd .\Documents
% cd C:\Users\doug\Documents

Can you see why all three of these commands are equivalent?

Going “backwards”

Okay, so let’s imagine the following situation. You are working on a terminal with the working directory set to C:\Users\doug\Documents and you decide that you would like to switch your working directory back to C:\Users\doug. It’s been my experience that the first thing a novice will attempt is,

% cd doug
No such file or directory.

Which makes a lot of sense. doug is the directory I’d like to be in, after all! Unfortunately, this doesn’t work. If this was your first thought, don’t feel badly! In my experience, this is the first thought of at least 85% of my students.

To explain why, we need only apply the concepts discussed above. The argument does not begin with a drive letter, and so it is interpreted as a relative pathname, or .\doug, which we can expand to C:\Users\doug\Documents\doug given our working directory.

So, how do we actually do it? Well, you technically already know. You should stop reading now and see if you can figure it out on your own. All the tools you need are above this point in the article.

The answer is to use an absolute pathname. So, the command to do what you want is,

% cd C:\Users\doug
% cd
C:\Users\doug
Given an absolute pathname, you can access any file or directory from any command anywhere on the system (assuming your user has access to it). This is really powerful, as we’ll see time and time again through this series.

Of course, there is a convenient shortcut, again. Going backwards is something that you will want to do quite often, after all. The trick is to use .., like this,

% cd 
C:\Users\doug\Documents
% cd ..
% cd
C:\Users\doug

The way that this works is that .. is a shorthand for the working directory’s parent directory. A parent directory is the directory containing another directory. So, for example, if your working directory is C:\Users\doug\Documents\writing, then the parent directory would be C:\Users\doug\Documents. You can find it out pretty easily by just dropping the last bit of the pathname.

You can actually chain multiple ..’s together to move back multiple levels. For example, if the working directory is C:\Users\doug\Documents\writing then we have the following,

Relative Pathname Absolute Pathname
.\ C:\Users\doug\Documents\writing
..\ C:\Users\doug\Documents
..\..\ C:\Users\doug
..\..\..\ C:\Users
..\..\..\..\ C:\
..\..\..\..\..\..\ C:\

Once you reach the root, which is the last directory in the chain, you can go back no further. So any extra .. you add will have no effect, as shown by the last entry in the table.

Listing a Directory’s Contents

Now that we know how to specify files by pathname, be it absolute or relative to our working directory, and how to change our working directory, it would be helpful if we had some way to know what files were located in what directories on the system. There is, as I’m sure you’ve come to expect by now, a command for this.

The command dir is used to provide a listing of the files and directories contained within a specified directory. It can be used in one of two ways. If it is called with no arguments, it will provide a listing of the contents of the working directory, like so:

% dir

Alternatively, it can be provided with the pathname of a directory to list the contents of that directory.

% dir Documents
...

This command is quite useful for locating files, or at the very least for checking whether or not a file is present in a particular spot.

A Word on Pathnames

One of the trickiest things to master about the command line is knowing how to refer to a particular file by pathname. The important thing to remember is that any command which accepts a pathname will work equally well with either an absolute one, or a relative one. And that most any command which accepts a file or directory name as an argument, is actually taking a pathname. This includes any pathnames involving ...

This can lead to some pretty useful combinations. As an example, imagine that your working directory is currently C:\Users\doug\Documents\programming, and that you are working on a file in this directory called .\example.lua. If you wanted to take the current version of this file, and create a backup copy in C:\Users\doug\Documents\backups, you can do this with a single command without changing working directory,

% copy .\example.lua ..\backups

Or, if you are working in that same directory, and you just downloaded an example program in your Web browser called test-code.r. If you wanted to move that test code into your working directory, you can do that with the following command,

% move C:\Users\doug\Downloads\test-code.r .

or, equivalently,

% move ..\..\Downloads\test-code.r .
The move and copy commands are fairly intelligent with their destination arguments. If the destination is the pathname of an existing directory, they will place the copied/moved file into that directory, keeping the filename the same. If it is not a pathname of an existing directory, they will use the destination argument as the pathname of the moved/copied file instead.

Creating and Deleting Directories

Finally, you can actually create and remove directories directly from the command line with the commands mkdir and rmdir. mkdir accepts as an argument a pathname and, if no file or directory with the pathname exists, will create a directory with that pathname.

% mkdir new_directory

rmdir does the reverse, it accepts the pathname of an existing directory, and removes it. Note that the directory must be empty first. You can clear it out manually by using del, and then use rmdir to remove the directory itself.

% rmdir new_directory

Conclusion

In this article, we discussed the concepts of relative and absolute pathnames. These are used to specify a particular file using not just its name, but also the sequence of directories it is contained within. Relative pathnames are stated from the working directory of the shell, which can be set and displayed using the cd command. Absolute pathnames are stated from the relevant drive letter (C:\ in most cases on Windows).

We also saw the dir command, which can be used to view a listing of the contents of a directory, and the mkdir and rmdir commands for creating and deleting directories.

In the next article, we will see how we can use special arguments, called options, to expand the functionality of the commands that we have seen up to now. We will also see how we can run commands against many files at once, using globbing.


  1. Note that cd works a bit differently in Bash and its derivatives. On these shells, it lacks the working directory writing functionality that it has on Command Prompt. Instead, the pwd command is used for this purpose, and cd is strictly used for changing the working directory. If cd is called with no arguments in Bash, rather than displaying the working directory, it will change the working directory to be the user’s home directory. ↩︎

Previous
Next