A brief introduction to UNIX and xemacs


Obtain an account

You will need an account in this course for the computer mentor.cc.purdue.edu that is administered by ITaP-- formerly PUCC (Purdue University Computer Center). You will login into this computer remotely to compile your programs.

Before you go to your PSO, make sure that you know the login and password for mentor. This is the same account and password that you use for the PCs in the Purdue computer labs. If you don't know your ITaP login and password go to MATH 231 to get it before your PSO.
 

Login into mentor

Click on the icon on the screen that is labeled "mentor". This will connect you to the computer mentor.cc.purdue.edu. Type your ITaP login and password.

Change your current shell to tcsh

The default shell for your mentor account is called "csh" (pronounced Ce shell). This shell does not have line editing, so we recommend you to switch to tcsh (pronounced Te Ce shell) instead. Type the following to switch your current shell to tcsh:
 
mentor.cc.purdue.edu% chsh
Changing login shell for <your-login> on *all* ACMAINT hosts.
Old shell: /bin/csh
New shell [/bin/csh]: /usr/local/bin/tcsh
Now logout typing
mentor.cc.purdue.edu% exit
Login into mentor again. After you login, you should obtain a ">" prompt that is the tcsh prompt.

Simple UNIX commands

The most commonly used UNIX command is "ls".  Type:
> ls
The command "ls" with no arguments lists the contents of the current directory. If you want to see the details of the files, type:
> ls -al
The command "pwd" prints the directory path of the current directory. This current directory is the directory where the shell is currently in. Type:
>pwd
/home/lookout/<some-letter>/<login>
The directory path  printed is a list of the directories necessary to reach the current directory from the root directory.

To create a new directory (for example a directory called cs354), type:

> mkdir cs354
> ls
To change the current directory to the cs354/ directory, type:
> cd cs354
> ls
You will notice there are no files in there. Now type:
> ls -al
You will see two files: "." and ".." One dot means the current directory, and ".." means the parent directory.

To change the current directory to the parent directory type:

> cd ..
The home directory is the directory where all your files are stored and is where the current directory points to when you login. To change the current directory to your home directory type "cd" with no arguments. Type:
> cd
> pwd
> ls
Sometimes you would like to go back to the previous command. To do that in tcsh type the <up arrow> (the key that has the arrow pointing upwards). Type:
> <up arrow>
several times and you will get the last commands. To execute the current command displayed type <ENTER>. You may also type the <down-arrow> to go to the next command.

For example, type <up-arrow> until you find the "ls -al" command and then type <ENTER>.

If you have made an error while typing, you may use the <left-arrow>,  <right-arrow>, or <backspace> to move the cursor and correct it.

You don't need to type all the characters for a file or command. You may use <tab> and <ctrl-d> to complete the name of a file.

For example, type:

> ls /usr/j<tab>

This will complete to "ls /usr/java"

If the prefix matches several files, you may see a list of files matched by typing:

 > ls /usr/p<ctrl-d>

This will show all the files that start with "/usr/p"

Also you can reffer to a list of files by using the character "*". The "*" character in a file name represent one or more characters. For example:

