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: | Thursday, 26 December 2024, 7:05 PM |
1. Introduction
- Use the command 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.
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: doskey
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
- 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:
d:
<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
dir
and press <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 <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 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)"cd .. <ENTER>
cd . <ENTER>
cd .\John <ENTER>
cd John <ENTER>
.cd .. <ENTER>
Absolute paths
cd C:\John <ENTER>
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>
<CTRL>-<Z>
(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>
listdir <ENTER>
dir /AD
..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.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>
<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.
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>
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.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.
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).