6.0 Basic Unix Commands
The goal of this section is to give a very brief overview of some of the
simplest Unix commands. Upon completion of this section, you should be
able to:
-
List the contents of your directory.
-
Move through your file system.
-
View a text file.
-
Print a text file.
-
Copy a file.
-
Delete a file.
All of the commands demonstrated in this section are Unix commands that
should be entered in an Xterm window. If you don't
have an Xterm window up, you'll
need to start one by selecting Xterm from the Workspace Menu.
Each of the commands demonstrated in this section should be completed with
the <Return> key.
6.1 Listing a Directory's
Contents
As a user of the departmental computer system, you have been given a private
disk area for storing mail, programs, etcetera, at your discretion. You
file system has been supplemented with a few basic default files to provide
your account with standard departmental functionality.
To view the files currently in your disk area, type the ls
command...
at the Xterm prompt. You should see something similar to:
Mail/ bin/ hello
The first two entries, Mail/ and bin/
are directories, the last entry is a file.
The definitions of the two entities can be quite complex, but for
now, the standard Folder Analogy will suffice:
A directory is much like a manilla folder. Within
this folder can be placed a number of individual files, or even whole other
folders. In fact, your complete file system is only a small folder within
the total users folder which resides in yet another folder.
The ls command lists the contents of
a directory which, in the case above, was your personal directory (know
as your "home directory"). This command can accept a number of
options (known as arguments) which can change the way the output looks.
For example, typing:
Will display:
total 3
drwx------ 3 fdrebin 512 Aug 14 14:02 Mail/
drwxr-x--- 2 fdrebin 512 Oct 13 1992 bin/
-rwxr-x--- 1 fdrebin 782 Oct 13 1992 hello
The -l option indicates the "long
format" should be used to display the current directory. Briefly,
this describes the file type and permissions, the number of references
to a given file, the owner, the size, the date and time it was last changed,
and the name of the file. The implications of all this information will
be discussed later in this course.
6.2 Moving into a New Directory
As mentioned earlier, ls performs its
task upon the current directory (also known as the Present Working Directory).
To move into another directory, the change directory command (cd)
can be given the argument of a new directory like so:
fdrebin@blowfish> cd Mail
As mentioned before, Unix is a case-sensitive operating system, so the
command must be entered exactly as shown above. If you attempt to use all
lower case, the machine will display an error message.
Upon hitting <Return>, you will
be moved into the Mail directory. You
can confirm this by doing an ls as described
above. You should see something similar to:
aliases context inbox/
nsmail/
components drafts/ include sent/
You may have noticed that the title at the top of your Xterm window has
changed to show "blowfish:/usr/users/fdrebin/Mail"
now. This is a convenient way to map your location.
Looking at the results of the ls command,
you will see a directory named nsmail.
This is where the Netscape mailer stores your E-mail. To move into this
directory, type:
fdrebin@blowfish> cd nsmail
Note the change again in the title bar. You can also determine your current
location by typing:
Which stands for Present Working Directory. You will probably see something
like this:
/usr/users/fdrebin/Mail/nsmail
The first portions is the expanded name of your home directory. While there
is some rather important information here, for now, we can safely ignore
it. The last two portions give the relevant information; that you are currently
in the Mail/nsmail directory of your
file system.
I'm sure now you're ready to move back to your "home" directory.
There are several ways we could accomplish that. One way is to backup one
directory at a time using a command like:
/usr/users/fdrebin/Mail/nsmail
The .. following the change directory
(cd) command indicates the "parent"
directory or the directory one level above the current directory. Now the
Present Working Directory looks like this:
/usr/users/fdrebin/Mail
If we issued the same cd command again
we'd wind up one level higher, back at the home directory.
An easier, more convenient way to return your home directory is use a command
like:
/usr/users/fdrebin/Mail/nsmail
But wait, we didn't specify where to change directory to. That's right.
If you don't specify any arguments to the cd
command, it will automatically take you to your home directory. So now
the present working directory looks like this:
/usr/users/fdrebin
6.3 Viewing a Text File
What good is having a bunch of files if you don't know what's in them?
No good at all, but ahh, there is a way. You knew there would be.
Let's look back at what is in our home directory. If your not in the home
directory use the cd command as explained
in the prior section to return to your home directory.
Let's look again at what files you have in your home directory.
Mail/ bin/ hello
What's that hello file all about? The
only way to find out is to see what's in it.
There's a few ways to view the contents of this file. Here's one:
fdrebin@blowfish> cat hello
Will result in the contents of the hello file being display, from
beginning to end, on your display. The cat
command stands for concatenate and can be used to concatenate two or more
files together to form a single file. Our use of the command here, on a
single file, has the effect of displaying the file to the screen.
Notice though that you can't read the top line(s) of the hello file.
The top line(s) scrolled off the screen because the contents of the file
didn't fit within the window. What to do? You could resize the window as
discussed earlier in this tutorial but resizing the window all the time
is a pain, and it wouldn't work for really long files. Here's a better
way:
fdrebin@blowfish> more hello
This command will result in the contents of the hello file to be
displayed one screen at a time. When the contents of the file fills up
a screen, the display will be paused to give you a chance to read it. You'll
notice that the last line of the display will look something like this:
--More--(95%)
This lets you know you haven't reached the bottom of the file yet and that
you've seen 95% of the text. (your percentage may be different) When you
are ready to see another screen full, or the rest of the file, simply press
any key on the keyboard to advance the text; the <Spacebar>
works for me.
6.4 Printing a File
You can't carry around a Unix workstation with you all the time to view
your files (at least not yet). So what do you do if you want to be able
to see the content of your files outside of the computer? You print it
out on paper of course.
The command to print a file is lpr.
By the way, lpr, is short for Line Printer which is a rather an archaic
term for describing a printing device. The lpr
command is used like this:
fdrebin@blowfish> lpr hello
That would print out the hello file to the default printer. Default
printer? What's that? When you login at a particular workstation, the system
attempts to set your default printer to whatever printer is closest to
where your are working. So if you are working in the computer lab at Griffin/Floyd
your default printer should be the printer in the lab known as statprn.
You can redirect your printing to any other printer on the network by specifying
its name as part of the lpr command
like this:
fdrebin@blowfish> lpr -P oysterprn
hello
Notice the use of the -P argument just
before the name of the printer.
Printing is arguably the most expensive task a computer does. With that
in mind, you should consider limiting your printing to those files that
are necessary. Our computer system is equipped with a number of sophisticated
programs for viewing a wide variety of documents online, without needing
to print.
6.5 Piping output to a Printer
Often, the information you wish to print is either not in a file, or, is
not in a location easily accessible. Unix provides an easy method for passing
the output of one command as the input of another. This is known as piping.
The pipe symbol (|) is used to tell
Unix to pass output to a new program. Here's an example:
fdrebin@blowfish> cat hello | lpr
This will print the contents of the hello file just as we did before.
Earlier we showed how to use the cat
command to send the contents of a file to the screen so we could view it.
In this example we show how we can use the pipe symbol to redirect output
from the cat command as input to the
lpr command. Although its not obvious
in the example above, you will find many uses for the pipe symbol. Of course,
piping is not restricted to just printing files. It may be used in conjunction
with a number of programs, which will not be discussed here.
6.6 Copying a File
Another useful command allows you to make a copy of a file. There are many
reasons why you may want to copy a file. You may want to copy files that
are important to you, or copy a file just before you make any major changes
to it, or to simply copy a file into a subdirectory.
The command for copying a file is cp,
which of course is short for copy. For the sake of an example, let's copy
the hello file using the command:
fdrebin@blowfish> cp hello hello.bak
This creates a new file called hello.bak. If you list the contents
of your home directory now it should look something like this:
Mail/ bin/ hello hello.bak
Notice the new file.
You can include directory information as part of the copy destination.
So if you wanted to copy the hello file to your bin directory you
could issue a command like this:
fdrebin@blowfish> cp hello ~/bin/hello.bak
This tells the copy command that you want to copy the file into the /bin
directory off our your home directory.
The "~" (tilde) is a shorthand
method of writing your home directory, and the "/" is
used to separate the names of subdirectories off of this main directory.
So, ~/bin references the /bin directory
one level below your home directory. You will find the ~/
shortcut a handy to use whenever you need to reference your home directory.
To verify that the file was copied correctly you can do something like:
fdrebin@blowfish> ls ~/bin
hello.bak
6.7 Deleting a File
Now that we've left a few extra files lying around your file system, its
time to clean them up. It is important to keep in mind that disk space
is a limited resource. In the course of your computer use, it is important
to remove unneeded or obsolete files in a timely manner. This is true for
mail messages, programs, data files, etcetera.
The command we use to delete files is rm,
which is short for remove. Looking at the files in our home directory we
see:
Mail/ bin/ hello hello.bak
We don't need that hello.bak file there anymore so let's delete it using
a command like this:
fdrebin@blowfish> rm hello.bak
which will remove hello.bak file from our home directory. Another
ls command will verify it.
What about that file we copied to ~/bin ?
We probably should delete that one also. We can do that easy enough with
a command like:
fdrebin@blowfish> rm ~/bin/hello.bak
If we did an ls for the ~/bin
directory, like we did earlier, we would see that the file is now gone.
One note about deleting files. In a Unix environment, if you accidentally
delete a file there is no way you can easily undelete it. That doesn't
mean you're doomed. The Systems Manager performs regular daily backups
of each user account. If you've inadvertently deleted a file then you should
contact the Systems Manager and ask him to restore it for you.