Lab 1: CS Lab Machines & Command Line
Objectives
The main goals for this lab are:
-
Get familiar with the command line.
-
Set up your course directory on the CS machines.
-
Start using vim.
Make sure to write down your answers for each Question below. Whenever you see TODO, that means there is an action item for you to complete. When you see Question, that means you should fill in your answer on the worksheet provided to you. When you believe you have finished the lab, show Prof. Shiptoski your work.
1. CS Lab Machines & the Command Line
The computers in the lab rooms (Park 230 and 231) are dual booted with Windows 10 and Ubuntu (a version of linux). This means that you can use either the Windows or Linux operating systems (OS). For this course, we will be using Linux.
You should have received an email from David Diaz with your credentials to the Computer Science lab machines. If you did not receive credentials from him, immediately stop by his office in Park 294.
TODO: Log into the Linux (Ubuntu) OS using the credentials you received from David. Note: These are not your BMC/HC credentials that you normally use.
After logging in, click on the grid on the bottom left of the screen. This will launch a screen showing all the applications. In the search bar on top, type terminal, and then click on the application. This will launch the terminal. In this semester we will be working primarily in the terminal.
Understanding the Command Line
As soon as the terminal application launches, you should see a black screen. This is where you will type command line commands. For now, we haven’t covered any commands in detail so just type anything into the terminal.
To run a command in the terminal, click Enter. After typing
anything into the terminal (here I mean actually mean anything),
click Enter.
Question 1: After you click Enter, what does the terminal
tell you?
Answer:
Change Your Password
You can completely interact with your computer using just the terminal! When a new account is created for you on any computer system, you should always change your password.
TODO: Change your password! In terminal, type the command passwd and then press Enter
and then follow the instructions to change your password.
Command Line Prompt
All of the text thats on the left of where you are typing
commands is called the prompt. You should notice that there
are two words in the prompt that are seperated by an @
symbol. For example, kshiptoski@goldengate:~$.
Question 2: What are the two words in your prompt? What
does the first word indicate (the word infront of the @)?
What does the second word indicate (the word after the @)?
Answer:
Navigation Commands
Now we are ready to start using command line arguments. We will be using these command lines today:
-
ls -
cd -
mkdir -
wget -
cat -
wc
pwd command
The first command we’ll look at is pwd. This command prints
the path to the working directory.
Computers are structured in a directory hierarchy where directories can contain other directories or files. A directory is just another way of saying a folder.
TODO: Run the command pwd (this means type pwd in the
command line and click Enter - we’ll be use this
terminology from here on out).
Question 3: What gets printed out? What do you think it means?
Answer:
ls command
The next command will look at is ls. Instead of us first describing this command,
we’ll let you try it out yourself. Run the command ls.
Question 4: What do you see printed out on in the terminal? What do you think all of that means?
Answer:
We can add arguments to the ls command. After typing in ls, type the output of the pwd command. For example, I would run the following command:
ls /home/kshiptoski
Question 5: When you run ls with the path to your working directory, what do you see? Do you see anything different than before when you ran just ls? Why or why not?
Answer:
Piping
We can pass the output from one command as an argument to another command by using the | symbol.
TODO: Run the following command:
pwd | ls
Question 6: What do you see? Do you see anything different than before when you ran just ls? Why or why not?
Answer:
Home Directory
So far we’ve used ls to look at the files and folders in our home directory. A home directory is where all the files and folders that belong to a specific user exists.
TODO: Look carefully again at the command line prompt. At the end of the line after the name of the machine you are working on, you should see two characters. What symbols do you see? One of those represents your home directory - which one do you think it might be?
We can answer this question by using the ls command. TODO: Run the ls command twice, each time
add one of the characters/symbols as an argument to ls. For example, if the characters were A and B
(they are not), you would run:
ls A
ls B
The output of running ls with each character would give you a hint of which of the two symbols represents your home directory.
Question 7: So, which of the two characters/symbols represent your home directory?
Answer:
Absolute vs Relative Paths
So far we used ls to look at files in our own directory. When we run the command ls /home/<USERNAME>
(here <USERNAME> is a placeholder for your actual username), the argument was an absolute path. An absolute path
is the path from the root (or top) of the computer/file system. We can also specify relative paths. These are paths
that go from the current working directory to another folder that we specify. For example, when we run ls Downloads/, that will tell us what files and folders exist in the Downloads directory.
Question 8: If we wanted to see what files exist in the Downloads directory but using an absolute path, what argument would we pass to ls?
Answer:
Viewing Other Directories
From pwd, we see that the path to our home directory was /home/<USERNAME>.
Question 9: How do you think we could use ls to find out what other users have an account on the CS lab machines?
Answer:
We can also use a relative path. From your own directory, run the following command:
ls ../
Question 10: What do you think that did? What do you think ../ means?
Answer:
cd command
The next command will look at is cd, which stands for change directory.
Like ls, you can pass an argument to the cd command. Here the argument we pass in is the directory we want to move to.
TODO: Run the command cd Downloads/.
Question 11: What changed in the command line prompt? What do you think that tells us?
Answer:
We can also run cd without any arguments.
TODO: Run cd without any arguments.
Question 12: Based on the prompt, what directory do you think we are now in?
Answer:
Question 13: Think about the following command: cd cs113. What do you think will happen if we run this command?
Answer:
Question 14: Now run cd cs113. Did we move into that directory, why or why not?
Answer:
mkdir command
We can create new directories in the command line using the command mkdir.
TODO: Run mkdir without any arguments.
Question 15: Were you able to create a new directory? If not, why not?
Answer:
Question 16: You should notice an error message. Read the error message. Are there any new technical terms in the error message? What do you think it means?
Answer:
The error message should also tell you an option or flag to use with mkdir
that can help you figure out how to use mkdir. We’ll use the term flag and option interchangably here.
Question 17: What is that specific flag?
Answer:
Next, use that flag to read the instructions to figure out how to create a new directory using mkdir.
There is another flag you can use when running mkdir that will tell us the version of mkdir.
Question 18: Based on the results from running mkdir with that flag, what version number
is the mkdir on the CS lab machines? Also, who is the author?
Answer:
TODO: Use mkdir to create a new directory called cs113.
Question 19: Using a command that we’ve seen so far in today’s lab, how can we determine that the directory cs113 was indeed created?
Answer:
Setting Up Your CS113 Directory
It’s a good idea to stay organized through out the course. You will be writing a lot of programs across many homeworks, labs, and in-class demo sessions. Therefore, we will now create that structure.
TODO: Using mkdir, cd, and ls, create the
following directories and subdirectories in your cs113 directory.
cs113/
labs/
lab1/
homeworks/
hw1/
demos/
More Commands
There are many more commands that you will be using during the semester.
These include cp, rm, man, and others. Using the --help flag, you can read about them
in the terminal.
As the semester progresses, you will get more comfortable with and even gain mastery over the command line. Programming is a skill that can be developed like any other skill: practice, practice, practice!
2. VIM Editor
This semester we will use the vim editor for editing files on our system. You will primarily use
vim to create and edit files containing the java program code to run on our system.
The vi and vim (Vi IMproved) editors are available on every Unix system. vim is an efficient
and lightweight text editor that is easy to use after learning a few basic commands, which you
can learn by running though the vimtutor tutorial.
vim is particularly useful when working remotely over an ssh connection (an ssh connection is
how you will connect to the lab machines remotely).
vim also has many advanced features and is very configurable through, e.g., the use of a .vimrc file.
However, just a few basic commands are enough to get you started.
vim operates in two modes:
-
insert mode: keystrokes are interpreted as inserts into the file contents at the point of the cursor.
-
command or escape mode: keystrokes are interpreted as vim commands, which allow a user to do such things as saving, exiting, searching, or moving around in the file.
To switch from insert mode to command mode, press the ESC key.
There are many ways to switch from command mode to insert mode. One way is to press the i key.
TODO: Run the following commands in order and think about what each one is doing.
$ pwd # list your current working directory
$ cd # go to home (root) directory from current directory
$ ls # list root directory contents
$ cd cs113/labs/lab1 # cd into your cs113/labs/lab1 directory
$ pwd # should list /home/you/cs113/labs/lab1
$ ls # list the contents of your /home/you/cs113/labs/lab1
TODO: You should now be in your cs113/labs/lab1 subdirectory. Run the vim tutorial:
$ vimtutor # start the vim tutorial
TODO: Go through the sections listed below of vimtutor (the other sections cover more
obscure features that are not necessary). It will take about 30 minutes to run through these lessons:
-
All of Lesson 1 (moving around, x, i, A, :wq)
-
Lesson 2.6 (dd)
-
Lesson 2.7 (undo)
-
Lesson 3.1 (p) and 3.2 (r)
-
Lesson 4.1 (G) and 4.2 (searching)
-
Lesson 6.2 (a), 6.3 ( R ), and 6.4 (y and p)
3. Downloading & Viewing Files
For the rest of this lab, you should be working in the labs/lab1/ directory.
wget command
We are now going to start looking at some data. wget is a “non-interactive network downloader,” in
other words it is a command that will download files from the internet.
TODO: Run wget and follow the instructions to figure out how it works.
Question 20: What argument does wget require?
Answer:
We are going to download a book from Project Gutenberg, an awesome project that is dedicated to the creation and distribution of eBooks.
TODO: On Project Gutenberg, search for a book that you are interested in. Then use wget to
download the book.
Note: Make sure to download the plaintext version. This can be found by clicking on Other formats & older devices. Right click on Plain Text and click copy link.
Example Command:
wget https://www.gutenberg.org/ebooks/345.txt.utf-8
Question 21: What command can you use to determine that the file has downloaded? (Write your answer here, and then actually run the command to ensure it is there).
Answer:
mv command
mv is a command that can be used to move from one directory to another, or even rename files.
Question 22: Use mv to rename the file to be the name of the book. Make sure to not have
any spaces in the name of the file and that the file name ends in .txt. Write down the command
you used here.
Answer:
cat command
The last command we’ll use in this lab, is cat. We are going to figure out what cat
does by playing with it.
Question 23: Run cat. Next, type a message into the command line and press Enter. What happened?
Answer:
Once you are finished with cat, hit CTRl-C, this will terminate the cat program.
TODO: Using the same way we figured out how to use other commands, use the same method to read more about cat.
TODO: Then, use cat to print out the entire book.
TODO: Read the entire book. Just kidding! This is the end of the first lab, please give your completed worksheet to Professor Shiptoski.