Introduction to Unix I & II 

Computer Science Macintosh & Gnu/Linux Lab
  WWW: http://www.maclab.cs.uchicago.edu
E-mail: tutor@cs.uchicago.edu

Authors: Ivan Beschastnikh & Steven Alyari
This document was last modified Mon, Oct  6, 2003

The official location for this document is:
 
http://www.maclab.cs.uchicago.edu/unix_tutorial


Contents

  1. Purpose
  2. Notation
  3. Introduction
  4. Basic Commands
    1. Directories
    2. Files
    3. Other Commands
  5. E-Mail
  6. Printing 
  7. Remote Access
    1. ssh
    2. scp
  8. Editors & their basic commands
    1. pico
    2. emacs
    3. vi
  9. CVS (by David Beazley)
  10. Window Managers
  11. Programming Languages
  12. Other Sources
    1. man pages
    2. the web

1. Purpose



The purpose of this tutorial is to provide you with an overview of the services offered on UNIX-like systems and especially the GNU/Linux operated CS Network.  Another primary goal is to provide you with many references to documentation that we have found especially useful for learning.  The tutorial is termed UNIX I and II, simply because beginner's seem to understand that name better and because GNU/Linux is definiately a UNIX-like operating system.  UNIX is a trademark of AT&T Bell Labs and an Operating System designed in the 1970s, which partly laid the conceptual framework for many other operating systems such as GNU/Linux, FreeBSD, and even Mac OS X; simply, to name a few.  Though, oddly, it is interesting to note that these OS's have developed so much since the 70's that to continue to use the term UNIX is to rob UNIX the original meaning of the term UNIX.  And finally before we begin, we add one more layer of confusion by telling you that GNU stands for GNU's Not UNIX.


2. Notation



3. Introduction


There are many different types of Unixes. The ones in the lab before you are a type of Unix called Linux. Specifically Stable DebianDistribution. To be able to gain access to the machines in this lab you will need to have a cs domain account and password which can be created @ https://www.cs.uchicago.edu/info/services/account_request The graphical interface that you see before you is just another program run by the computer. This is unlike windows where the visual screen is the only way to interface with the computer. A barebones unix operates through the use of multple terminals. You can view each physical terminal by "switching terminals." This is done using C-M-F1, C-M-F2 ... C-M-F12. Note that Unix machines can have a varied number of terminals. Usually between 1 and 7 for the physical terminals and an almost unrestricted amount for the virtual terminals used by remote users. Even if you use the X-windowing system to login and work, you will still need to know various commands in order to be efficient and do things that are otherwise impossible.
Everyone Logs On


4. Basic Commands


There are a few guidlines that you should always follow: Never execute a command unless you are pretty sure of its effects and those effects are desired, and all Unix commands are case-sensitive which means that 'dir' and 'DiR' are not the same thing and may in fact be two different commands with very different actions.

All commands are must be typed on the command line. To execute a command, type [ENTER] after the command. Most commands take different arguments which give the command information about what it needs to do, but some run without any arguments.  Arguments always follow the command and usually have a strict placement with respect to each other. There are two types of arguments: Mandatory and Optional. Commands also have 'options'. Options are just like arguments except they modify the behaviour of the program. options are usually specified by a dash.

For example, the two commands 'ls' and 'ls -a /'  both run the same program - 'ls' but the first is run without any options or arguments while the second is run with an option 'a' and an argument '/'. In the tables that follow, some commands take italic argument(s) enclosed into brackets and/or argument(s) unenclosed in brackets. The italic arguments are optional while non italicized arguments are mandatory and will cause an error if not specified.

Directories

The directory structure on Unix starts with '/'. This is the root directory or the top level directory of the whole system. The home directory which is where your files are located is specified by '~/'.

Directory Command
Description
cd [directory]
changes current directory to directory
if argument not given, changes current directory to '~/' - the home directory
note that directory can be the absolute path or relative to your current path (see examples)
cd ..
change current directory one level up
mkdir DIR
creates a directory within the current directory named DIR
rmdir DIR
removes a directory with path DIR
pwd
prints the current working directory
cp [-R]  SOURCE DEST
copy a directory source as dest
-R specifies to copy the directory recursively


