Getting Started With Unix
Getting Started With Unix
Log in
The first time you log in on a UNIX systems, you will be asked for your username and
password. At the login: prompt, type in your username, then press the <RETURN>
key.
login: ifaa123
You are then prompted for your password as follows:
Password:
Type the password issued to you with your user number then press the <RETURN> key.
Your password will not be displayed. UNIX is case sensitive so type lower case and
upper case characters accordingly.
Changing passwords
Passwords should be changed on a periodic basis to protect your files and your user
number. To change a password, type the following command at the command prompt:
% passwd
You will be prompted first for your current password, then twice for a new password (
the second prompt provides verification). In subsequent logins your old password will
not be accepted.
Choosing good passwords - basic rules of thumb
When selecting a password, the following guidelines should be followed:
• Choose a password that is meaningful only to you, something that will not be
easily guessed.
• Use 6 to 8 characters, or longer if permitted.
• Mixing case or inserting digits in the middle of your password makes it difficult
for someone else to guess.
• Choose a password that you can remember.
• Do not choose your initials or nickname.
• Do not choose an ordinary word found in the dictionary.
% chfn
The chfn command displays one line of the personal information at a time. To leave the
information as it is, press <RETURN>. To change the information, type the new value
and press <RETURN>. To leave an item blank, type none and press <RETURN>.
After you have responded to all the prompts, chfn displays the message "Updating user
information" and exits.
Note: This option is not currently available on all UNIX systems.
Logging Out
To end your session, type logout at the prompt.
% logout
% ls
List the files in the current directory in long format (the -l option has been added):
% ls -l
List the file named test.doc in long format (a specific argument has been added):
% ls -l test.doc
List the files named test.doc and unix.class in long format:
% ls -l test.doc unix.class
Multiple commands may be entered on the same line, but the commands must be
separated by semicolons. Command lines may contain as many as 1024 characters.
UNIX commands usually do not provide notification of successful completion of a
command; notification occurs only on an error.
% ls
The system responds with a list of files contained in your current directory. In the BSD
version of UNIX the default is to list files in the ASCII alphabetic order.
Exercise:
1.Use the ls command to display the files in your working directory.
% ls -l
The ls command has several additional options. The -rc option lists files in reverse
order of creation date. Other options for the ls command are listed below:
-a List all hidden files, those filenames that begin with a . (period)
-R Lists entries recursively, includes current directory and all subdirectories.
-l List in long format, includes file permissions, owner, size in bytes and time of last modification
for each file.
-F Lists files with a symbol used to denote file type (a / character following a file name denotes a
directory file).
-r Reverse the order of the sort, to reverse alphabetic order or reverse order of creation, as
appropriate
-t Sort by time modified
Files beginning with a "." (the period character) are called hidden files. These files are
used to configure your environment and to set the default behavior of various utilities and
application programs. An example is the .login file which contains commands that are
read and executed each time you log in. Another hidden file is the .mailrc file which is
used to configure and change the default behavior of the mail program. Hidden files are
not displayed by the ls command unless the -a option is used.
Exercise:
1.Use the ls command with each of the options listed above to display the
contents of your working directory. Use the information displayed to
answer the following questions:
Which file was modified last?
What is the name of the largest file?
2.Use the ls command with the -l option to list the following files:
test.doc unix.class neon students.doc
% cat unix.class
% cat unix.class test.doc project_plan
Exercise:
1.Use the cat command to display the contents of the file named
unix.class.
2.Execute the cat command only one time to display the contents of the
files named test.doc, short_course.1 and project_plan.
% more test.doc
• To advance to the next screen press the space bar.
• To advance only one line, press the <Return> key.
• To exit the more command, type q.
Keystroke Action
<Return> Display next line of text
d Scroll forward lines [11] lines (Default is 11 lines.)
f Scroll forward one screenful
b Scroll back one screenful
<interrupt> Quit
q Exit from more
<space bar> Next screenful of text
h or ? Access the on-line help information
Exercise:
1.Use the more command to control the display of the file named
test.doc.
2.Execute the more command only one time to display the contents of the
files named test.doc, short_course.1 and project_plan.
3.View the contents of the files listed in step 2 by using both the space bar
and the <Return> key to advance the screen output.
4.Use each of the keystrokes listed above while displaying the contents of
the files named test.doc and unix.class.
Directories
A directory is a file that contains information that is used by the system to find other
files. When you log in to a UNIX machine you are automatically placed in a directory
called your home or login directory. The home directory is created for you when you set
up a computer account with ITS. This directory can be used to organize your files.
The directory that you are using is referred to as your working directory. When you first
log in, your login directory and your working directory are the same. If you move to
another directory using the appropriate commands, the new directory becomes your
working directory.
u0 u1 u2 u3
cc
username
Changing directories
To change directories, use the cd command. For example, to move into the subdirectory
called bin (in your home directory), type cd bin.
% cd bin
In the UNIX operating system, a naming convention has been established to easily
identify your current directory and its parent directory. A single period or dot (.) refers to
the current directory. Two periods or dots (..) refer to its parent directory. The
command cd .. moves up one level in the directory hierarchy.
% cd ..
To go to the root directory, type cd /.
% cd /
To return to your home directory from any location, type cd.
% cd
% pwd
/home/ccwf/u0/cc/username
Exercise:
1.Use the pwd command to determine your current directory.
Pathnames
Each file and directory has a pathname that uniquely identifies that specific file.
Anywhere you can use an ordinary file name, a pathname can be used. A pathname that
specifies the path from the root to your working directory is called an absolute pathname.
An absolute pathname is always read and written left to right, with the root (designated
by the / character) always starting the pathname. The other type of pathname is a relative
pathname. A relative pathname specifies the name of a file or the path to a directory
starting at your current working directory. A relative pathname never begins with /.
Absolute pathname: /home/ccwf/u0/cc/username
Relative pathname: bin/email.course
Because pathnames are unique, it is possible to have two directories or files with the
same name in two different directories. It is not possible to have two directories or files
with the same name in the same directory.
% mkdir research
Exercise:
1.Create the following directories using the mkdir command.
research programs documents book correspondence
cp unix.class training.unix
Exercise:
1.Use the cp command to copy unix.class to a new file named
training.unix.
2.Copy the file named chap1.1 to a new file named book_review.
3.Copy the file named roast to a new file named beef.
% cp saved.text.mss documents
Exercise:
1.Copy the following files to the directory named documents.
chap1.1 saved.text.mss
2.Copy the following files to the directory named book.
text.group students.doc
% mv project_plan train_project
% mv Text.Group bin
Exercise:
1.Use the mv command to rename project_plan to train_project.
2.Rename the file called text.group to Text.Group.
3.Rename the file named UNIX_training_materials_1.8.92 to
training_1.27.92.
4.Move the file named chap1.1 to the research directory.
5.Move the file named Text.Group to the bin directory.
% rm -i training.unix
% rm: remove training.unix?
% rm -i bin/Text.Group
% rm: remove bin/Text.Group?
% rmdir research
Exercise:
1.Delete the following directories using the rmdir command.
research correspondence
Filters
Filters are tools that allow you to manipulate text. Filters allow you to perform a task on
a file and then filter the changes on to another file. Filters do not alter the original file.
% sort -f students.doc
Exercise:
1. Use the sort command to sort the contents of the file students.doc.
Which line is listed last? Why is this line listed last?
2. Using the cat command, display the contents of the file students.doc.
Has the file been modified?
3. Sort the file students.doc in reverse alphabetical orrder.
4. Sort the file students.doc, telling UNIX to ignore the case.
5. Sort the file students.doc in reverse order and ignoring case.
% grep nd test.doc
Since grep distinguishes between upper and lower case, the -i option can be used to
ignore case.
% grep -i nd test.doc
The -n option is used to display the line number for each string located.
Exercise:
Using the grep command on the file students.doc, determine if the
following students took the class.
Boris Yeltsin
Steve Allen
Bill Clinton
% wc test.doc
Exercise:
1. Using the wc command, determine how many lines are in the
students.doc file.
2. Does this give you an accurate count of the number of students in the
class? Why or why not?
3. How many words are in this file?
The first character in the long file description signifies whether the file is a ordinary file
(a dash "-" character signifies an ordinary file) or a directory file (a "d" signifies a
directory file). The next 9 characters define the type of permissions set for the file. If
permission is not granted, a dash "-" character is displayed.
Character #1
2 3 4 5 6 7 8 9 10
File/Directory User Group Others
read write execute read write execute read write exec
ute
(-/d) (r/-) (w/-) (x/-) (r/-) (w/-) (x/-) (r/-) (w/-)
(x/-)
There are three levels of file protection - user, group and others. They are defined as
follows:
User (u)The user or owner of the file or directory.
Group (g)A defined set of users that share access to the file or directory.
Others (o)The remainder of authorized users on this system.
Every file or directory in a UNIX file system has three types of permissions (or
protections) that define whether certain actions are permissible for the user, a group and
others. These permissions are:
read (r)a user who has read permission to a file may look at its contents. For a directory,
read permission enables a user to list the files in that directory.
write (w) a user who has write permission to a file can modify the contents of that file.
For a directory, the user can create and delete files in that directory.
Exercise:
1. Set the file permissions for the file chap1.1 to read and write for
members of your group. Use the ls –l command to confirm your
changes.
2. Set the file permissions for the file victor to read only for user, group
and others. Use the ls –l command to review your changes.
3. Now change the file permissions for the file victor to read and write for
the user only (removing all other permissions. Use the ls -l command to
review your changes.
Another method used to specify permissions for the chmod command uses a 3 digit octal
number, with each octal digit representing the permissions for a particular set of users
(user, group, other). The table below provides the octal numbers for each possible
combination of symbolic permissions.
Using the octal numbers in the diagram above, the chmod command would be issued as
follows:
% ls -ld
To set the permissions such that the group can read, write and execute the directory
named research, type:
% chmod 700 .
To change the permissions for you parent directory to only user read, write and execute,
type:
% chmod 700 ..
Exercise:
1. Use the ls –ld command to review the directory permissions for you
home directory.
2. Set the file permissions on your home direcotry so that all users in your
group can execute your directory, but not see the contents of your
directory. Now, use the ls –ld command to review your changes.
3. Using the mkdir command, create a directory call documents and set
the file permissions so that members of your group can review
information about files in this directory (read the list of files in the
directory).
% umask
22
% umask 133
Note: The umask command only effects the current login session. To permanently
change your umask setting, the umask command must be added to the .cshrc file.
Printing
The Printing files - the lpr command
The lpr command (lpr is an abbreviation for lineprinter.) is used to send a copy of your
file to a printer. The option -P is used to name a print site which follows the option. To
use the lpr command, type lpr, space, -P, the print site, space and the filename.
% man sites
If your file is a PostScript file (produced by troff, TeX or LaTeX text formatters), it will
not print accurately on a lineprinter. A PostScript file has %! as its first two characters
and contains formatting instructions that cannot be interpreted by a lineprinter.
PostScript files should be sent to a laser printer (output sites for Laser Writers end with
an lw). For more information about printing PostScript output files, see the man pages
(man lpr).
% man ls
A complete description of the ls command and its available options are displayed.
Exercise:
1.Use the man command to look up the on-line documentation on the ls
command. Use the information listed in the man pages to answer the
question below.
List two options that can be used with the ls command.
2.Using the man pages, look up the more command. What is the purpose
of the more command?
The man command searches the sections in the order 1, 6, 8, 2, 3, 4, 5, 7 and displays the
first man page found.
Section 7 is used at the ITS for UT specific information or for information that does not
fit in any other section.
To use the man pages to look up a specific section, type man sectionnumber and the
command or term.
% man 7 sites
Exercise:
1.Use the man pages to look up information in section 7 pertaining to print
sites.
csh
kernel cc
vi
Hardware
"O
pe g
r t i n
Sy a m"
s t e User
sh Program
The shell can also be used as a programming language. You can use a shell to execute a
set of shell commands placed in a file, called a shell script or shell "program". That shell
script can then be invoked at any time and will perform the listed commands.
The most common shells are described below. The ITS machines use the C-shell as the
default interactive shell. The Bourne shell, by convention, is the shell used for writing
shell programs.
sh The "Bourne shell" was named for the shell's author, Steven Bourne of Bell Labs.
The Bourne shell is found on every UNIX system. The usual convention is to use the
Bourne shell to write shell programs.
csh The "C-shell" was written by a group of people at Berkeley. Many of the
programming language constructs resemble the C language, hence the name C-shell. The
C-shell is commonly used for interactive use. The C-shell is known for the three major
features described below.
• Job Control allows the user to switch between multiple processes.
• History keeps a list of previously executed commands which can be
recalled and executed.
• Aliases allow the user to abbreviate commands.
ksh The Korn shell, written by David Korn is available from the AT&T Toolchest.
The Korn shell adds job control, history, command line editing, aliases and subroutines to
the Bourne shell. The Korn shell is compatible with Bourne shell scripts and is
% printenv SHELL
/bin/csh
Shell Duties
• Locates commands; passes arguments and control to the command
• Handles the sequential and concurrent execution of commands
• Performs I/O redirection
• Provide wildcards for filenames
• Maintains environment variables
• Handles pipes (chain output of one program into another program)
• Provides job control
• Provides command aliases
• Provides a programming language
Processes
Each program that you run while logging in or working in the shell is called a process.
After the login process is complete, the C-shell process is running and provides the
prompt that you see. At this point, additional processes may be run. Examples of other
processes one might choose to run are the mail program, vi, troff, a C compiler or another
C-shell.
In the diagram above, the C-shell represents the login shell. A child process is a program
that was started under the current shell. The shell and its children are arranged in a
Environment Variables
An environment variable can store information that is available to the current shell and to
its children. Examples of some environment variables are shown below.
The printenv command is used to display the current environment variables. To use the
printenv command, type printenv at the command prompt. The shell displays the current
environment variables. An example of the output displayed from the printenv command
with an explanation for each variable is listed below.
% printenv
HOME=/home/path/u0/cc/userdirectory your login or home directory pathname
PATH=/usr/local/bin:/usr/local:/usr/ucb:/bin:/usr/bin:
The variables listed in the previous example are known as global variables. This set of
variables is available to all processes. The setenv command is used to set global
environment variables. The unsetenv command is used to unset global environment
variables. To change the EDITOR environmental variable to ed, type setenv, space,
EDITOR (in caps), space and ed.
% setenv EDITOR ed
To change the EDITOR environment variable back to vi, type the command again
substituting vi for the ed value.
In the C-shell you have the option to create and set additional "local" variables which are
used in the current process. The set command is used to create and set a local variable.
The UNIX convention for variables is that UPPERCASE LETTERS are used for
GLOBAL VARIABLES and lowercase letters are used for local variables. In the
example below, the set command is used to set a local variable g to the value
/home/games.
% set g=/home/games
% unset g
The environment variables that you will use frequently are HOME, PATH and TERM. A
discussion of each is provided.
Exercise:
1.Using the pwd command, print out the path to your current working
directory.
Exercise:
1. Use the who command to determine the users currently logged in.
2. Issue the who command again, but this time redirect the output to a file
called users.
3. Sort this file and redirect the output to a file called users.sorted.
Exercise:
Use pipe (|) with ls -l to see a list of your files one screen at a time?
We want to see who is currently logged on, but we want the information
sorted and displayed one screen at a time. Use the pipe (|) in conjunction
with the who, sort and more commands to achieve this goal.
Sequential Commands
A series of commands can be listed on one command line using the semicolon (;)
character to separate each command. The commands will be executed sequentially. If
one command fails, the other commands are still executed.
Concurrent Execution
The UNIX operating system is a multitasking. The ampersand (&) character placed at
the end of a command line is used to run a process or job in the background. The process
in the foreground continues to accept standard input and is displayed on your screen. To
start a process and run it in the background, type the command followed by the
ampersand character.
% cc program.c &
After you enter the command, the process id is displayed and the command continues to
run in the background.
% cc program.c &
[4] 17738
% ps
5218 2f T 0:01 vi
7937 2f T 0:00 vi
Hangup
The second option, -9 sends a kill signal to a process. The -9 option cannot be ignored
and will always kill the process. The process will not have an opportunity to clean up
after itself. To use the kill command with the -9 option, type kill, space, -9, space and
the PID number.
% kill -9 10433
The kill command given with the C-shell id will kill all processes within that shell.
% jobs
% fg %1
Before you logout, you must resume and finish or kill suspended jobs.
Often, you may start a time consuming job and wish to continue working on other
projects at the same time. In the example below, you will format a long document using
the troff text formatting program.
% bg %1
csh - History
The c shell keeps a list of commands that have been executed during this login session.
The list is called history and is stored in the memory of the system. History is listed in
the order of execution (with your last command listed last). The number of commands
stored and displayed in history is set using variables. To see the current history, type
history at the prompt.
% history
% ! 15
Exercise:
Use the history command to get a list of your previous commands.
Use each of the event selectors listed in the table above to execute a
command from history.
csh - Aliases
An alias is used to create shorthand commands. To display the current aliases that have
been defined, type alias.
% alias
h history
l ls -FC
To create a new alias, type alias, space, the new alias, space and the name of the
command.
% alias d date
To delete an alias, type unalias, space and the name of the alias you wish to remove.
% unalias d
Exercise:
1.Create an alias for the date command.
2.Create an alias for the who command.
3. Delete both aliases.