Anda di halaman 1dari 7

Page 1 of 7

EE1030
Introduction to UNIX
Salt Lake Community College
Department of Engineering
(Electrical Engineering) MHM

ASSIGNMENT #3 UNIX SHELL COMMANDS

Unix Shell Commands


In this lesson you will be introduced to a variety of new Unix shell commands, and will learn
about some features of the Unix TC-Shell. This is the last lesson specifically about Unix.

File Protection
Unix will allow you to control who has access to read, write, and execute your files and
directories. These access controls are implemented with file permissions, which you can change
using the chmod command, group ownership, which you can change using the chgrp command.
Before we begin, let’us look at the existing files and their status.
Try: ls -1 ( This is one, Copy and Comment )
Now
Try: ls -l ( This is small L, long listing, Copy a few lines and comment)
Permissions. You may noticed , you have a listing of files or directories similar to to the followings :

total 12
-rw-r--r-- 1 smartpe smartpe 2 Jun 12 15:57 smartpe_a
-rw-r--r-- 1 smartpe smartpe 1778 Jun 12 16:06 smartpe_a1
drwxr-xr-x 3 smartpe smartpe 512 Jun 14 14:17 smartpe_aa1
-rw-r--r-- 1 smartpe smartpe 20 Jun 12 16:10 stuff1
-rw-r--r-- 1 smartpe smartpe 0 Jun 12 16:08 stuff_a
-rw-r--r-- 1 smartpe smartpe 36 Jun 1 23:50 test1
-rw-r--r-- 1 smartpe smartpe 14 Jun 9 18:00 test2_a

The first field on each line shows you the permissions for that file or directory. Each field is
composed of a sequence of characters with the following meanings:
Page 2 of 7

Symbol Meaning

- privilege denied

d file is a directory

r the file can be Read

w the file can be Written

x the file can be eXecuted

Why is the same symbol sometimes repeated multiple times in a single field? It turns out that you
can separately control what the user who owns the file can do to that file, what other members of
the group can do to a file, and what any other user can do to a file. (We’ll explain what groups are
shortly.) We can break this potentially confusing field into four separate parts:

directory user group other

d rwx rwx rwx

The first symbol indicates whether or not the entry is a directory. (You can’t change this.) Then
there are three groups of three symbols each, showing the read, write, and execute privileges for
the user who owns the file, the group, and others.

Changing Permissions. You have complete control over access to every file that you own. Let’s
look at how to change protection.

For example, you cold not t have permission to write (modify) ``smartpe_a1" if the w was “-“ ie
-rw-r--r-- was -r-r--r-- .'
'If you want to change this permision, you need to use the chmod command
to change the permissions. Here is an example of the chmod command:
[sophist/home/smartpe]$ chmod u-w smartpe_a1
Try: chmod u-w smartpe_a1 ( Remember this was your file, and smart was your last name)
Now
Try: ls -l smartpe_a1 ( Copy and comment about the w)
You have noticed that, now, the write permission is not permitted in the smartpe_a1 file. You can
change it back to write permission using the command
Page 3 of 7
chmod u+w smartpe_a1

Try: chmod u+w smartpe_a1

Now

Try: ls -l smartpe_a1 ( Copy and comment about the w)

The first argument to chmod specifies the permissions you are changing. the ‘‘u’’ indicates that you
are changing permissions for you, the ``+' 'indicates that you are adding a permission, or “ -“ not
giving a permission, and the ``w' 'indicates that you are changing the write permission. The
following argument(s) list the file(s) for which you want to change permissions. (Run this
command in your Unix Shell window now.) Now you (but no one else) will be allowed to write the
file. Verify that by listing the directory again.

Here is a guide to specifying the permission parameter to chmod:

. First determine who you want to change permissions for. Your options are yourself (u), the
group (g), and other users (o). You can specify any or all of these three.

. Next decide whether you want to add or revoke permissions. Use a plus (+) to add
permissions, and a minus (-) to revoke permissions.

. Finally, specify the permissions you want to change. The permissions you can change are read
(r), write (w), and execute (x). You can specify any or all of these permissions.

Suppose that you decide to let group members and all others read and write the file. You could do
this with a sequence of four chmod commands similar to the one you just tried. Alternatively, you
could do it in one step by specifying multiple users and permissions in one command:
chmod go+rw smartpe_a1

Try: chmod go+rw smartpe_a1 (Copy and comment)

Try: ls -l smartpe_a1 (Copy and comment)

The ``go''specifies that you want to change permissions for the group and others, and the ``rw'
'
specifies that you are changing the read and write permissions. (Run this command in your shell
window.) List your files to verify the change.

Suppose now that you decide that you (and no one else) should have write access to smartpe_aa1 .
You can remove the write access privileges of everyone else with
chmod go-w smartpe_aa1
and give yourself write access with
chmod u+w smartpe_aa1
Page 4 of 7

Try:chmod go-w smartpe_aa1 (Copy and comment)

Try: ls -l smartpe_a1 (Copy and comment)

Try:chmod u+w smartpe_aa1 (Copy and comment)

Verify that the changes have taken effect.


Try: ls -l smartpe_a1 (Copy and comment)

The execute privilege has a different meaning for directories than it does for files.

Execute privilege for a directory means that permission is granted to connect to the directory.

