ECS40 - Week 0

Introduction to UNIX

1. Logging In

Login to your workstation with your CSIF account and password. If you don't know your username or password, take a look at the glass case across from room 83 in the basement or ask the helpful people in room 47.

What if you want to work from home, but don't have some form of unix installed on your computer? You can remotely login to the CSIF computers using SSH. First you'll need to obtain a free client for Windows called PuTTY (or some other SSH client). The machines in the basement are conveniently named pc1.cs.ucdavis.edu through pc98.cs.ucdavis.edu. For more information, see the CSIF FAQ.

2. The prompt

Click on the icon that looks like a black square with a white around it, or find Terminal in the main desktop menu. It should bring up a terminal with a prompt. This is the command line interface, a very powerful tool once you learn how to use it.

If you haven't done so already, change your password with the command yppasswd.
[hsuf@pc42 ~]$ yppasswd
Changing NIS account information for hsuf on thoth.
Please enter old password:
Please enter new password:
Try logging into your neighbor's machine with the following command (type the commands after the $ and replace <computer name> with the actual computer name).
[hsuf@pc42 ~]$ ssh <computer name>
Open a new terminal and see who is logged into your computer with the w command.
[hsuf@pc42 ~]$ w
22:22:40  up 19:08,  2 users,  load average: 0.05, 0.04, 0.01
USER     TTY     FROM             LOGIN@   IDLE   JCPU   PCPU  WHAT
hsuf     pts/0   node-423a6562.sm  9:28pm  1:26   0.04s 0.04s  -tcsh
hsuf     pts/1   node-423a6562.sm 10:06pm  0.00s  0.08s  0.01s  w
You can send message to your neighbor with the command write <username>. You'll get a blank line where you can being to type your message. End your message with a blank line then press Ctrl-D.
[hsuf@pc42 ~]$ write hsuf
hello francis
<Ctrl-D>
[hsuf@pc42 ~]$
The most important command you'll learn today is man. If you don't remember anything else today, remember man. You can use it to learn about other commands or how to use a particular command. Using the -k option will search for commands related to the keyword search term.
[hsuf@pc42 ~]$ man w
[hsuf@pc42 ~]$ man -k who
Question: What other options are available for man?

3. Files and Directories

You're probably already familiar with files and directories. When you log in, you start out in your home directory. You can move around the directory tree with the following commands.

pwd : shows your current directory
ls : shows a listing of the files in your current directory
ls <directory name> : show a listing of the files in a directory
cd <directory name> : change your current directory to the named one
cd .. : go up one directory
cd : change your current directory to your home directory
cd - : change to the directory you were last in
mkdir <directory name> : create a new directory
rmdir <directory name> : delete a directory

You can manipulate files with the following commands:

less <filename> : look at the contents of a file page by page
cp <filename> <newname> : copy a file
mv <oldname> <newname> : move (or rename) a file
rm <filename> : delete a file

Wildcards: You can use the star or question mark character (* or ?) as part of a filename. The star can replace 0 or more characters while the question mark replaces exactly 1 character. The resulting filename will represent all files that match. For example a* will match files named aback, abaft, abandon, ad, ah, and am, while a? will only match ad, ah, and am (assuming you have those files in your directory).

Redirection: If the output of some command goes by too quickly or you want to save it for future reference, you can direct it to a file. Simply add "> filename" to the end of your command. For example, ls > foo, will create a file named foo containing a list of the files in your current directory. You can also redirect commands to other commands with a pipe ( | ). For example, try ls | less.

Exercises:

  1. Create a new directory named "hat"
  2. Go into "hat" and create a new directory "cat"
  3. List the contents of "hat".
  4. Create a new directory "fish".
  5. Create a new file "one" in "fish" containing a listing of "hat" directory.
  6. Copy the file "one" to "two".
  7. Copy "two" to "red".
  8. Copy "red" to "blue".
  9. List all the files in the directory containing an "e".
  10. Look at the contents of "red".
  11. Delete all the files and directories you just created.

4. Editor

You'll need to use an editor to write your code. There are lots to choose from in unix, but I'll start you off with emacs. You can try out the other editors like vi if you wish. You shouldn't be using editors like pico, kwrite, or gedit since they don't offer many of the advanced features you'll want to learn to use.

Run emacs. Try out the tutorial by typing Ctrl-h, T (that's Ctrl-H, followed by T)

Create a new file with Ctrl-X, Ctrl-F called "hello.c" and write a simple C program to print out the words "Hello, world!" 100 times. If you need some help starting, you can use this file hello.c.

You can colorize your code by running
M-x font-lock-mode
(Type Alt-X and then type out font-lock-mode). If you want this to happen automatically, create a .emacs file and include the line:
(global-font-lock-mode 1).
You can play tetris with
M-x tetris
If you're feeling frustrated with emacs you can talk to a therapist with
M-x  doctor.

5. Job control

You can do more than one thing at a time on the command line. This time, run emacs &. You see the prompt return immediately. Putting a & after a command runs it in the background. You can use following command to manipulate processes.

Ctrl-C : interrupt and break the foreground job
Ctrl-Z :  suspend the foreground job
jobs : list the running jobs
fg <%n> :  bring job number n to the foreground
bg <%n> : run job number n in the background
ps : list the running processes (what is the difference between jobs and processes?)
kill <process id>: kill off the process with that id

6. Compiling

Use gcc to compile the file you just wrote.

gcc -g -o hello hello.c

And run it with ./hello.

7.  Other useful tools

pine or mutt : mail agent
tin : newsgroup reader

Exercises:
Try sending your TA some mail.
Run tin and subscribe to the ucd.class.ecs40 and ucd.class.ecs40.d newsgroups.

8. Text processing tools

Use man to figure out what the following tools do

grep
head
tail
sort
wc
diff

Exercises:
Using the file /usr/share/dict/words, can you find the first 10 words have the word 'cat' in them?
How many total words are there?