When you obtained a Unix account, you got a starting password that was common to the class. You should change that password to something that is not easily guessed. There are good reasons for that. There are persons "out there" who would rather tear down than build great new things. Some of these people go around trying to break into Unix systems so that they can show you how clever they are by deleting files or leaving messages telling you how stupid you are for letting them in. Usually these people get in because someone has given away their password or made it so easy that anyone could guess it (like your last name, or your last name backwards, etc.). Nevertheless, the password you were given doesn't have to be the one that you live with. The command to change it is to type
passwd
The Unix operating system will respond by asking you to type your old password, then the new password, then it will ask you to retype your new password (just to make sure you haven't mis-typed it). If you respond consistently, then Unix will change your password in its records.
Your userid and your passwords are valid for access to the
Unix systems in MH 227 and DH 450 but not WSH 1. (You can get
a separate Unix account for the computers in WSH 1).
These accounts are separate from any Windows accounts you may have
in the department or on campus.
Guidelines for passwords
Don't
use your name, your significant other's name, your license plate
number, your student ID number, your pet's name, or any other
password that can easily be guessed by someone who knows who you
are.
Make it at least six characters.
DO use numbers and special characters mixed in with letters. Just because it has a special character (such as the ones along the top of the keyboard above the numbers) doesn't mean it has to be hard to remember.
Pick something that you don't have to write down to remember.
Unix commands are entered usually at a keyboard. If you are familiar with DOS, then you already know some Unix commands. Actually, Unix was developed many years earlier than DOS and DOS took some of the Unix commands and made them part of DOS.
Most Unix commands consist of a few letters. Since Unix was
originally developed at a time when there were no graphical user
interfaces, one of the goals was to make the commands short so there
would be less to type. So you will see things like cp
instead of "copy" or mkdir
for "make
directory". In addition to the command name itself, there are
often options associated with the command. Usually these options are
single letters or groups of letters preceeded by the '-' character
(the hyphen symbol). These options are called "flag arguments"
and alter the action of the command in some way or other depending on
the command.
Following the command and its options there may be the name of a
file or a directory. These names can be virtually any number of
characters (unlike DOS). Get in the habit of using names that tell
you what's in the file such as StackMaker.java
or
projectSpecifications
.
Click here for more
information on Unix file names.
Here's the general form of a Unix command. Most Unix commands look like this but there is considerable variation. In particular, some commands don't have all the components.
commandName -options fileOrDirectoryNames
In Unix, as in most operating systems, files are located in a hierarchical structure of folders (usually referred to in Unix as directories). You can think of a directory as a place to store files or other folders. At the top of the structure, there is a directory called the "root". We denote the root directory by the / symbol. Inside that directory there are "sub-directories" containing files and other subdirectories. This continues down until we get to the directory that you first see when you login to the Unix system. This directory is called your "home" directory. Other users have their home directories in some other part of the hierarchy.
Your home directory is the directory you "are in" when you first open a terminal window. Your home directory may be in a directory called export which may be in the / (root) directory. Although there are similarities from system to system about where users home directories are located, there is no requirement that they must be in a particular location.
ls
commandls
is an abbreviation for "list" as
in "give me a list of the files in my directory".
Try typing the following command in your home directory:
ls
What do you see? Depending on what files you have in your home directory, you may see nothing.
Now type the following command in your home directory:
ls
-a
The -a
indicates an option to show all files
in your home directory. You will notice some files that start with
the '.' (dot) character. These are files that are required by various
programs and utilities. Unless you use the -a
option you
will not see them listed. For this reason they are often referred to
as "hidden files".
Now type the following command:
ls
-F /
The /
character is the directory name for the root
directory. What you should see are the subdirectories and any files
that are located in the root directory. The -F
option
will result in directory names having a /
character
following their name. You may notice other names with @
symbols or *
symbols following their names. These are
not actually part of the name but instead "markers" that
identify the types of files. For example files with the *
symbol are ones that are executable (somewhat like files with a .EXE
suffix in DOS). Unless you use the -F
option on the ls
command, these markers will not be visible.
Try putting the F
option and the a
option together (-aF
).
Does the order of the parameters affect the output?
Try putting in the name of a
subdirectory under the root directory in place of the root
directory. What happens if you just enter the name (e.g. ls
etc
). Try putting the /
character before the
subdirectory name and see what happens (e.g. /etc
).
Try adding the -l
option. This is the long
option that gives a long description of each file or subdirectory.
The first column of characters contains information about who can
access the file or subdirectory and the level of access. (See the
chmod
command) The third column indicates the name of the owner of the
file or directory. The fourth column is the size of the file in
bytes. Next is the date and time of the most recent change to the
file or directory. Finally the name of the file or subdirectory is
listed.
cd
commandIn the section on the ls
command, we can view the
contents of the root directory, for example, but if you just logged
into your Unix account, you are in your home directory.
To be in a directory means that you can access the files in that directory more easily than if you were in another directory. More easily just means that you can refer to a file by its local name instead of what is called its full path name.
Type the command:
ls
-l /etc/hosts
You will see the long listing for the file called hosts
which is located in the etc
subdirectory of the root
directory. The name /etc/hosts
is the full path name for
the hosts
file.
Type the command:
cd
/
followed by:
cd
etc
followed by:
ls
-l hosts
The cd
command changes the directory in which you are
working to the one named in the command. The first command moves you
to the root directory (/
). The second moves you to the
subdirectory called etc
. In this directory you can refer
to the hosts
file by its local name.
The full path name contains the sequence of subdirectories from
the root directory down to the subdirectory in which the named file
lives. The full path name for the hosts
file, for
example, is /etc/hosts
.
Use the cd
command to go to the etc
subdirectory of the root directory again. Now type the command:
cd
..
followed by:
ls
-l
In which directory are you located now?
Change directories to the root directory. Now enter the command:
cd
followed by:
ls
-l
In which directory are you located now? So what do you think the
default directory is when you just type cd
with no
directory name?
cp
commandThe cp
command allows you to copy a file. Its basic
form is given by:
cp
fromFile toFile
A copy of the fromFile
will be made and given the
name toFile
.
If the toFile
name is a directory name, then a copy
of the fromFile
will be made, given the same name as the
original, and placed in the named directory.
We saw in the cd
command that the symbol ..
(two dots) can be used as an alias for the directory just above the
current directory in the directory hierarchy.
The symbol .
(one dot) can be used as an alias for
the current directory.
Place yourself in your home directory. How can you copy the
/etc/hosts
file into your home directory giving it the
same name?
Place yourself in your home directory again. How can you copy the
/etc/hosts
file into your home directory and give the
copy the name of hosts.copy
?
rm
commandThe rm
command allows you to remove (delete) a file.
Its basic form is given by:
rm
-i fileToDelete
The -i
option causes Unix to ask you for confirmation
before deleting the named file. You should NEVER use the rm
command without this option.
Using aliases, you will
be able to customize your interface to Unix so that the -i
option becomes the default and you won't have to keep remembering to
include it. Aliases are covered in another
part of this lab.
After you have done the exercises relating to the cp
command, you will have some files in your home directory that you
should delete. Delete them.
mkdir
commandThe mkdir
command allows you to create a
subdirectory. Its basic form is given by:
mkdir
directoryName
First of all, you must be located in the directory in which you
want to create the subdirectory. Secondly, you must have permission
to create a subdirectory in that directory. (Permissions are
discussed in the context of the chmod
command.)
You always have permission to create a subdirectory in your home directory or in any subdirectory under your home directory.
Create a subdirectory called temp.lab
in your home
directory. Now issue an ls
command in your home
directory (use the -F
option). Change directory to your
new subdirectory. Copy the file /etc/hosts
to this
subdirectory. Use the ls -l
command to check to see if
the copy was successful.
rmdir
commandThe rmdir
command allows you to remove (delete) a
subdirectory. Its basic form is given by:
rmdir
directoryName
You will find that at some point you no longer need a particular
subdirectory and you wish to delete it. The rmdir
command will do this. It will, however, only delete subdirectories
that have been emptied of all their files and subdirectories.
Consequently, if you want to delete a subdirectory, you must first go
in and delete all files in that subdirectory. If the subdirectory has
other subdirectories, then you must delete all the files in those
subdirectories, too.
Of course, you must have permission to delete the named
subdirectory. (Permissions are discussed in the context of the chmod
command.)
You created the subdirectory called temp.lab
while
learning about the mkdir
command. Delete that
subdirectory now. First try issuing the rmdir
command
BEFORE deleting any files in the subdirectory. What happens? Now do
what is necessary to delete the subdirectory and do it. How can you
check to make sure that the subdirectory was deleted?
pwd
commandSometimes, if you have been changing from one directory to
another, you may not recall exactly where you are in the hierarchical
structure. The pwd
command allows you to determine this
information. Its basic form is given by:
pwd
The output of this command gives you the path to your working directory, that is, the directory in which you are working.
In your home directory enter the pwd
command. If the
root directory is considered level 0 in the directory hierarchy, at
what level is your home directory?
mv
commandYou can move a file from one directory to another by using the mv
("move") command. Its basic form is given by:
mv
fileName newFileOrDirectoryName
where fileName
is the name of the file you wish to
move and newFileOrDirectoryName
is the name of the file
in its new location. That is, you can change the name of the file as
you move it. If newFileOrDirectoryName
is the name of a
directory, then the file is moved to that directory and keeps the
same name.
Since the name of the file can be changed as you move it, you can use this command for renaming a file. For example, if you type:
mv
oldFileName newFileName
then the file called oldFileName
gets renamed to
newFileName
without moving out of the directory in which
it is located.
You can even use this command for changing the names of
directories. Simply replace oldFileName
by
oldDirectoryName
and newFileName
by
newDirectoryName
in the previous example.
Change the name of the temp.lab
directory that you
created when working with the mkdir
command. Make its
new name labFiles
.
Go to your newly named subdirectory (labFiles
) and move
the file named hosts
(which you copied from /etc/hosts
)
into your home directory.
chmod
commandIn several of the Unix commands, you can only carry out the
command if you have permission to do so. For example, try copying any
file (e.g. the hosts
file that you obtained earlier)
into the root directory with the command:
cp
hosts /
You should have obtained a Permission denied
message.
You don't (and shouldn't) have permission to place things in the root
directory. Only the system administrator should be able to do this.
Suppose you have a file that you would like to keep private so that others would not be able to view it. Suppose you have another file that you would like to make available for others to copy.
In short, you would like to be able to set up permissions to
restrict users from some files and to allow users to access other
files. The Unix command to do this is called chmod
(for
"change mode").
Unix recognizes three classes of users when looking at permissions related to directories or files:
the user (owner) (the person who created the file or directory).
the group (this usually means a group of persons working together on a project, for example---and may not be used on the Unix system you are using).
all other users.
Each file has three kinds of permissions.
read permission
write permission
execute permission
Go to your home directory and type the command:
ls
-al
In the first column you will see something like
drwx------
or
-rwxr-xr-x
The first letter will be d
if the named item is a
directory and will be a -
character if it is a file.
After that the letters appear in clusters of three corresponding to
the sequence:
rwx
The first cluster shows the permissions for the user/owner, the
second cluster shows the permissions for the group, and the third
cluster shows the permissions for other users. If the appropriate
letter is present, then that permission is granted to that user or
those users. In the file example just above (the one beginning with
the -
character), the owner has read, write, and execute
permissions, the group has read and execute permissions, and other
users have read and execute permissions.
If you have read permission then you can see and copy the file. If you have write permission, then you can change (write) the file. If you have execute permission, then you can execute the file (assuming it is an executable file). For directories, execute permission means that one can go to that directory.
Conversely, if you don't want someone to be able to copy a file, then they should not have read permission. If you don't want someone to be able to change a file, then they should not have write permission. If you don't want someone to be able to go to one of your subdirectories (or even your home directory) then they should not have execute permission.
The chmod
command allows you to change the
permissions of a file or directory by specifying the set of users you
want to affect, the change (either granting or denying permission),
and the permission category. For example to give all other users read
permission to the file named sharedFile
, enter the
command:
chmod
o+r sharedFile
The 'o' means "other users", the '+' means to grant permission, the 'r' means "read permission". These options are placed in this order with no space between characters.
As another example, to deny access to other users to the directory
named privateFiles
, enter the command:
chmod
o-x privateFiles
The '-' means to deny permission.
Assuming you have a subdirectory named labFiles
, find
out what permissions other users have. Now change the permissions so
that no other user can see inside that subdirectory.
How would you change the permissions in your home directory so that no one else could access your directory? (Recall that '.' can be used to represent the "current directory".)
How would you change permissions so that all other users can copy a
file (called sharedFile
) in a subdirectory of yours
called labFiles
? (In order for someone to copy a file,
they have to be able to access all the directories including your
home directory and any subdirectories leading to that file.)
Click on to go back to the main directory.
Click on to take the quiz for this module.
These pages were developed by John Avila SJSU CS Dept.