Skip to main content

Tutorial: Introduction to the Command Line

Site: OpenCourseWare for GIS
Course: Programming for Geospatial Hydrological Applications
Book: Tutorial: Introduction to the Command Line
Printed by: Guest user
Date: Tuesday, 21 May 2024, 9:54 AM

1. Introduction

Before we can do any scripting you have to learn how to use the Command Line and how files and directories (folders) are organised on your computer. Therefore today we'll start with a tutorial on using the Command Line.

After the Command Line tutorial you'll be able to:
  1. Use the command prompt and the most common commands
  2. Navigate to files and directories and use their relative and absolute paths
  3. Make batch files

During the excersises we are going to use the command prompt.

Note that this course is designed for Windows users. On Mac and Linux there is a different command line interface and you have to use other commands that are not covered in this course.

The preferred way to learn the command is to use this online DOS emulator, so nothing can go wrong on your own system. The exercises are based on this online DOS emulator. The amount of concurrent users of the emulator is limited, so if you can't access it, please try again later.

When you get more familiar with DOS you can use the regular command prompt or the OSGeo4W Shell.
The OSGeo4W Shell comes with QGIS and has everything set for GDAL which we'll use in another module.

2. Viewing the contents of a directory

Start the online DOS emulator if you haven't done yet.

C:\> is called the command prompt. It shows your current working directory (on Windows this is called a folder). In this case it means that you are at drive C (which is called the root). You cannot delete or edit the command prompt. You'll have to type the commands after the > of the command prompt.

1. Type the following command after the prompt:
After typing a command press the <ENTER> key to execute the command.

In the instructions we use <> to indicate that you have to press a key.

In this case we activated the logging of the command history that we'll use and explain further in Chapter 6.

We will view the contents of a directory by using the dir command. The dir command stands for "directory".

2. Type the following command after the prompt:

dir <ENTER>

You will see this:

This is called a directory list. A directory list is a list of all the files and subdirectories that a directory contains. If you type dir /? you will see all options of the dir command. For example, if you type dir /w you will see the directory listing in columns.

3. Try these commands to see what is on the C drive.

3. Drives and Directories

In this chapter you'll learn:
  • how to change drives
  • how to create directories

3.1. Changing Drives

If you have different hard drives or partitions, you may need to change to another drive. If you are on drive C and you want to move to drive D you can type:


at the prompt and press <ENTER>. You will now continue on drive D.  If you have another drive, it is recommended not to use drive C, because it contains your system files. In these exercises in the online DOS emulator, however, we'll use the C-drive only, it doesn't have other drives.

3.2. Creating Directories

1. Type dir and press <ENTER> again at the command prompt to get the directory listing.

Look at the list on your screen. All the names that have <DIR> beside them are directories.  The others are files. The size of the files is also displayed. You can see a list of the files in another directory by changing to that directory, and then using the dir command again.

2. Type the following commands:
md John <ENTER>
md Peter <ENTER>
These commands create subdirectories called John and Peter. Check if the directory has been created by listing the directories:

3. Type dir <ENTER>

You can see that the two subdirectories have been created on the C-Drive.

4. Type dir /AD <ENTER>
This will list only the directories and not the files:

3.3. Change directory

1. Now go to the newly created directory "John" by typing:
cd John <Enter>

cd means "change directory". You can see that the command prompt has changed to C:\JOHN

Note that DOS is not case sensitive (it interprets upper and lowercase in a similar way), so you could also have typed cd john.

