We will be using PC's and logging in over a network. One way to do this is to:
Figure 1: Directory Structure Example
Every UNIX system contains a 'root' directory which is the trunk of the tree. The files and directories branch out from the trunk. Users have their own space set aside on the disk for their files and directories. This space is called the user's 'home directory.' The home directory of a user is represented by their username.
/ This refers to the root directory. ~ This refers to the users home directory. . This refers to the current working directory. .. This refers to the super directory of the current working directory, or the directory up one level. - This refers to the previous directory.
Figure 2: Sample Directory Tree
Figure 2 represents a sample directory tree. ryan
's
home directory contains two items: a subdirectory, myDir
, and a file,
file1
. Every time ryan
logs into this computer, he will begin at
his home directory. The home directory of a user can be represented by a '~,'
(called a tilde). Similarly, the home directory of any other user can be
represented by '~username'.
For example, for ryan
, '~' would represent his own home directory,
while ~kate would represent the home directory of user kate.
UNIX is case-sensitive; that means file1, File1, and FILE1 all refer to different files. A '/' is used to separate subdirectories. A pathname is a sequence of symbols used to identify a file or directory. Every file has a filename. The simplest type of pathname is just a filename. If you specify a filename as the pathname, the operating system looks for that file in the current working directory. If the file resides in a different directory, you must tell the operating system how to find that directory by specifying a pathname.
There are 2 types of pathnames. If the pathname starts form your current working directory then it is a relative pathname. If it starts from the root directory then it is an absolute pathname.
For Figure 2, the pathname of file1
would be:
If userryan
is in his home directory:If userfile1ryan
is outside his home directory:If user~/file1>kate
wants to refer toryan
's file:Absolute pathname for~ryan/file1ryan
's file:/ryan/file1
Similarly, the pathname of file2
would be:
If userryan
is in his home directory:If usermyDir/file2ryan
is outside his home directory:If user~/myDir/file2kate
wants to refer toryan
's file:Absolute pathname for~ryan/myDir/file2ryan
's file:/ryan/myDir/file2
The command cd
switches the user from the current working directory to a
different directory.
For example, in order
to change myDir
in ryan
's home directory:
If userryan
is in his home directory:If usercd myDirryan
is outside his home directory:If usercd ~/myDirkate
wants to accessryan
's files in myDir:cd ~ryan/myDir
cd
can also perform the following specific tasks:
cd ~takes users to their home directory. cd ..takes a user up one directory. (If ryan
is inmyDir
, this command will take him to the directory above, which is his home directory.) cd -takes users to the directory they were previously in. (If kate
were previously in her home directory, but had changed toryan
's (viacd ~ryan
,) thencd -
would take her back to her home directory.) cd /takes users to the root directory.
ls
displays either the contents of the current directory, if no path is
specified, or the contents of the specified directory. For example
would display:ls ~ryan
Thus, the contents of the home directory ofmyDir/ file1
ryan
is
the subdirectory, myDir
, and the file file1
.
ls
also has some options. One of the most common is ls -l
which
displays the contents of the specified or current directory with more information.
For example:
might produce the following output:ls -l ~ryan
The first character in each line represents whether the item is a file or a directory (drwx-w---x 2 ryan 100 4096 Jun 14 2000 myDir -rwx-w---x 1 ryan 100 1450 Jun 14 13:01 file1
d
for directory, -
for file). The next nine characters represent the permissions set to that item
(permissions will be discussed below). The
next number represents the number of links of the file.
(Not discussed here.) Next is the owner of the file, ryan
.
Following this is the userid of the user; ryan
is user 100.
Following this is the size. file1
is 1450 bytes. A byte is the amount of
space needed to store 1 character in a
computer. myDir
is 4096 bytes (all directories are 4096 bytes).
Next is the month, followed by the day, followed by either the year or time
the item was created. If the file way created during the current year, the
time is displayed, if not the year is displayed.
And finally, the last column is the full filename.
This command displays the pathname of the current directory. For example, if
ryan
's current directory is myDir
,
would display:pwd
/ryan/myDir/
ryan
wishes to
delete file1
he can type:
It is important to remember that once a file is deleted, it is gone forever. Use this command carefully!rm file1
kate
wishes to copy file1
to myDir
she types:
We can use cp to make a copy of a file. Ifcp file1 ~/myDir/
kate
wants another file just
like file1
in her home
directory called file1save
, she can type:
cp file1 file1save
kate
wishes to move file1
to myDir
, (and
keep the same name) she can type:
If she is in her home directory and wants to move it tomv file1 ~/myDir/
myDir
AND give it
a different name, she can type:
An ls of the home directory would no longer show file1. mv is often used to rename a file.mv file1 ~/myDir/newFile1
mkdir
command creates a new directory. For example, if
ryan
wishes to create the directory newDir
within his home
directory he types:
mkdir newDir
rmdir
command removes an empty directory (so you have to remove the
files in the directory first with
rm
). For example, if ryan
wishes to delete the empty directory
newDir
he types:
rmdir newDir
more
displays the contents of files one screen at a time.
For example, to display the contents
of file1
one screen at a time use the following command:
(Then hit the space bar to get another screenful)more file1
who
and the related command whoami
give information about users logged into the computer.
whoami
informs you of your username while who
lists all the people currently logged into
the same computer that you are logged into.
For example, for user ryan
, the command whoami
displays:
Possible output for the commandryan
who
:
The first field is the username, the second field is the terminal port they are connected to, and the final two fields are the date and time of connection.kate tty0 Jul 14 12:00 ryan tty1 Jul 14 11:45 mike tty8 Jul 14 05:45
Typingfinger ryan Login name: ryan In real life: Ryan Doe Phone: 555-1234 Directory: /usr1/ryan Shell: /sh/tcsh No unread mail. User Real Name Idle TTY Host ryan Ryan Doe 0:03 p1 moose.wpi.edu
finger
with no arguments returns a list of all users on the system.
quota
displays your disk quota. A disk quota is the amount of disk space
that you are allocated. Every once in
awhile, you should check to see if you are running out of disk space:
The following is sample output from the command quota
.
Disk quotas for user ryan (uid 100): Filesystem blocks quota limit /ryan 3498 4000 5000
The man command accesses the online unix manual pages. Because they are hard to read, we often try every other option before resorting to the man pages (ask your friend, try the web, look it up in a book, etc.) In the above, command_name is the name of the command you want to look up. If you don't know the name of the command, you can do a keyword search by adding the "-k" option before the command_name. If the command you are looking for is anywhere in the man pages, this will find it.man command_name
Every file is stored with a file permission that designates who has the ability to (1) read from, (2) write to, and/or (3) execute that particular file.
A file permission consists of nine items: three groups of the three letters,
r
, w
and x
where r
means read
, w
means
write
and x
means execute
. For example:
The three groups are Owner, Group and Public. For the above:-rwxr----x file1.txt
Here, the Owner (usually the creator) of the file has permission to read, write (change) and executeOwner: rwx Group: r-- Public: --x
file1.txt
. We
won't be using groups, but here the Group has permission to Read (look at) the
file, but not to Write (change)
or Execute the file. And the final three characters represent the level of
access that the Public (sometimes called the
World) has to the file. Therefore, in our example the Owner of the file has permission to read, write, and execute the file. The Group has the ability only to read from the file, and the Public has the ability only to execute the file.
chmod
is a unix command to change the permissions of a file.
there are two methods to use chmod, one is to use the binary/octal method, the
other is to use arguments to specify how to change the permissions.
Binary/Octal Method:
where permission is a three digit number with each digit an octal number (0 - 7). The first digit represents the Owner's permissions, the second represents the Group's permissions, and the third represents the Public's permissions. Each digit is a combination of the three possible permissions: read, write, and execute. We have to look at each octal digit in binary (base 2) to understand the meaning. Let's do this with the examplechmod permission filename
In binary,chmod 755 file1.txt
755
is 111 101 101
(spaces for
clarity).
Each group of three binary digits represents the triple rwx
.
The first group, 111
, means that the owner can read,
write
and execute file1.txt
.
The next group of three, 101
indicates that the Group can
read
and execute, but not write (change) file1.txt
.
The third group, also 101
indicates that the Public can
read and
execute, but not write to file1.txt
.
Therefore, the value for a permission of Owner to read, write, and
execute, Group read, and Public read,
(rwx r-- r--
) would be
744
; the value for a permission of Owner
to read, write, and execute, Group to read,
write, and execute, and Public none
(rwx rwx ---
) would be 770
; and the value for
permission for all 3 groups
to perform all 3 operations (rwx rwx rwx
)
would be 777
.
The other syntax for this command is:
who has to be a single letter signifying which set of permissions to affect: user, group, or other. The codes are as follows:chmod who[+/-]permissions file
The [+/-] designates whether to allow or disallow the following permissions. The permissions, like octal mode, are for reading, writing, and executing. The codes for these are:u = user g = group o = other a = all
There can even be more than one argument to specify how to set all the permissions. These arguments must be separated by a comma:r = read w = write x = execute
If I have a file namedchmod who[+/-]permissions,who[+/-]permissions file
foo
,
and I want to make sure everyone has permission to read it, I would execute the
following command:
This means, for the file foo, I want to add the read permission to all the sets of permissions. Suppose I want to give everyone read and write, and execute permission (this means anyone can erase it too!). The command would look like:chmod a+r foo
Now, suppose I want to give everyone read permission, but only give myself write and execute permissions, I would type:chmod a+rwx foo
The advantages of using this method over the octal method is that it is easier to understand, and you can set one permission regardless of the other permissions. The disadvantage is that a complicated command takes longer to type and think out (once you know the octal codes, the octal method is almost always faster, but if you forget them, the letter method is usually easier to understand). The best way to learn how to use chmod is to use it. Try the examples in your unix account. You can create a dummy file named foo by typing:chmod a+r,u+wx foo
you can check the permissions on foo by typing:touch foo
ls -l foo