Lab3

advertisement
CMPT 183 Lab # 3
Objective: At the end of this lab you will be able to











Learn about directories
List the contents of a directory
Make a directory
Change the current directory
Go to the parent of the current directory
Go to your home directory
Go to the parent of the current directory
Print the full name of the current directory
Remove files & utilize the wild card
Remove a directory
Display a file without using a text editor
Learn about directories
In UNIX, much like in Microsoft Windows, it is possible to create folders to
store files. In UNIX these folders are called “directories”.
Every user has a home directory, this is what you see when you first log on.
Within your home directory, it is possible to create as many other directories
as you want.
This can be especially useful for keeping track of lab work, homework, and
other files.
List the contents of a directory
In the first lab we learned about the commands dir and
two basic methods for listing the contents of a directory.
If you type
These are the
ls you should see this:
pegasus.montclair.edu% ls
HelloWorld.class
HelloWorld.java
If you type
ls .
lab2.txt
dir you should see this:
pegasus.montclair.edu% dir
total 6
-rw------1 zurawskij fall02
-rw------1 zurawskij fall02
-rw------1 zurawskij fall02
426 Jan 23 13:52 HelloWorld.class
211 Jan 23 13:52 HelloWorld.java
465 Jan 23 13:53 lab2.txt
Whenever you need to see what files you have in a directory try using these
commands.
Make a directory
To make a directory in UNIX you will need to use the mkdir command
followed by a name.
If you type
mkdir temp and then dir you should see this:
pegasus.montclair.edu% mkdir temp
pegasus.montclair.edu% dir
total 6
-rw------1 zurawskij fall02
-rw------1 zurawskij fall02
-rw------1 zurawskij fall02
drwx--l--2 zurawskij fall02
426
211
465
96
Jan
Jan
Jan
Jan
23 13:52 HelloWorld.class
23 13:52 HelloWorld.java
23 13:53 lab2.txt
23 2003 temp/
Anything listed with a slash (/) after the name is a directory.
Change the current directory
Now that we have created a directory, we need to get inside of it. This is done
easily in UNIX with the cd command.
If you type
cd temp and then dir you should see this:
pegasus.montclair.edu% cd temp
/home/students/zurawskij/temp
pegasus.montclair.edu% dir
total 0
Using this command you can get to any directory in the UNIX system. This
stands for change directory.
Go to the parent of the current directory
Once you are in a directory you can go “up” one level by using
If you type
cd ..
cd .. and then dir you should see this:
pegasus.montclair.edu% cd ..
/home/students/zurawskij
pegasus.montclair.edu% dir
total 6
-rw------1 zurawskij fall02
-rw------1 zurawskij fall02
-rw------1 zurawskij fall02
drwx--l--2 zurawskij fall02
426
211
465
96
Jan
Jan
Jan
Jan
23
23
23
23
13:52
13:52
13:53
14:00
HelloWorld.class
HelloWorld.java
lab2.txt
temp/
This is called changing to the parent directory. We say this because the
original directory contains another directory, much like a parent and a child.
Go to your home directory
No matter where you are, it is always possible to go to your original home
directory by using a simple command.
If you type
cd ~ and then dir you should see this:
pegasus.montclair.edu% cd ~
/home/students/zurawskij
pegasus.montclair.edu% dir
total 6
-rw------1 zurawskij fall02
-rw------1 zurawskij fall02
-rw------1 zurawskij fall02
drwx--l--2 zurawskij fall02
426
211
465
96
Jan
Jan
Jan
Jan
23
23
23
23
13:52
13:52
13:53
14:00
HelloWorld.class
HelloWorld.java
lab2.txt
temp/
This method is very fast, especially if you are deep inside of another directory.
Print the full name of the current directory
If you forget where you are located in a directory structure, it is possible to
display the full path.
If you type
pwd you should see this:
pegasus.montclair.edu% pwd
/home/students/zurawskij
This command is called present working directory.
Remove files & utilize the wild card
From time to time your directory may get cluttered with files, and you may
choose to delete them. In Microsoft Windows it is possible to drag things to
the ‘recycle bin’, or on a Macintosh things must be sent to the ‘trash’. In UNIX
there is nothing like that, but there is a command.
If you type rm HelloWorld.class and then
should see this:
y
and then
dir
you
pegasus.montclair.edu% rm HelloWorld.class
rm: remove HelloWorld.class (yes/no)? y
pegasus.montclair.edu% dir
total 4
-rw------1 zurawskij fall02
211 Jan 23 13:52 HelloWorld.java
-rw------1 zurawskij fall02
465 Jan 23 13:53 lab2.txt
drwx--l--2 zurawskij fall02
96 Jan 23 14:00 temp/
Class files are not necessary to keep, anytime you run the javac command a
new one will be created. From time to time you should delete these files. It is
also possible to delete multiple files at once using what is called a ‘wild card’.
If you type
rm *.class and then dir
pegasus.montclair.edu% rm *.class
No match
pegasus.montclair.edu% dir
total 4
-rw------1 zurawskij fall02
-rw------1 zurawskij fall02
drwx--l--2 zurawskij fall02
you should see this:
211 Jan 23 13:52 HelloWorld.java
465 Jan 23 13:53 lab2.txt
96 Jan 23 14:00 temp/
Be very careful using this, it does not prompt you for a yes or a no. Only use
this to get rid of .class files.
In addition to deleting things, the wild card (*) can be used for other purposes
as well. The basic idea is that the
to omit.
* can replace any letter or letters you wish
Remove a directory
Directories must be removed from the system in a special way, the rm
command will not work.
If you type
rmdir temp and then dir
pegasus.montclair.edu% rmdir temp
pegasus.montclair.edu% dir
total 4
-rw------1 zurawskij fall02
-rw------1 zurawskij fall02
you should see this:
211 Jan 23 13:52 HelloWorld.java
465 Jan 23 13:53 lab2.txt
Before you can delete a directory it must be empty. If you happen to get an
error message be sure to check and see if the directory is empty.
Display a file without using a text editor
Lastly, it is possible to view a file (or files) without opening an editor, like pico.
There are several ways to do this, I am going to high light two of them.
If you type
cat HelloWorld.java you should see this:
pegasus.montclair.edu% cat HelloWorld.java
// File name: HelloWorld.java
// Author: Jason Zurawski
// Date: January 23, 2003
public class HelloWorld
{
public static void main (String[] args)
{
System.out.println ("Hello World!");
}
}
The command cat stands for concatenate, the real use of this file is to
combine files together, but it works well for displaying the contents of a file as
well.
If you type
more HelloWorld.java you should see this:
pegasus.montclair.edu% more HelloWorld.java
// File name: HelloWorld.java
// Author: Jason Zurawski
// Date: January 23, 2003
public class HelloWorld
{
public static void main (String[] args)
{
System.out.println ("Hello World!");
}
}
The command more is useful for viewing large files. After printing one screen
it will pause until the space bar is pressed. This way it is possible to read a
document at your own pace.
To Hand in:
If you haven’t printed your assignment from last week (lab2.txt) you should
do so now:
lpr –PRI108H lab2.txt
Do the following commands exactly as they appear:
cd ~
script lab3.txt
mkdir lab3
cd lab3
dir
pwd
mkdir child
cd child
dir
pwd
cd ..
rmdir child
cd ..
rmdir lab3
dir
pwd
cat lab2.txt
exit
lpr –PRI108H lab3.txt
Download