A file should be executable if it makes sense to ‘‘run’’ it. For example, the output of a compiler
should be executable. (Compilers will generally set the execute privilege automatically.) In
addition, ‘‘shell scripts’’ (file that contain Unix commands) can be made executable.

If you attempt to run a file that it doesn’t make sense to run, you will most likely get an error
message.

Groups. The group protection privileges allow you to grant access to certain users (members of a
group) while denying them to all others. For example, when a professor runs a class, he or she
often creates a group consisting of all of the TAs for the class. Then, for example, he can create
files that can be read and written by him (the user), read and written by his TAs (the group), but
only read by the students (the others).

If you are a student, you are probably not a member of more than one group. You can find out
which groups you belong to with the command
groups

Try: groups (Copy and comment )


As an ordinary user you can neither create groups nor add names to an existing group. Only
‘‘superusers’’ with special access privileges can do that. Over time you may become a member of
new groups. For example, you may become a TA or you may work on a group project in a class for
which new groups are established.

If you do belong to more than one group, you should know that the group access privileges
associated with a file apply to only one of those groups. You can find out what group a file belongs
to by doing a long listing.

Protection philosophies. If you are a student, you are probably a member of a single group to
which every other student belongs. So you should generally set the group and other privileges to be
identical.

There are several common combinations of access privileges:

-rw---- This is the default that is used when you create a file. It means that you can read and write
the file, but that no one else can do anything. This is a good protection to use for homework
assignments.
Page 5 of 7

-rw-r-r- This gives you read/write access to a file, but allows others to read it. This is a good
protection to use for files that you want to share with others.

drwx--- This is the default that is used when you create a directory. It means that you can read,
write, and connect to the directory, but that no else can do anything.

drwxr-xr-x This gives you complete access to a directory, but allows others to connect to it and
read (i.e., list) it.

History and Scroll Back


The history command is quite simple. It will display the last few commands that you have issued
in your shell window. This is useful because Unix uses the history to provide a few typing
shortcuts. To illustrate, first enter the following three commands into a Unix shell window.
who
ls
date

Try: who ( Copy and comment )


Try: ls ( Copy and comment )
Try: date ( Copy and comment )

The three commands should now be on the end of your history.

If you would like to repeat the last command that you issued, you need only use up or down
arrows.

Online Manual
There is an on-line manual available to users. This manual may seem a little intimidating at first,
however with experience you will find that it is one of the most useful features of the Unix system.
Information on almost any aspect of the Unix system you are using can be found here.

To look up an entry in the manual, type ‘‘man’’ and then the command you wish to look up. For
example, if you are interested in learning how to use the manual, you should type
man man

Try: man man ( Don’t copy, use enter key to see more lines, use ^c, to end the command.
Comment)
(In the future, we will refer to the manual pages as the ``man pages''.) Since the information
produced by man is frequently quite long, a man page is displayed one screenful at a time. You can
go to the next page by pressing the space bar, go to the next line by pressing the return key, or
simply stop looking at the manual page altogether by typing q.
Try: man man -page (Do not copy, use space bar to look at more pages, type q to end, write your
comment)

What can make man frustrating is wanting to know how to do something, but not knowing the
Page 6 of 7

name of the command. Fortunately, man can help with that too. Man will take an optional
argument, which will make it work in a different way. If you type man -k SUBJECT (think of ‘‘k’’
for keyword) then you will get a one line description of each man page concerning that SUBJECT.

Suppose we wanted to know how to change to another directory. Typing


man -k directory

Try: man -k directory ( Do not copy, write comment)


This should bring up about 25 lines, each consisting of a command and a brief description of what
the command does. (Note that each description has the keyword directory in it.) With a bit of
study, you can learn that the command to ‘‘change working directory’’ is the cd command. You
could now look at the man page for cd for more specific information.

The optional argument in this case is -k directory. Most Unix commands take a variety of
optional arguments of this form. In fact, the most common use of the man pages is to look up the
options associated with a particular command.

Other Useful Commands


This section will introduce you to a few simpler, yet useful commands that you can use in your
shell window.

The date command will display the current time and date in in your shell window. Give it a
try. While it may seem that this is an extremely simple command, it is actually just a
slightly simple command. There are many options you can use to format the date in almost
any way imaginable. Look at the man page for date for more information.
Try: date
You already know that the Unix operating system is capable of multitasking. Along the same
lines, it also allows more than one user to be using a machine at one time. While you can’t
have more than one user on the console of the machine at once, it is possible to log in to a
workstation from another computer and run programs on it. The who command will show
you who is currently logged into your workstation.
Try: who
The uptime command will show you the load average of your workstation, for the last minute,
five minutes, and fifteen minutes. The load average is a measure of how much work the
computer is trying to do. The higher the load average, the more work the computer is doing.
Load averages higher than 3 or 4 indicate that the workstation is heavily loaded.
Try: uptime
The clear command will clear your shell window, and give you a fresh start with a prompt at
the top.
Try: clear (Comment)

The finger command will display information about a user. It takes as an argument the name
of a user. For example
Page 7 of 7
finger someUser
will show you information on any user who has ‘‘zachary’’ in his or her name.

You can also obtain information about users on a remote machine by appending the name
of the machine. For example
finger smartpe

Try: smartpe ( Remember smartpe is your login name, Copy and comment)
This should show you information about users .

The finger command has a reputation for being very slow at times.