2. List the contents of the directory John (use the command that you've learned before).

Relative paths

Now you'll see that the directory is not empty. There are two directories, namely . and ..

Actually, the directory is empty, but it also shows the relative paths:

 . means "current directory"

.. means "mother directory (or one directory up)"

3. Type
cd .. <ENTER>

4. Check the command prompt.

We're back in the directory (root of the C-Drive) where we started the exercise.

5. Now type :
cd . <ENTER>

6. What is the result of this command? Explain why.

7. We can go back to directory John using the relative path, type
cd .\John <ENTER>

Note that this is the same as cd John <ENTER> .

8. Go one directory up to go back, type
cd .. <ENTER>

Absolute paths

We can also use absolute paths to navigate on our drive. Absolute paths always start from the root of a drive, in our case C:\.

9.  Type
cd C:\John <ENTER>

10. If we want to go to the root (drive C) we can do it using the absolute path, type
cd \ <ENTER>

Why bother about absolute and relative paths?

11. Go back to the directory called John. You can choose if you want to use the relative or absolute path

12. Type
cd ..\Peter <ENTER>

13. Look at the prompt and explain what this command did.

Both ways of changing directories are equivalent. Why bother than? Well, if you move your files that are organized in directories and subdirecties to another drive (e.g. drive D), than your absolute paths will not work, while your relative paths will still refer to the same locations. This is specifically important when writing scripts, which we'll do later.

3.4. Delete directories

Now we're going to delete the directory called Peter.

1. First move one directory up, so you're back in the root of the C-Drive. Type:
cd .. <ENTER>

2. Remove the directory Peter by typing:
rd Peter <ENTER>

rd means "remove directory".

3. List the directory to check if Peter has been removed.

Note that you can only remove a directory in this way if it is empty!

4. Creating and viewing files

In this chapter you'll learn:

  • How to create text files
  • How to create batch files
  • How to visualise the contents of files
  • How to append files

4.1. Creating files

Of course you can create files using your Windows software, but since you're learning the command prompt you'll learn how to create a simple ASCII text file.

1. Type:
copy con listdir.bat <ENTER>

dir /AD <ENTER>


(keep the control button pressed and type z)

Your screen looks like this:

2. Check if the file is in the directory list. What is the size of the file?

4.2. Display contents of files

Now display the contents of the file on the screen.

1. Type:
type listdir.bat <ENTER>

With the type command you can view the contents of a file.

2. Now type:
listdir <ENTER>

Congratulations! You have created your first script, a batch file. Batch files are scripts that can be used to execute commands in batch. In this case the file executed dir /AD.
Because batch files always have the file extension .bat, the computer knows that this is a batch file and will execute the commands in the file. We'll come back to this later.

3. Let's create another file. Type:
dir > list.txt <ENTER>

This command will not show the result of the dir command on the screen, but saves it to an ASCII file (text file) called list.txt. So we can use > after a command to save its results to a file instead of printing it to the screen.

4. Check the contents of the file:
type list.txt <ENTER>

You can see that the list is larger than your screen.

5. Type:
type list.txt | more <ENTER>

The result of list.txt is given to the more command which displays the results in pages as big as your window. Press   <ENTER> to see the next line. Press <SPACE BAR> to see the next page. Press <CTRL-C> to stop. You can use this last key combination to stop any command if it isn't doing what you like, it crashed or it takes too long.

6. Type:
more <ENTER>

Nothing happens, because the more command expects input from another command. So you can wait forever. In this case you can stop the execution of the more command by using <CTRL-C>.

7. Now try this:
type listdir.bat >> list.txt <ENTER>

8. Display the result:
type list.txt | more <ENTER>

9. What happened?

In summary:

  • > saves the result of a command to a new file. If the file on the right hand of > already exists, it will be overwritten.
  • >> appends the result of a command to an existing ASCII file.
  • | uses the result of the command on the left hand side in the command on the right hand side of the |. This is called a pipe.
  • Use <CTRL-C> to stop the execution of a command.
We can also use these operators to create so called lock files. These are used in scripting. The script will check if a certain file exists. If it exists, the program will wait, if it is removed, the program continues. Therefore, these files can be empty. You can create a lockfile with: type NUL > lockfile.txt <ENTER>

5. Copy, move, rename & delete files

In this chapter you'll learn how to:

  • Copy files
  • Move files
  • Rename files
  • Delete files

5.1. Copy files

1. Now we can copy the file list.txt to a new file by typing:
copy list.txt newlist.txt <ENTER>

2. Check if the file is added to the directory

3. We can append text files with the copy command. Type:
copy list.txt+newlist.txt listappend.txt <ENTER>

4. Check the contents of the file listappend.txt.

5. We can also copy list.txt to subdirectory John:
copy list.txt john\newlist2.txt <ENTER>

6. Go to the subdirectory John and check the contents of the subdirectory.

5.2. Move and rename files

Now we're going to move newlist.txt to the directory John.

1. First go back to the root of the C-drive.

2. Type:
move newlist.txt John <ENTER>

3. Go to the subdirectory John and check the directory listing.

4. Because it is a bit confusing to have a copy of newlist.txt in subdirectory John, we're going to rename it:
rename newlist.txt listjohn.txt <ENTER>

5. Check the directory listing again.

6. Save the directory listing to a file called dirjohn.lst

5.3. Use wildcards

1. Type:
dir <ENTER>

2. Type the commands below and check the results:
dir *.txt <ENTER>
dir *john.* <ENTER>
dir *.??t <ENTER>
dir *.?xt <ENTER>

3. Explain the * and ? wildcards

5.4. Delete files

Because we made copies, we want to remove duplicate file.

1. We'll remove newlist2.txt:
del newlist2.txt <ENTER>

2. You can remove now all files by typing (Make sure you are in the right folder, there's no undo in the command line!):
del *.* <ENTER>

(or simply: del . <ENTER>)

3. Now remove the directory John (remember how to do that?).

6. Command line history

Sometimes you often use the same command. There are several tricks to type them more efficiently.

In your Windows Command Prompt Doskey is by default activated. Doskey is the tool that tracks the command line history. The emulator that we use has some limitations and some of the command below might not work. Remember that we had activated it in Chapter 2 of this tutorial.

Let's test if it remembered the commands we type from now.

1. Type the <F3> button.

This repeats the last command used.

2. Clear the command prompt and press the right arrow button several times. You can see that the characters of the previous command are repeated. If you press the up and down buttons, you can browse through the previously used command.

When you use <TAB> while typing a path or a filename, it will try to automatically complete it. This is very useful, because you avoid typos. In the emulator this doesn't work, but in your own Command Prompt you can use it, so don't type the complete file and directory names, but use tab completion.

With the doskey command we can do even more.

3. Type:
doskey /h <ENTER>

This prints all the commands you typed during this session to the screen.

If you close the command prompt, the command history will be lost.

4. Save the command history to a text file using one of the commands previously learned.

In this way you can edit the command history file in e.g. notepad. If you remove all the wrong commands and you save the file with the .bat extension, you can execute all of the commands in batch.

5. Try this for a few of the commands you have learned so far.

You can close a command prompt either by clicking on the cross in the corner, typing exit and pressing enter, or choosing Close when right clicking the Command Prompt icon on the task bar.

For the emulator you can simply close the browser(tab).