Introduction to the Command Line
|Site:||IHE DELFT OPEN COURSEWARE|
|Course:||GIS OpenCourseWare for Hydrological Applications|
|Book:||Introduction to the Command Line|
|Printed by:||Guest user|
|Date:||Tuesday, 19 October 2021, 10:56 AM|
Prior to learning GDAL or scripting it is important to learn the Command Line and how to use commands from the command prompt.
Table of contents
- 1. Introduction
- 2. Viewing the contents of a directory
- 3. Drives and Directories
- 4. Creating and viewing files
- 5. Copy, move, rename & delete files
- 6. Command line history
Use the DOS prompt and the most common commands
Navigate to files and directories and use their relative and absolute paths
- Make batch files
2. Viewing the contents of a directory
Start the online DOS emulator if you haven't done yet.
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.
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:
You will see 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
- 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:
<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
<ENTER>again at the command prompt to get the directory listing.
<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.
md John <ENTER>
md Peter <ENTER>
dir /AD <ENTER>
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
Note that DOS is
not case sensitive (it interprets upper and lowercase in a similar way), so you
could also have typed
2. List the contents of the directory John (use the command that you've learned before).
you'll see that the directory is not empty. There are two directories, namely
Actually, the directory is empty, but it also shows the relative paths:
. means "current directory"
..means "mother directory (or one directory up)"
cd .. <ENTER>
cd . <ENTER>
cd .\John <ENTER>
cd John <ENTER>.
cd .. <ENTER>
cd C:\John <ENTER>
cd \ <ENTER>
Why bother about absolute and relative paths?
10. Go back to the directory called John. You can choose if you want to use the relative or absolute path
cd ..\Peter <ENTER>
12. 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
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 means "remove directory".
3. List the directory to check if Peter has been
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.
con listdir.bat <ENTER>
dir /AD <ENTER>
(keep the control button pressed and type z)
Your screen looks like this:
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.
type listdir.bat <ENTER>
.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.
dir > list.txt <ENTER>
list.txt. So we can use
>after a command to save its results to a file instead of printing it to the screen.
type list.txt <ENTER>
type list.txt | more <ENTER>
list.txtis 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.
7. Now try this:
listdir.bat >> list.txt <ENTER>
list.txt | more <ENTER>
9. What happened?
>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.
<CTRL-C>to stop the execution of a command.
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:
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
5. We can
list.txt to subdirectory John:
list.txt john\newlist2.txt <ENTER>
5.2. Move and rename files
Now we're going to
newlist.txt to the directory John.
1. First go back to the root of the C-drive.
newlist.txt John <ENTER>
2. Go to the subdirectory John and check the directory listing.
it is a bit confusing to have a copy of
newlist.txt in subdirectory John, we're
going to rename it:
newlist.txt listjohn.txt <ENTER>
4. Check the directory listing again.
the directory listing to a file called
5.3. Use wildcards
2. Type the commands below and check the results:
dir *.txt <ENTER>
dir *john.* <ENTER>
dir *.??t <ENTER>
dir *.?xt <ENTER>
3. Explain the
5.4. Delete files
we made copies, we want to remove duplicate file.
1. We'll remove
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>)
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
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.
doskey command we can do even more.
doskey /h <ENTER>
This prints all the commands you typed during this session to the screen.
4. Save the command history to a text file using
one of the commands previously learned.
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).