Thanks to Carleton Detar for providing significant elements in this lab exercise!
Find the department's computer lab manual . Look at the lesson "Logging on and Doing Windows". Play with the navigation buttons in the lesson pages.
Learn how to resize, minimize and move windows.
(Lab Manual: Logging on and Doing Windows.)
Start a terminal window using the root window menu.
(Lab Manual: Logging on and Doing Windows.)
Read the first part of the Lab Manual lesson "Bare Bones" . From a terminal window, determine your "working directory" (think of this as your location in the Unix file structure) by typing pwd (print working directory) at the prompt. If you have just logged on to your machine or have just opened a terminal application, this directory is your "home directory". (Here it is assumed that your working directory is set to your home directory.)
The result of pwd may be a little long, but after the last slash character /, you should see your username. This long-ish thing, maybe of the form /u/course/myname, is called the "full path name" for your home directory.
You will be doing quite a few exercises in this class, so it is useful
to set up directories for organizing them. Let's do this by creating
a parent directory called "exercises", which will have subdirectories,
one for each lab exercise. So next, create a "subdirectory"
called exercises using
mkdir exercises
Check that this was successful by typing ls (the list command).
Move to directory exercises by typing the "change directory" command
cd exercises
Then type pwd to verify that you have moved your working directory
to exercises.
Type ls. The directory should have no files in it, as this command will indicate.
Create a new subdirectory within directory exercises called ex01.
Change your working directory to this new subdirectory and type pwd. Notice that Unix directory levels are delimited by the slash (/) symbol.
Try moving back to directory exercises using
cd exercises
Why does this fail? Answer: the cd command, when used in the
manner just shown, thinks you want to change to a subdirectory of
ex01 called exercises. The
directory ex01 has no subdirectories; indeed it has
nothing in it.
Return to your home directory using cd and the "full path name", that is, the thing that was printed out by the pwd command when your working directory was your home directory (e.g., /u/class/myname).
Note, in unix, all full path names begin with a slash /. In the examples above you used directory names that were not preceded by a /). Instead, you specified a "relative path name": that is a directory whose location is determined relative to the current directory.
Actually, you can get to the top of the whole unix file structure by
typing
cd /
Type pwd and an ls to see what file or directories
reside here. Don't mess with these! Return to your home by using a
sequence of cd commands with relative path names, e.g.,
cd u
cd course
...
Since getting to your home directory is so fundamental, there are a
bunch of ways to do it. The easiest is typing just cd all by
itself. Try using the special tilde ("twiddle") ~ character.
cd ~
cd
Note that other users' home directories can be accessed with the
~ symbol. For example, the instructor's account, called
p6720, has a home directory of ~p6720, so try
cd ~p6720
ls
Note that the tilde is an abbreviation for the full path to the home
directory, so any path beginning with tilde is actually an absolute
path.
Finally, you should acquaint yourself with ways to move
up the file hierarchy. Two periods, .., will move you up
one level. Experiment with this; also try
cd ../..
cd ../../..
cd ~/exercises du -k . > ex01/lab01Spaces are important in Unix, so pay careful attention to the spaces and the dot here. If you make a mistake, delete the file with the command rm ex01/lab01 and try again.
What did the above command do? There are several things to notice.
First the command du with a flag -k is a Unix "disk
usage" utility that tells you what directories and subdirectories you
have and what their sizes are in kilobytes. We won't be using it very
often in this course. The dot . for "current directory" tells
the du command to examine the directory tree descending from
the current directory, which should be your
directory exercises. Try running just this first part of the
command
du -k .
This time the command output is sent to the terminal
window. Then examine the contents of your lab01 file by
running the commands
cd ex01
cat lab01
Can you see that the > ex01/lab01 took what would have gone
to the screen and put it in the file lab01 in the
subdirectory ex01 instead? That is called output redirection.
Now that there is a file in place, try some of the variants of the ls command.
ls *
ls -l
ls -la
ls *0b1
ls lab?
ls lab??
This exercise has to do with file manipulation.
Use the cp command to copy the file file1 from the
~bromley/www/unix_emacs_intro directory to the directory.
ex01 that you created above. The syntax for cp is
cp filea pathb
where filea can be any filename (e.g., instead of filea
you could have something like /home/data/file.dat) and
pathb is a pathname (either a filename or a directory) (e.g.,
instead of pathb you could give the directory
~/asst01/). When pathb is a directory, the copied file
keeps the name of the original file. When pathb is a file, the
copied file has whatever name you give it.
Tip: Notice that for this exercise the first file
in the copy command is not in your local directory, so you need to
construct the full file name including the full path. So start with
the directory given above, follow with a slash and then the name of
the file in that directory. Then the second file in the copy command is
not in your home directory. You have a choice here. (1) You could
start by first going to the ex01 directory simply using a dot
. for current directory. Or you could specify the path
to ex01 from wherever your current directory happens to
be.
For more details on how to use the cp command
(or other shell commands), type
man cp
Push the space-bar to scroll down the "man page".
When you are done with man hit q to return
to your shell.
Do an ls to make sure the copy worked. Look at the file with the more or less command. (When done, typing q gets you back to your shell.) Also, look at it with the cat command.
Move (mv) (i.e. rename) file1 to the filename filea.
Copy all files of the form file? from directory ~bromley/www/unix_emacs_intro to your directory ex01. (As a good habit, you should use ls to see which files are in the source directory before you copy them over....)
Use cat to see what is in these files. Also
try out the grep command:
grep jjjj file1
grep nebula file?
grep NASA file*
Before finishing, run the following command to list the files and
directories you collected in your home directory and append the
listing to the bottom of your answer
file lab01.
cd ~/exercises/ex01
ls -l level* file* >> lab01
The -l is a the lower case letter "ell" and not a 1.
Please be sure you type two ">>" to append your result to the file.
What did this command do to the file lab01? If you aren't sure, run just the first part of the command: ls -l level* file*. Then run the command more lab01. Can you see that the double-arrow redirection >> lab01 caused the output of the ls command to be appended to the file lab01?
Please be sure you know what this command does and what it did to the
file, because to do the next exercise you will need to know which
lines you just added here.
Finally, remove these files with the rm command:
rm file1
rm -f file2
rm file?
Note the (possibly risky) use of the -f directive in the
second line.
Type
echo Hello WorldNotice echo sends the stream of characters "Hello World" back to your terminal (which is, by default, the "standard output"). Next, have that same stream of characters get put into a text file called hello.txt:
echo hello world > hello.txtHere, the > means "redirect" the standard output (stuff which would normally go to your terminal) to the file hello.txt. Verify that the file was created using the "ls" command, and use the "cat hello.txt" command to confirm that the file has the expected string of characters.
Now try the previous command again. It will fail because your operating system does not want you to overwrite a file which already exists. You can for the overwrite by replacing > with >! -- the "!" character (aka "bang") will force the overwrite:
echo HELLO WORLD >! hello.txtConfirm with cat that this worked.
Next revisit the command "grep" from the previous exercise. Type
grep HELLO hello.txtGrep goes through the file hello.txt looking for lines containing the word HELLO. Now just try
grep HELLONotice how things just sit there. Grep is waiting for a stream of characters, but there is no file listed as in the preceding case (with hello.txt). It is expecting to find the stream from the "standard input", which by default is your keyboard. Try typing random stuff, and every so often hit the ENTER/RETURN key. Notice how grep behaves if you type in HELLO as part of the "random stuff". To make grep quit, type CTRL-C (that is, hold down on the CTRL key while you hit the C key (no need to capitalize with SHIFT).
Now, we come to a pipe, symbolized by the | character. A pipe is a conduit which takes the standard output of one thing and dumps it into the standard input of another. Using the examples of echo and grep above, guess what these do:
echo HELLO WORLD | grep HELLO echo HELLO WORLD | grep goodbyeCreate a new file "lab01b" using the echo command so that the first line reads:
Hello, world.
[For an optional challenge, how would you "echo" a quotation, with the appropriate quotation marks? For example,
"Hello, world," said the Cat, "it doesn't matter.".Hint: the try preceeding a quote mark with a back-slash.]
Copy the file file.dat from the directory ~p6720/exercises/unix_emacs_II to your working directory. Use cat to confirm that it is a two-column (x-y) text file: You can peak at the beginning and end of the file using
head file.dat tail file.datTry
head -n 3 file.datto see its effect. The "-n" is a command option; this options (unlike some others) requires an argument ("3" in this case) for it to work.
Here is an equivalent way of using head or tail in conjunction with cat:
cat file.dat | headWhat will
cat file.dat | head | taildo?
Now try looking at the contents with "more" or "less". To scroll down hit the space-bar. To scroll back up hit "b". To quit, hit "q".
To learn more about these (and other) commands, try (for example)
man headThe "man" pages are a source of great wisdom. They put the "M" in "RTFM."
Sorting data, as in putting them in some sequence is often a useful thing to to. Notice that the 1st column of file.dat from the previous exercise is already sorted. compare
cat file.dat sort file.datto confirm, but you might want to use the "more" utility. You can sort the file according to its first column in reverse order using the -r option:
sort -r file.dat | head(piping through head just lets you get a sense of the first few numbers). Compare with
tail file.dat
Next try to sort using the second column:
sort -k 2 file.datUse the man page for sort for details on the -k option. The number 2 above just means 2nd column. Then do a reverse sort Note: you can string together commmand options, maybe:
sort -r -k 2 file.dat sort -rk 2 file.dat[What problem do you see with the sorting results? Hint: what is the difference between sorting and alphabetizing a list of numbers?] Go to the man page to find a command option to sort things numerically as opposed to alphabetically. [The letters "n" and "g" might be good to keep in mind...]
With the unix awk command, find the product x*y of for each line in your datafile:
awk '{print $1*$2}' file.datExplore some of the possible numerical operations of awk by trying to use sin, cos, exp, log, functions (e.g., cos($1+$2)). Using awk, confirm that the 2nd cal is just sine of the first...
[*If awk seems to ignore, say, trig functions, try ssh'ing to a different maching, like kepler....]
The single quotes around the first argument are needed so that the shell does not change anything before it passes the argument string to awk. Awk's second argument, the filename, tells awk where to get standard input. If the second argument is not present, awk expects input from the keyboard. Use awk with only one argument to find the product 0.12345679*9. Try this by using the "$1*$2" form above; you'll have to type in the numbers, separated by a space, then hit C-d (control-d) to signal to awk that you are done giving input. You can use awk as a simple calculator as follows:
awk 'BEGIN{print sin(4*3.14159265/3);}'With the BEGIN in this case, awk is not expected to process any input. It just evaluates the expression and quits. Try it.
The purpose of this exercise is to introduce you to the emacs editor
and to fix up your file for handing in.
Please see the introductory emacs section in
Lab Manual: Bare Bones.
Start up emacs from your shell using
emacs &
(or use the desktop Launch menu). Note the ampersand
indicates that emacs should run "in the background" and should not
tie up your shell command line.
Use the pull-down menu bar options ("Buffers" "Files" etc.) to open a new file called newfile. Try typing text into the file's "buffer" and saving it. Then, type cat newfile on your shell's command line and see that the file was saved as expected.
You should read through the Bare Bones lesson about basic emacs operation. You should start learning the emacs keyboard shortcuts ("hot keys") to free yourself from the mouse while editing. Some shortcuts are given in the pulldown menus. You will learn more from the emacs tutorial (see the "Help" pull down menu). For example, typing Ctrl-x Ctrl-f (abbreviated C-x C-f) opens a file which emacs uses to create a "buffer" that can be edited. If the file already exists, it will be loaded into your buffer. C-x C-s saves your current buffer to the file, and C-x C-c exits emacs.
When you are ready, use the emacs editor to edit the file lab01. Play.
Now we consider processes. Start up an emacs window by typing
emacs(type exactly this! no ampersands!). Note that your shell will be tied up running the emacs job. Suspend the job by typing ^Z (Ctrl-Z). The number that you see in the message that you job is suspended is the "job number" of this emacs process. Put the job in the background using "bg". Next kill the job using "kill" with a second argument set to "%" immediately followed by the job number. For example, use
jobsto see and confirm job numbers, e.g., "[1]" for job number 1. If you see more than one job, please ask if you are unsure why. Otherwise try
kill %1
Next, relaunch emacs and type "ps" to find the process id (PID) of your emacs job, and use "kill" to terminate the job with the PID.
Use a "ps aux" to print out information about all processes running on your computer. Since this might be a long unreadable list, pipe it through "grep" this file to get a list of your processes:
ps aux | grep yourname(assuming your username is "yourname"). Note that you can skip the creation of the tmp.4.tmp file by piping the output of "ps" to the input of "grep". Thus, try
ps aux | grep yourname(Recall from above: when grep has only one argument, it expects its input to come from standard input.) The "|" symbol links the input of grep to the output of ps.