Example 4.0:

 cd ~/                  	        # go to home dir
> pwd # where am i?
/home/ivan # i'm in my home dir
> mkdir tmp # make directory in home dir name 'tmp'
> cd tmp # change current directory to 'tmp'
> pwd
/home/ivan/tmp
> ls # list files in working directory
> ls -a
. ..
> ls -a -l
drwxr-xr-x 2 ivan college 512 Oct 4 19:29 .
drwxr-xr-x 3 ivan college 512 Oct 4 19:29 ..
> mkdir tmp2 # make new directory name 'tmp2'
> ls # is it there?
tmp2 # ... yes it is
> ls -a tmp2 # show me everything in tmp2
. ..
> cd tmp2
> ls -a -l
drwxr-xr-x 2 ivan college 512 Oct 4 19:38 .
drwxr-xr-x 3 ivan college 512 Oct 4 19:38 ..
> pwd
/home/ivan/tmp/tmp2
> cd ..
> pwd
/home/ivan/tmp
> rmdir tmp3 # remove tmp3
rmdir: tmp3: No such file or directory # oops
> rmdir tmp2 # remove correct directory 'tmp2'
> ls # is it still there?
> # ... no

Files


Files are the most basic abstractions in Unix. They are used for everything, starting from all devices such as printers, floppies and cdroms, and ending with configuration files and your every day files.

ls
list files in current working directory
less FILENAME
view the file FILENAME, use the key 'q' to exit
more FILENAME
view the file FILENAME, use the key 'q' to exit


rm FILENAME
remove the file FILENAME
cp SOURCE DEST
copy the file SOURCE as DEST
mv SOURCE DEST
first copy the file SOURCE as DEST and then remove the file DEST








Example 4.1:

> cd ~/tmp/					# go to /home/ivan/tmp
> pwd # where am i?

An operating system is the program that controls all the other parts of a computer system - both the hardware and the software. Most importantly, it allows you to make use of the facilities provided by the system. Every computer has an operating system.

The UNIX operating system has three important features; a kernel, the shell and a filesystem.

/home/ivan/tmp
> ls -la
drwxr-xr-x 2 ivan college 512 Oct 4 19:42 .
drwxr-xr-x 3 ivan college 512 Oct 4 19:42 ..
-rw-r--r-- 1 ivan college 55 Oct 4 19:42 hello_world
> more hello_world # what's in this file?
Welcome to the MATRIX!
Thou wilt be programmed!

- neo
> less hello_world # what's in this file?

Welcome to the MATRIX!
Thou wilt be programmed!

- neo
hello_world (END) # press 'q' to exit
> cp hello_world h2 # copy 'hello_world' to 'h2'
> ls
h2 hello_world # now there are two files
> mv hello_world h3 # rename 'hello_world' with 'h3'
> ls # now what's there?
h2 h3 # two files: 'h2' & 'h3'
> rm h3 # remove h3
> ls
h2 # now there's only h2
> mv h2 ../ # move h2 one level up the directory tree
> ls # now there are no files here
> cd .. # go up one level
> ls
h2 # here's the file we moved
>


Other Commands


There are many other commands under Unix that accomplish a variety of tasks. If you are trying to do something, chances are that a command was already written to accomplish whatever it might be. Table 4.3.1 lists some of the most useful ones

Command Description
ps
lists the currently runnign process


touch

w [ -hlsuw ] [username]
list all the users on the machine as wel last their information
finger [username] finger without username acts like 'w' with no options
when a username is supplied, it prints an entry from database about the user username






















5. E-Mail


Having a user account on the CS domain not only allows you to use machines within the domain but also send and receive email onto your account. Congratulations, your new email is <username>@cs.uchicago.edu

Many are available on our linux systems, including pine, mutt, and mail readers provided by various browsers such as mozilla or netscape.

For more information about your CS mail account go here: http://www.cs.uchicago.edu/info/services/email/
To configure IMAP for checking mail, follow these instructions:  http://www.cs.uchicago.edu/info/services/imap
To configure mail forwarding, follow these instructions: http://www.cs.uchicago.edu/info/services/email/forwarding

