CS 151 - Introduction to Data Structures
Lab 2
Unix
Using Unix
Some important UNIX commands:
- cd
Change Directory
Example: cd cs151 (change to the directory cs151) (Note that this will not work if you
do not have a cs151 directory.)
Example: cd .. (change to the directory one above where you are, for instance is you are in
/home/xena/cs151, then cd .. would put you in /home/xena
Example: cd with no argument. This returns you to your home directory
- ls
LiSt Files
Display a list of all the files in the directory. It is very common to use the ls command immediately after
cd.
Example: ls list the files in the current directory
Example: ls cs151 list the files in the directory cs151 (if it exists)
- mkdir
MaKe DIRectory
Create a directory
Example: mkdir cs151 make a directory named cs151 Actually do this in your home directory.
if you have not already done so. To be sure you are in your home directory enter the command
cd (I often get back to my home
directory when I mean to be somewhere else. Annoying, but at least then I know where I am)
- pwd
Displays the current working directory. In other words this command shows you where you are.
- cp
Copy a file
Example cp a b make a copy of the file name "a" in the currect directory into a file named "b"
in the current directory. Note that if "b" existed before the cp command, then it will be overwritten
cp /home/gtowell/Public/151/L02/microtrades.txt m.txt make a copy of the file "microtrades.txt
from the directory "/home/gtowell/Public/151/L02/" into the currect directory in a file named m.txt
Lots of riffs on this general idea are possible with cp
- scp
Secure copy a file
The important difference between cp and scp is that you can use scp to copy between
machines.
Example scp UNIXLOGIN@MACHINEiD:a b make a copy of the file named "a" that is available on the
machine MACHINEiDin the home directory of user UNIXLOGIN into a file named "b"
in the current directory. Note that if "b" existed before the cp command, then it will be overwritten
cp /home/gtowell/Public/151/L02/microtrades.txt m.txt make a copy of the file "microtrades.txt
from the directory "/home/gtowell/Public/151/L02/" into the currect directory in a file named m.txt
Example scp a UNIXLOGIN@MACHINEiD:b Make a copy of the file a in the current directory
and put it in the home directory of UNIXLOGIN on MACHINEiD in the name b.
- mv
Move (and/or) rename a file
Example: mv a b rename the file named "a" to "b". As with cp, if there was a file nmed "b", it
will be overwritten.
mv /home/YOU/aa aa Move a file named "aa" from your home directory to the current directory and
keep that file named "aa".
- rm
remove a file
Example: rm aa Permanently delete the file "aa"
- cat
show the entire contents of a file to the terminal. ("cat" was originally an acronym for CAthode Ray
Tube, which might give you a feel for how long this command has existed.)
Example: cat aa the entrire contents of aa will show on the screen
- javac
JAVA Compile
Compile a java program.
Example: javac HW.java
This will compile the java code int he file HW.java. If there are no
errors, it will create a file HW.class.
- java
Run a java program.
Example:java HW
Run the java program named HW. This requires that the file HW.class is present in the current directory.
Execute the above unix commands, put answers to the questions to their right
cd |
What directory are you in?
| |
| What is the contents of this directory?
| |
cd / |
What directory are you in?
| |
| What is the contents of this directory?
| |
cd ~ |
What directory are you in?
| |
| What is the contents of this directory?
| |
cd /home/YOU |
(Replace YOU with your login name) What directory are you in?
| |
| What is the difference between this command and the previous one?
| |
ls /home/gtowell/Public/151 |
What did this command do?
| |
cat /home/gtowell/Public/151/L02/HW.java
cat /home/gtowell/Public/151/L02/HW.class
|
What did the second command do? Is the file HW.class readable? One sentence: explain.
| |
A good understanding of these few UNIX commands is about all you need to this course. (Many Unix users know little
more than these.)
Lets finish this lab by copying files between your computer and the CS Unix computer. The command to copy between
machines is scp. "scp" is "secure copy"; it basically extends the cp command so it can be used between
machines.
First make a directory on your machine for this lab. The directory should be within the cs151 directory. (use cd
and mkdir as needed).
Next, move into the diretcory you just made
Copy a file from Linux to your local directory. (Note that if you are doing this all from within linux you can
use cp rather than scp (ask me))
scp YOUR_CS_LOGIN@goldengate.cs.brynmawr.edu:/home/gtowell/Public/151/L02/microtrades.txt localmicro.txt
You will need to enter your password. This command copies a file from my Public directory onto your computer. You
will do many such copies this semester. You will learn to love (or hate) ssh and scp.
Edit the file using your favorite text editor (e.g. TextEdit on Mac, Notepad on Windows or you can use VSC). The
major point here is just to find this file on your machine using a standard GUI tool.
Finally, copy this file back to Unix, but into your directory.
scp localmicro.txt YOUR_CS_NAME@goldengate.cs.brynmawr.edu:cs151/lab02/remotemicro.txt
You should be able to use a combination of ssh, cd ls, and cat to confirm that the file remotemicro.txt exists in
the in
the Unix directory structure you created earlier.
Send email to gtowell151@cs.brynmawr.edu with the following:
- A completed table for the questions in the UNIX section
The easiest way to do this might be to use your phone and take pictures of the completed table and your screen
showing answers. Alternately, if you typed your answers, just send them. These are just suggestions for how you
could turn things in. Feel free to be creative. All I require is legibility.