Lab 1: Logging in and Editing files
Objectives
After completing this lab, you should be able to:
- Login to the course server
- Using the command line:
- Create and navigate in to / out of directories
- Create and open text files
- Edit and save text files
- Programmatically count the words in a text file
- Determine the ASCII code for characters in a text file
Ask a TA if you get stuck!
Go ahead and start working through this lab on your own, but if you run into any problems or get stuck, be sure to call a TA over -- that's what they're there for!
Logging in
Logging into ada requires the use of a secure shell (ssh) "client" program -- there are a few different ones installed on the lab computers. The one we recommend is free, and can also be downloaded onto your own computer if you're running Windows. It's called "putty", and can be found at http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html. If you're working on the lab computers, of course, you just need to locate it in the programs list and run it.
Logging into ada requires the use of your my.iit.edu username and the password handed out in class. After successfully entering your username and password, you will need to change your password. Do this now.
After a successful login, you'll see a prompt (the "command prompt") that looks something like this (it might vary a bit):
user@ada:~>
Now we're ready to start typing in some commands.
Directories and Navigation
While there are literally thousands of commands and programs that you can run from the prompt, you really just need to learn a few to get by initially.
For starters, the "ls" command allows you to list the contents of the current directory, try typing the following command and hitting enter:
ls
You won't see much, because you haven't create any files yet!
If you want to know what directory you're currently in, you can use the "pwd" (short for "present working directory") command:
pwd
You'll see that it prints out something like "/home/username". That's your "home directory" on ada.
You can create a directory with the command "mkdir" -- you have to give it an argument to tell it the name of the directory you want to create. Try the following to create the "test" directory under your home directory:
mkdir test
Now, if you do an "ls" (try it), you should see the "test" directory you just created in the command output.
Because you just created a directory, you can navigate into it -- do this with the "cd" (short for "change directory") command. Try the following:
cd test
Now run the "pwd" command to confirm that you're sitting in the "test" directory you just created. To go back "up" to the containing directory, use the ".." argument to the "cd" command:
cd ..
Not bad. Now let's delete the "test" directory we created, as we won't have any use for it. We do this with the "rmdir" command:
rmdir test
You'll be using the "ls" and "cd" commands a lot this semester -- be sure you understand their basic usage, and what they do!
Creating text files
We'll create a text file next. To do this we have to use a text editor. While there are several editors installed on ada, the easiest is probably one called "nano". We usually start it up by giving it an argument that corresponds to the name of the file we want to create. Run the following command:
nano hello.txt
You should see the editor fill the screen, with your cursor positioned at the start of the file, and a bunch of shortcut keys listed at the bottom of your screen. The little caret ('^') characters before each shortcut mean that you need to hold down the control key ('CTRL') together with the appropriate letter to carry out the action.
If you hit ctrl-x now (without having entered any text yet), the editor will simply quit without creating or saving the file. Try it and restart the editor again.
Now try and enter some text into the editor screen. Anything will do -- go for a few lines. Hitting ctrl-x now will ask you if you want to save your changes before quitting -- hit 'y' for yes and enter to confirm the file name.
If you run the "ls" command now, you should see the file you just created in the directory listing.
Text information
Now that you have a text file, you might want to learn a little more about it. One of things you can do very easily is to obtains statistics about the file contents. Do this with the following command:
wc hello.txt
wc stands for "word count". Note that the program prints out 3 numbers just before the file name corresponding to the number of lines, words, and characters in the file. Pretty nifty!
Another thing you can do is to obtain a listing of the ASCII values of the characters in the file -- try the following command (make sure you type it all in correctly!):
od -Ad -w10 -td1 hello.txt
The leftmost column of the output lists the first numerical byte "offset" of the corresponding row. The remaining columns, read left to right, contain the numerical values of consecutive bytes in the file. Make sure you understand how to read the output, and what it means -- ask a TA for help if you're confused!
Exercise 1
For your first exercise, you need to create and edit a text file named "ex1.txt" that should produce the following statistics when run through the "wc" command:
4 25 100
I.e., your file should contain 4 lines, 25 words, and a total of 100 characters. You will probably need to open, edit, save, and exit the text editor (and run the "wc" command) multiple times before getting it just right -- good practice!
Show the contents of your file to the TA, and demonstrate the output of the "wc" command for full credit.
Exercise 2
Next, using a text file and the output of the "od" program, determine the ASCII code for the following characters:
'a': ___
'A': ___
'%': ___
'@': ___
'?': ___
You should show your TA the contents of the file you use and the "od" output for full credit.