To find out more about pine (developed at the University of Washington), go to: http://www.washington.edu/pine/
To find our more about mutt, go to: http://www.mutt.org

6. Printing


There are two commands for generic printing services.  enscript prints text files, while lpr prints postscript files.  This is their specific syntax.
  1. > enscript Pmaclab@oberon <file.txt>
  2. > lpr -Pmaclab@oberon <file.ps>
In addition you may want to setup your $PRINTER environmental variable to oberon.cs.uchicago.edu in your shell's rc file, so that all applications inherit your default printing environment.

7. Remote Access 


The machines you see before you can also be accessed in one other way - Remotely. You can be sitting at home or anywhere else within this lab and still be able to log into a Unix machine on the cs domain, see the files residing on this machine and execute commands on the machine.  To find a machine on the CS network check out the following page: http://www.cs.uchicago.edu/info/services/loginhosts_top.  If you are a CSPP student, the list is significantly longer and can be viewed here: http://stuff.cs.uchicago.edu/searchresults/os_name/Linux/alive_days/1/ips_loc/402/net_group_list/cs_users_cppcs/stdalone_url/on

The remote machine is usually reffered to as the HOST
The local machine is usually reffered to as the CLIENT

There are many programs that you can use to gain access remotely. SSH - secure remote login program and TELNET - unsecure remote login program are your two main options  We strongly advise you to use SSH to connect to remote computers because it encrypts your password and any command you send over the network. In fact, the machines in the lab do NOT allow TELNET connections and will only accept SSH.

ssh

To use ssh you must supply it with a hostname or an ip address of the host. The hostname is indicated on the monitor of your machine. Remember that all hostnames on CS domain should be followed by '.cs.uchicago.edu' for correct resolution.

> ssh the-400.cs.uchicago.edu                # connect to the-400 on cs domain

SSH assumes that you want to connect as the current user, but if you want to connect as someone else, you can do so:

> ssh mickey_mouse@the-400.cs.uchicago.edu   # connect as mickey_mouse to the-400

TELNET works in a similiar way, to find out more, use 'man telnet' and 'man ssh'


scp

scp stands for 'Secure cp' where 'cp' is copy. In other words, scp copies directories and/or files across machines. This is very useful if you want to 'upload' or 'download' a file to a host or your local machine. Note that this is a secure way to transfer files whereas the popular 'ftp' - file transfer protocol method is insecure but has a more elaborate interface.


For example, i want to view a file I have saved on harper.uchicago.edu:

> scp ivan@harper.uchicago.edu:~/hello_world from_harper    # get file hello_world from harper and put name it as from_harper
ivan@harper.uchicago.edu's password:
scp: warning: Executing scp1 compatibility.
hello_world 100% 51 0.0KB/s 00:00 # download information
> more from_harper
hi, this file resides on harper.uchicago.edu

EOF

>

Now i want to upload a directory of files to harper:

> scp -r tmp harper.uchicago.edu:~/new_tmp	# upload the directory tmp to harper and name it as new_tmp
# make SURE that the -r flag is present, this means to scp recursively
ivan@harper.uchicago.edu's password:
scp: warning: Executing scp1 compatibility. # some warning messg, ignore
stupid.sh 100% 85 0.0KB/s 00:00
helloworld 100% 100 0.0KB/s 00:00
helloworld2 100% 471 0.0KB/s 00:00
job_latex.tex 100% 1110 0.0KB/s 00:00
h2 100% 55 0.0KB/s 00:00
flight.tex 100% 3604 0.0KB/s 00:00
flight.tex~ 100% 885 0.0KB/s 00:00
hello_world 100% 55 0.0KB/s 00:00
>


8. Editors & their basic commands


Editors are a necessity in Unix. There is basically no way to edit files without the knowledge of some editor. There are many editors in Unix and everyone has a preference. Here are the three you will find people using most frequently.

Pico

Pico  -  simple  text editor in the style of the Pine Composer. It is one of the easiest editors to use in a Unix environment and can be learned in minutes. To run pico, type 'pico' or 'pico filename'. If the file does not exist, pico will create a new one and if the file exists, you will see it displayed in a window.

UW PICO(tm) 4.2                New Buffer                                  # top of the pico window