> ls /usr/lib/*.a

Lists all the files that end in ".a" in the directory /usr/lib.


 

Summary of UNIX commands

ls Lists the contents of the current directory
ls -al Lists the contents of the current directory in detail
pwd Prints the path of the current directory
mkdir new-directory Create a new directory
cd directory-path Change to a directory
cd Change to home directory
cp old-file new-file Copy a file from old-file to new-file
mv old-file new-file Rename a file from old-file to new-file
rm file Remove a file
echo "message" Outputs the string "message" 
<up-arrow> Get previous command
<down-arrow> Get next command
<left-arrow>, <right-arrow> Move cursor to the left or rigth.
<tab> Path-completion

Running xterm

The xterm program is similar to telnet but has other flexibilities such as window-resize. To run xterm type:
> /usr/openwin/bin/xterm -sb &
The "-sb" option is for scroll-bar and the "&" is to run xterm in the background. Running a program in the background means that the shell will return a prompt immediately and will not wait until the program completes.

You may open as many xterm windows as you want using the command above.

Running xemacs

We strongly recommend you to use xemacs to edit your files. xemacs is a professional editor for the UNIX environment. To run xemacs type the following from the xterm window.
>/opt/SUNWspro/bin/xemacs &
This command will run xemacs in the background. You will see the xemacs window appear on your screen.

You can use xemacs using the pull-down menus like other editors with a graphical user interface. If for some reason you are stuck in the prompt at the bottom line type <ctrl-]> More information on xemacs can be found in xemacs-tutorial.txt.

Copying and pasting across windows

You can use your mouse to copy and paste across windows as follows. Highlight the text you wish to copy using the left mouse button. Move the mouse to the window in which you wish to paste. Make sure the cursor is at the position where you wish to paste (click the left mouse button to reposition the cursor, if necessary). Now you can click the middle mouse button to paste the highlighted text into the other window!

Finding other UNIX users

To see the users who are currently logged in in mentor type "who"
> who
Execute this command and find your name in the list.

Using the UNIX file system

The UNIX file system is a hierarchy of directories that start with the root directory "/"

You may list the contents of this directory typing:

> ls /
There are some important directories such as:

/dev                       All the devices of the machine (terminals, printers etc.)
/etc                       Administration
/usr/include               Header files used for "C" programs
/usr/bin                   Commands

The home directory

Every student with an account has a home directory. The home directory is a diectory reserved for you where you will store your programs and homeworks. When the special character "~" is at the beginning of an file name, it refers to the home directory of the user whose login name follows the "~".

Try

> ls ~
> ls ~/cs354
> ls ~cs354   - Lists the cs354 home directory

Using wildcards

When the special character "*" appears in the argument of an UNIX command, it will be expanded to all the files that match that argument.

For example, type:

> cd /bin       - Go to /bin directory
> ls            - Lists all the files in /bin
> ls m*         - Lists the commands that start with m
> ls *ma*       - Lists all the files that contain ma
> ls *ma* *ta   - Lists all the files that contain "ma" or end with "ta"
Now try to lists all the files that start with m and that contain g.

Wildcards do not match files that start with a "." . If you want to see those files, you will have to either use "ls -al" or match those files explicitly.

Type:

>cd     - Goes to home directory
>ls     - Lists files in home directory (there are no files that start with "."
>ls -al - Lists all files, including the ones that start with "." and also prints timestamp
>ls .*  - List all files that start with "."
>echo .* - Prints to the screen the names of the files that match .*

The .login file

You will see in your home directory that there is a .login file. This file contains a list of commands that are executed every time that you login.

Summary of directory commands

>pwd         - Gives the current directory
>cd          - Goes to the home directory
>cd ..       - Goes to the parent directory
>cd <dir>    - Goes to a specific directory <dir>
>mkdir <dir> - Creates a new directory
In your home directory, create the following directory structure:

      ~/cs354/lab1/ ---  that contains

       subdir1/   subdir2/

    subdir1/ contains

       subdir11/ subdir12/

    subdir2/ contains

       subdir21/ subdir22/
 

Redirecting standard output to a file

Every command in UNIX has a "Standard Input", "Standard Output", and a "Standard Error". By default, "Standard Input" is the keyboard, and, "Standard Output" and "Standard Error" are the computer display. However, the input and outputs could be files. In this section you will see how to redirect input and outputs to files.

To redirect the standard output of a command to a file you need to add "> filename" at the end of the command. For example, to write the list of files in your home directory to a file "ls-out" type:

cd
ls > ls-out
You can edit the file ls-out to see its contents. Alternatively, you can use the command "cat filename" to print the contents of the file to the screen. For example to print the contents of  "ls-out" type:
cat ls-out
Also, it is possible to append the output of a command to an existing file by adding ">> filename" at the end of the command. For example, type:
ls >> ls-out
ls >> ls-out
cat ls-out
Alternatively to cat you may use more. The difference between these two commands is that for large files cat does not stop and more will prompt after every page of text displayed. When using more type <space> to go to the next page.

Type:

more ls-out

Redirecting both standard output and standard error

In UNIX you may redirect the "Standard Output" and  "Standard Error" to two different files. However, most of the time you would like to redirect both to a single file. To do this add ">& filename" to create a new file or ">>& file" to append to an existing file.

For example, try listing a non-existing directory and send the output to "errout".

ls none-existing > errout
This command will print an error to the screen instead of the "errout" file. To redirect both the output and the errors of the command to "errout" type:
ls none-existing >& errout
cat errout
ls >>& errout
cat errout

Redirecting input

You can also redirect the input of a command to a file. This is often done by the TAs during the grading of your projects.

The input of the command "cat" with no arguments is the standard input. You can redirect the standard input of a command by adding "< filename" at the end of the command. For example the following two commands will display the contents of the file ls-out:

cat ls-out
cat < ls-out

The grep command

The command "grep word filename" displays the lines of a file that contain "word". Go to the directory ~/cs354/lab2 directory and print all the lines that contain "System" in the file EggBasket.java:
cd
cd cs354/lab2
grep System EggBasket.java
To print the lines that contain the word "Total" in the file EggBasket.java type:
grep Total EggBasket.java
To print the lines that contain the word "System" in all the files in the current directory type:
grep System *
In this case "*" matches the file names EggBasket.java and Hello.java.

Using pipes

In UNIX, you can feed the output of a command into the input of another command by separating them with the character "|" to create complex commands. For example, to display all the lines of the file EggBasket.java that contain both the word "System" and the word "baskets" you can type:
grep System EggBasket.java | grep baskets
In this command line the output of "grep System EggBasket.java" goes to the input of the command "grep baskets".

Also you may send the output of this command to a file:

grep System EggBasket.java | grep baskets > out
cat out

Using aliases

You may create an alias for a complex command to reduce the number of characters you type. When you want to execute the command you just type the alias. For example, you can create an alias called "ll" for the command "ls -al" that lists all the files and the timestamps of a directory:
> alias ll ls -al
> ll
Now typing "ll" will be equivalent to typing "ls -al".

The alias you have created will go away when you logout. If you want to make the alias permanent, you will have to add the alias command to your ~/.login file. If this file does not exist, you can create one.

To remove an alias use "unalias":

> unalias ll
> ll

Environment variables

The shell has a list of environment variables. To print the list of environment variables type:
> printenv
or to print a single variable type "printenv varname". For example, to print the value of the variable PATH type:
> printenv PATH
The most important variables are the following:

PATH - It is a list of colon separated directories where the commands typed are found. The shell will search the command in each directory until found or the end of the list is reached. By default the PATH variable includes the directory /bin.

DISPLAY - It is the Internet address of the computer where the x-windows applications are displayed. By default it is the IP address computer where you login.

HOME - it is the home directory.

PWD - It is the current directory.

SHELL - It is the shell program you are currently using.

Type the following:

printenv DISPLAY
printenv HOME
Also you can use the variables as part of a command or argument by typing $ before the variable. For example, to print the contents of your home directory you can type:
ls $HOME
That is equivalent to typing:
ls ~
You can also change the value of variables using "setenv VAR VALUE". For example, to set the value ~/cs354/lab3to variable lab3 you can type:
setenv lab3 ~/cs354/lab3
printenv lab3
cd $lab3
ls
In this way, you may use $lab3 everywhere you need the lab3 path.

You may also append a value to an existing environment variable by setting the variable and adding the variable to the value. For example, you may add the directory /opt/SUNWspro/bin to your PATH environment variable in the following way:

(Note: The command "which command" tells you in which directory a command was found.)

 Type:

printenv PATH
which xemacs      - It should print that xemacs was not found.
setenv PATH /opt/SUNWspro/bin:$PATH  - Adds xemacs dir to PATH
printenv PATH
which xemacs      - It should print that xemacs was found.
xemacs &
If after executing "xemacs &" the shell tells you that xemacs was not found, then the variable was not correctly set, try again. Also, it may be possible that you have already set up an alias for xemacs. You may unset the alias by typing "unalias xemacs" and try the previous steps again.

To change the value of the PATH variable permanently  you need to modify your .login file.

Modifying .login

Using xemacs open the .login file that is your home directory. Add the following line at the very end of the file:
setenv PATH /opt/SUNWspro/bin:/usr/openwin/bin:$PATH
Make sure that this line is spelled correctly in your .login file. Save the file and exit. Logout from the xterm and the telnet session. Then login again into mentor by clicking on the mentor icon, login, and type:
> xterm -sb &
> xemacs &
You will not need to type the directory of these commands anymore.

Running commands in the background

By default the shell will wait until a command finishes before printing the next prompt. However, in some cases, you don't want the shell to wait. That is the case when you run xemacs and xterm. For these programs you add the character "&" at the end of the command to make the shell and the command run concurrently, that is, at the same both run at the same time. This is called running a command in the background. The default is that the shell waits until the command completes. This is called running a command in the foreground, and it is the default.

When running a command in the background the prompt is printed before the command completes. You may need to type <Enter> to see the shell prompt again.

You can run any process in the background. For example:

> ls /dev     -Runs ls in the foreground (default)

> ls /dev &  - Runs ls in the background


This tutorial has been adapted from a similar tutorial on Gustavo Rodriguez-Rivera's page.