4.2 Specifies the version of PICO you are using and 'New Buffer' indicates that you haven't saved the file yet. If you opened an existing file or saved the current one, it would specify the filename.

^G Get Help  ^O WriteOut  ^R Read File ^Y Prev Pg   ^K Cut Text  ^C Cur Pos   
^X Exit      ^J Justify   ^W Where is  ^V Next Pg   ^U UnCut Text^T To Spell    # bottom of pico window

Each commands listed at the bottom has it's own key binding. The carrot - '^' indicates that you should press the [CTRL] key as you type the letter. So for example to exit pico, you need to hold down the [CTRL] key and while holding it down, press 'X'. To enter text into the buffer, just type keys on the keyboard. When you will try to exit, prompt will ask you if you want to save changes to which you can respond with a yes - 'y',  a no - 'n' or ^C to cancel the exit sequence.

For more information on pico, type ^G for a list of useful commands or 'man pico' for more elaborate information.


Emacs
 

Emacs is one of the most powerful editors in Unix. It supports many formats or modes for editing c, html, c++, tex files and many more. It is very highly customizable and supports macros. It's commands list ranges over many, many pages branding it a one of the bulkiest and most functional editors ever. Emacs tries to please everyone, even you!

To run emacs, type 'emacs' or 'emacs filename'. Emacs supports two different window states - as a program within a terminal or as a Xwindows program. Both have their advantages. By default, emacs tries to launch itself as an Xwindows application, but that can be overriden with the '-nw' flag on the command line. Therefore to run emacs in a terminal, type 'emacs -nw' or 'emacs -nw filename'.


Here's a list of basic commands implemented in Emacs, note that this is not an exchaustive list so if you are interested in learning more, visit
http://www.gnu.org/manual/emacs/index.html and http://www.gnu.org/software/emacs/emacs.html

To get started right away, download and print the Emacs reference card: http://www.refcards.com/download/emacs-refcard-letter.pdf
Since that link seems to be down, here's a website with about as much information: http://www.indiana.edu/~ucspubs/b131/
Vi  

Vi is one of the weirdest editors ever, and it's also the least common denominator of all editors of practially all UNIX system installations.  It also can be kind of fun to learn and use, if you are somewhat deranged.

Beginner and advanced vi documentation can be found here: http://people.cs.uchicago.edu/~salyari/vis/tutorial

A reference to UC Berkeley's screen oriented editor called vi (for VIsual):
to get into the editor from Unix:           {vi filename}
to exit the editor
saving all changes {ZZ} or {:wq^M}
throwing away all changes {:q!^M}
when no changes have been made {:q^M}
save a file without exiting the editor {:w^M}
write the file into another file {:w filename^M}
insert text
before the cursor {i ...text... ^[}
at the beginning of the line {I ...text... ^[}
after the cursor (append) {a ...text... ^[}
at the end of the line {A ...text... ^[}
after the current line {o ...text... ^[}
before the current line {O ...text... ^[}
delete the character ...
under the cursor {x}
to the left of the cursor {X}
delete n characters {nx} or {nX} (for n a number)
make two lines into one line (Join) {J}
find a string in the file ...
searching forward {/ ...string... /^M}
searching backwards {? ...string... ?^M}
repeat the last search command {n}
repeat the last search command in the
opposite direction {N}
find the character c on this line ...
searching forward {fc}
searching backward {Fc}
repeat the last 'find character' command {;}
replace a character with character x {rx}
substitute a single character with text {s ...text... ^[}
substitute n characters with text {ns ...text... ^[}
replace characters one-by-one with text {R ...text... ^[}
undo all changes to the current line {U}
undo the last single change {u}
move forward in the file a "screenful" {^F}
move back in the file a "screenful" {^B}
move forward in the file one line {^M} or {+}
move backward in the file one line {-}
move to the beginning of the line {0}
move to the end of the line {$}
move forward one word {w}
move forward one word, ignoring punctuation {W}
move forward to the end of the next word {e}
to the end of the word, ignoring punctuation{E}
move backward one word {b}
move back one word, ignoring punctuation {B}
return to the last line modified {''}
scroll a line onto the top of the screen {^Y}
scroll a line onto the bottom of the screen {^E}
move "up" in the file a half-screen {^U}
move "down" in the file a half-screen {^D}
move the cursor to the top screen line {H}
move the cursor to the bottom screen line {L}
move the cursor to the middle line {M}
move LEFT one character position {h} or {^H}
move RIGHT one character position {l} or { }
move UP in the same column {k} or {^P}
move DOWN in the same column {j} or {^N}
mark the current position, name it x {mx}
move to the line marked/named x {'x}
move to the character position named x {`x}
move to the beginning of the file {1G}
move to the end of the file {G}
move to line 23 in the file {23G}
repaint the screen with the cursor line
at the top of the screen {z^M}
in the middle of the screen {z.}
at the bottom of the screen {z-}



9. CVS 

A Brief Introduction to CVS (by David Beazley)

CS230/330 - Operating Systems (Winter, 2002).

Overview

CVS is version control system that is particularly useful when working on large software projects or when working in groups. In a nutshell, CVS stores your software in a special repository that holds all of the versions of your software as well as information about when files were modified, who modified them, and logs describing why changes were made. Some of the more notable features of CVS include the ability to "undo" changes if you mess up and making life easier when multiple people are working on a single project.

All of the gory details about CVS can be found at www.cvshome.org. This document describes the bare essentials that you need to know for this class.

The Repository

Your account on the class machines should already be properly configured to use CVS. In your home directory, there should be a directory called CVS. This directory is the repository that will hold all of your files. Under normal circumstances, you should never have to look in this directory. Likewise, you should never modify any files found in this directory.

Creating a new CVS project

To create a new CVS project, first create a directory with all of your working files. For example, you might have a directory "myproject" that looks like this:
% cd myproject
% ls
Makefile
README
bar.c
foo.c
%
Make sure your directory is clean and only contains the files you want to place in the repository. Object files, executables, core files, and editor backup files should be deleted from the directory in this process.

Now, import your directory into the CVS repository as follows:

% cd myproject
% cvs import myproject yourname start
N myproject/Makefile
N myproject/README
N myproject/bar.c
N myproject/foo.c

No conflicts created by this import
%
After typing the 'cvs import' command, an editor will appear asking you to type in a log message. This message should simply be a short description of the project. After saving the message, the files will be imported into the repository.

Note : To save the log message and exit from emacs, you will have to type Control-X Control-S followed by Control-X Control-C. To save the message and exit from vi, you should type ":w" followed by ":q".

After importing a directory into CVS, you should either rename or remove the original project directory and checkout a fresh version from the repository.

Checking out a project

To check a project out of the CVS repository use the 'cvs checkout' command like this:
% cvs checkout myproject
cvs checkout: Updating myproject
U myproject/Makefile
U myproject/README
U myproject/bar.c
U myproject/foo.c
Now, all of your original files should be restored in a new directory "myproject".

Making changes

When working in a CVS project directory, you can modify all of the files exactly as you normally would. However, after you are satisfied with a set of changes, it is a good idea to commit those changes back to the repository. This can be done for individual files or for an entire directory using the 'cvs commit' command like this:
% cvs commit foo.c
Checking in foo.c;
/u1/dave/CVSROOT/myproject/foo.c,v <-- foo.c
new revision: 1.2; previous revision: 1.1
done
%
Before committing the file, an editor will be started where you will be asked to enter a log message describing your changes. As a shortcut, you can also enter a change message on the command line like this:
% cvs commit -m "Fixed the segmentation fault error" foo.c
Changes made to an entire directory can be committed in the same manner. For example:
% cvs commit -m "Handin" myproject

Adding new files and directories

When you add new files and directories to your project, they are not automatically added to the CVS repository. Instead, you must explicitly add the files using the 'cvs add' command like this:
% cvs add spam.c
cvs add: scheduling file `spam.c' for addition
cvs add: use 'cvs commit' to add this file permanently
This only tells CVS that a new file will be added. The addition does not take effect until you commit changes using the 'cvs commit' command.

Removing files from the repository

To remove a file from the repository, use the 'cvs remove' command. For example:
% rm foo.c
% cvs remove foo.c
cvs remove: scheduling `foo.c' for removal
cvs remove: use 'cvs commit' to remove this file permanently
Again, this command only takes effect after you have committed your changes with the 'cvs commit' command.

Tagging a project

When your code is in a reasonably stable state, you can use the 'cvs tag' command to apply a name to your current project state. For example:
% cvs tag version1 myproject
Later on, if you ever need to go back to "version1", you can go into a clean directory and type "cvs checkout -r version1". This will check out the version of code that was tagged as "version1". This can be particularly useful if you proceed to mess something up beyond repair---you can simply restore the last tagged version and restart.

Doing a time-warp

If you are really in a bind and need to go back to a previous version of code, you can specify a date with the 'cvs checkout' command. For example:
% cvs checkout -D "2 days ago" myproject
Of course, this will only work if you have been diligent about committing files to your CVS repository. Do a 'man cvs' for all of the various date specification options.

Handing in a Project

All projects in this course will be handed in via CVS. Prior to handin, you need to go through the following checklist:

General tips

When making lots of program changes, CVS can be a lifesaver. As you work on the kernel project, you are encouraged to check in files as frequently as you see fit. There is no penalty for checking in several hundred versions of the same file (all we care about is the most recent version). CVS can be used by multiple people, each working on a version of a project in their own directory. When working with multiple people, it is always a good idea to do a 'cvs update' before committing any changes. This will bring your code up to date with any changes that someone else might have committed while you were working.

Where to go from here?

More information about CVS is available at www.cvshome.org. More details about each of the commands can be found by typing 'man cvs'.



11. Window Managers



The following is taken from: http://www.cs.uchicago.edu/info/services/debian_new
Debian supports a variety of window managers. Below are configuration details for gnome, kde and icewm.

Edit your ~/.xsession file

To switch to new window manager, you simply edit the .xsession file in your home directory. The window manager should be the last command run from this file (lines that start with # are comments).

We install software that is not included in the standard debian distribution in /opt; you can access newer versions of some window managers there.

For example, to use any of the following window managers, put the associated lines in your ~/.xsession file:

Note: Make sure your .xsession file is exectutable and begins with:
#!/usr/local/bin/bash
If you do not do this, your file will not work.

To provide you with an example, this is what my .xsession file looks like:
#!/usr/local/bin/bash
xset b off&
fvwm2
#blackbox



12. Web Pages


13. Programming Languages


14. Relational Database Management Systems (RDBMS)


15. Other Sources


Man and Info Pages

Not all commands are covered by Man and Info pages, but many of the standard ones such as binutils are.  Man and info are different ways of displaying different manuals for usually different commands.

type man <command> for the manual page of a command
> man ls
> man man

type info <command> for a TexInfo page of a command
> info emacs
> info info
now you may be saying all this is useful, but what if I don't know the name of the command I want to use.  what if I only know its purpose?  we give you apropos.
> apropos sort
lsort (n)            - Sort the elements of a list
sortm (1)            - sort messages
xroger (1)           - throbbing X logo, of a sort

Web


http://services.cs.uchicago.edu
Specific information from the people who bring the UNIX services to you.

http://safari.oreilly.com
Very recently, the University purchased a subscription to Safari, the new online book publishing service by O'Reilly.  O'Reilly is one of the best computer book publishers out there, and that's why this manual points you to specific O'Reilly pages that relate to the topics, if, of course, the University subscribed to the specific book.  Unfortunately, they do not have a subscription to EVERY book, but they stil provide access to a lot of great books!

http://support.uchicago.edu/docs/unix/
NSIT's set of tutorials regarding newsgroups, e-mail, editors, and shells.

http://unixhelp.ed.ac.uk/index.html
"Unix Help for Users"

More Unix Commands  (adapted from http://www.cs.uchicago.edu/info/services/finding_software)

Debian tries to put common application names in the menus of some windowing systems, such as kde or gnome. Look through the menus of your window manager for debian or programs.

You can list all the packages currently installed on a debian machine with:
dpkg -l

To see all the files contained in a package:
dpkg -L PACKAGE

/opt

We install software that is not part of the debian archive, or more recent versions than is in the archives, in /opt on each machine. ls /opt to see these packages. In general, the debian packages executables can be found in /usr/bin and the /opt packages executables can be found in /usr/local/bin