Introduction to the Unix Operating System
CONTENTS
- Introduction
- Things to Remember
- Getting Started
- Files
- The File System
- Unix Command Syntax
- Table of File Commands
- The ls Command
- Setting File Permissions with chmod
- Some Special Characters
- Text Editors
- Input/Output and Redirection
- Tailoring Unix
- How to use Command History
- Job Control
- Appendix A: Printing Commands
- Appendix B: Some Utility Commands
INTRODUCTION
This document is intended to help the new user of the computers that run a version of the Unix operating system. The command interpreter described in this document is the Korn shell, which acts as an interface between the user and the Unix system. The Korn shell is the default shell on the VLSI Lab's Unix systems, so user commands are generally the same on each Unix machine. Any differences in a command among the machines will be noted in this document.In order to include the information that a wide audience of users might want to know, this document has been packed full of information. There is probably more here than you will want to try to assimilate in one sitting. Try reading a section at a time and experimenting on your account as you work your way through the document. There may be some sections for which you currently have no use; skip them. They will still be there when you need them.
Things to Remember
- The Unix operating system is case-sensitive; i.e., a command in lower-case letters is interpreted differently from one in capital letters. ALWAYS turn off the Caps Lock key on your terminal or microcomputer before you log in. Enter all commands in lower-case letters unless otherwise instructed.
- Be sure to end everything that you type with a carriage return (marked Return). The Return key is shown as <CR> in this document. The computer doesn't know you are trying to tell it to do something until it sees a carriage return, so if the computer seems to be ignoring you, check to see if you forgot to press the carriage return.
- VCU's University Computing Services maintains a set of documents on using Unix at: http://views.vcu.edu/ucsmcv/resources/unix.html
GETTING STARTED
Logging In and Changing Your Password
Whether you are logging in at the system console or on a PC via Xwin 32, you will start at the Sun login screen.
- In lower case (Unix is case-sensitive), type the login ID that was given to you; it will probably be your initials with an alphanumeric suffix.
- After you have typed your login ID, press the Return key or click on the OK button.
The system will respond by presenting the password screen. Be sure that the Common Desktop Environment is selected as shown in the right portion of the password screen. Use the Options->Session menu item to change it if necessary. Enter the password you were given, followed by a carriage return.
- Once your password has been accepted you will be logged into the Sun with the Common Desktop Environment (CDE). You shold have two Unix windows with prompts displayed in which you can enter Unix commands, and other applications such as the Help Manager and the File Manager. After logging in, you should immediately change your password to something only you know. In a Unix window, enter the command:
passwd<CR>
The system will prompt you for your old password, then for your new password. Enter a new password with six to eight characters including at least one numeric or special character. The system will let you know if the password does not meet our security requirements. You will be asked to enter the password twice for typing verification. Once this process is complete, your password will be changed.
Choosing a password that provides system security is important, not only to protect your files, but also to protect the entire system from being accessed by the mischievous or unscrupulous. Do not use your name or initials or any word that can be found in the dictionary. Putting a number in the place of a letter (the digit 1 for the letter l, for instance) is also not secure, nor is putting a digit at the beginning or end of a word or name. It is safer to use a sequence that is meaningful only to you or to use two words, omitting vowels and joined with a character such as a hyphen or comma.
- You can change your password at any time by typing:
passwd<CR>
You may then change your password as described above.
FILES
The File System
Unix uses a hierarchical (or tree-structured) directory system to store files. A diagram of a portion of a file tree is shown below, with the directory /, called the "root" directory, at the top of the tree.In the Unix system, all the commands you type, as well as peripheral devices such as disk and tape drives, can be specified as a path to a file. A path name can be absolute, meaning it begins with /, or relative to the directory where you are attached, called the current or working directory. Directory names are separated by additional slashes (e.g., /etc/passwd). In the figure above, some of the directories shown are:
Name Directory Contents Name Directory Contents /bin Unix utilities, such as
ls, cat, cp, cal,
spell, date, tee, etc.
(described below)/etc administrative files
and directories, such
as the file "passwd"
which contains login
information for each
user account/usr additional utilities /lib libraries, such as C
language and mathematics
libraries/tmp temporary files; this
directory is cleaned
out periodically/dev peripheral devices,
such as disk drives and
terminals/students student
user directories/mentor Mentor Graphics
EDA tools/fac faculty/staff
user directories/apps additional
programsYou will be assigned a directory with your login ID as its name when you are given an account. The user accounts reside in the /students directory. When you complete the login process, you will be attached at the directory /students/your-login-id in the file tree. Your login directory is called your HOME directory where HOME is one of the Unix system's shell variables. Shell variables are read by many Unix commands and can be given values either by the system or the user. For more information about shell variables, see "Tailoring Unix" below.
You can create other directories and files below your HOME directory as you need them. For instance, if your login ID is "abc3f," you may be assigned a HOME directory with the name /home/abc3f. If you create a directory named "docs", you could put all the chapters of your thesis in that directory. Then the full path of a file named "chap1.thesis" would be
/students/abc3f/docs/chap1.thesis
File Names
Unix is very flexible in the naming of files (and directories), but there are some rules that you should use to avoid trouble. The best rule is to use only alphanumeric characters in file names and the "-" and "_" characters. Avoid using spaces in file names and don't use the "-" character as the first character in the file name. Remember that Unix is case sensitive, so the file names "My_file" and "my_file" are NOT the same file and can exist in the same directory at the same time. However, using capitalization to distinguish between file names gets very confusing, so the best rule is to use all lowercase in file names. Some users capitalize the first letter of directory names to distinguish them from file names, but using the "-F" argument with the ls command (see below) will accomplish the same thing.Finally, it is a good idea to add a "dot extension" on the end of the files to denote their type. For example, all text files should end with ".txt", Mentor Graphics do files should end with ".do", etc.
Unix Command Syntax
The typical syntax of a Unix command iscommand arguments files
where the command options, also called arguments, are usually preceded by a hyphen (-). More than one file name may generally be given. Optional arguments will be surrounded by square brackets ([ ]) in the documentation on each command.
The table on the next page gives some commands and selected options that are useful for manipulating files and moving around in the file system, including how to list the contents of a directory (ls), the contents of a file (cat) and how to move from directory to directory (cd). To get complete information on each command and all its options, use man command to view the on-line documentation. The syntax is:
man command
The ls Command
Sample output from the command ls -Alg is shown below with column numbers at the top for reference with the explanation following.1 2 3 4 5 6 7 8 _________________________________________________________________________ -rm-r--r-- 1 abc3f usr 4193 Jun 10 10:11 .history -rw-r--r-- 1 abc3f usr 626 Jun 6 12:08 .kshrc -rw-r--r-- 1 abc3f usr 338 Mar 17 16:43 .mailrc -rw-r--r-- 1 abc3f usr 9156 Nov 9 13:06 .newsrc -rw-r--r-- 1 abc3f usr 800 Jun 6 14:28 .profile -rw-r--r-- 1 abc3f usr 1424 Jun 6 14:28 .variables.ksh drwxr-xr-x 2 abc3f usr 144 Jun 9 16:21 News drwxr-xr-x 4 abc3f usr 144 Apr 16 14:34 docs drwxr-xr-x 2 abc3f usr 64 Jun 3 10:34 fortran_progs -rw-r--r-- 1 abc3f usr 3289 Nov 9 10:09 junk
Column 1: Ten characters giving file information; first character (-, l or d) is type of file; remaining nine are permissions. The first entry will generally be one of the following:
d = directoryThe next nine characters specify the file permissions given to user abc3f (first three characters), the group to which abc3f belongs (next three), and the other users on the system (last three).
l = symbolic link to a file
- = ordinary fileThe permission characters are as follows:
r for readIf all permissions are given for a file, these entries would be
w for write
x for execute
- for permission deniedrwxrwxrwxwhich gives the user, group and other read, write and execute permissions. For more information, see the section "Setting File Permissions with chmod."Column 2: links; for a file, this is the number of links to the file (see the man page on ln to read more about links); for a directory, it is the number of directories it is in (its parent and itself) plus the directories under it. Column 3: owner of file. Column 4: group to which owner belongs. Column 5: number of bytes (characters) in the file. Column 6: date file was last modified. Column 7: time file was last modified. Column 8: file name.
Setting File Permissions with chmod
One way to set the permissions on files and directories is with the chmod command. The command syntax is as follows:chmod mode filename
where
mode is a string of users and permissions.
filename is the name of the file whose permissions are to be changed.The example shown below gives mode as a string defined to be who_permission where who is
u = user (abc3f in the example above) g = group (usr in the example above) o = other a = all (default)and+ = gives permission - = denies permissionand permission isr = read w = write x = execute
Example 1: You want to remove read permission on a file named "personal" from everyone except you. Use the command: chmod go-r personal
Example 2: You want to make a command file called "printit" readable and executable by everyone. Use the command: chmod ugo+rx printit or chmod a+rx printit
Some Special Characters
Some characters have special meaning to the Unix system's Korn shell interpreter. These can be used so that you do not need to type long names of directories and files, or they can be used to "wildcard" (match more than one) directory and file name. Some of these characters are defined below.
File Expansion Characters Explanation . Current directory .. Parent of current directory ~/ The system will expand ~ before / to
the HOME directory of the user.~login_ID The system will expand ~ before a login_ID to
the HOME directory of login_ID, using
information from the /etc/passwd file.* Matches 0 or more characters; does not
match a leading period (.)? Matches any single character [...] Matches one character in a specification where
... can be a range (e.g., a-z, 0-9) or
a list (e.g. aeiouy) or a combination of both
(e.g., aeiouy0-9 matches any vowel or digit).
TEXT EDITORS
There are several text editors available to create and modify text files on the VLSI lab Unix systems. The vi text editor is command line driven (i.e., it uses only text commands, not the mouse) and runs inside an existing Unix command window. The vi editor is fast, fairly powerful and has the advantage of being available on all Unix systems you might encounter. However, vi is somewhat difficult to use and lacks some of the more advanced features of newer test editors. Additional information on using vi and vi commands can be found in the Basic Introduction to the VI Editor document. The vi editor is invoked from the Unix command line like this:vi filename<CR>
Another editor available on the VLSI lab Suns is xemacs. The xemacs editor is a version of the emacs text editor with an X window GUI added. The xemacs editor is easier to get started using than the vi editor and has a number of advanced features, but it is slower in execution speed than vi. The xemacs editor can be invoked from any Unix command window like this:
xemacs &
The result should be the appearance of the xemacs window as shown below:
Finally, a third text editor available on the VLSI lab Suns that you might want to use is nedit. The nedit editor can be invoked from a Unix window with the following command:
nedit &
The result should be the appearance of the nedit window as shown below:
Note that in nedit, you can use the Preferences->Language Mode menu item to setup nedit to recognize a specific language you are using. This results in such handy features as keyword and comment highlighting as shown in the above example where nedit is being used to edit a VHDL language file.
INPUT/OUTPUT AND REDIRECTION
One of the features provided by the Unix operating system is the ability to redirect output from a command to a file or to another command. Some of the characters used for redirection by the Korn shell are:
Redirection Character Use > Write output from a command to a file >> Append output from a command to a file < Read input to a command from a file | Send ("pipe") output from one
command to anotherCombining redirection characters with the files called standard input (the keyboard, file descriptor 0) and standard output (the screen, file descriptor 1), standard error (file descriptor 2), and the command tee, you will have the tools and symbols you need to make use of redirection. Study the examples below to get some ideas.
- To use electronic mail to send a letter you have prepared with an editor and saved as a file called "welcome.letter" to xyz3g@virginia.edu:
mail xyz3g@virginia.edu <welcome.letter
- To send all the FORTRAN compiler error messages to a file called "errors" rather than have them come to the screen:
xlf myprog.f 2>errors
- To send the output from the ls command to the file "ls.output" rather than to the screen:
ls >ls.output
- To join "file1" and "file2", send the output to the sort command and then put the sorted output into a file called "sorted.big.file":
cat file1 file2 | sort >sorted.big.file
- To sort another file and append the output to "sorted.big.file":
sort file3 >>sorted.big.file
- To send output from the sort command both to a file "new.file" and to the screen:
sort filename | tee new.file
TAILORING UNIX
Special Files
It is possible to customize your Unix environment by editing the appropriate files. You can change the names of commands as well as specify which command options are selected. Some commands, like mail and emacs, check for a specific file, often called a reconfiguration file (notice the letters "rc" at the end of many filenames) from which they get initialization information.The following are some of the reconfiguration files you may see when you issue the ls -A command.
Default .profile, .mailrc, .joverc, .variables.ksh and .kshrc files exist, but you can create your own versions of these files to set your initialization preferences. The .history file is created automatically by the Korn shell and the .newsrc file is created by the trn program. You can look at and modify any of these files except the .history file, which is not a text file. More information follows on the .profile, .variables.ksh, .kshrc and .history files, but first it will be useful to know more about Unix shell variables.
.profile used by the Korn shell to set the user's environment at login (see below) .variables.ksh used by ITC to set values for shell variables .kshrc sets user's command aliases, if specified (see below) .history stores commands you have issued (see below) .mailrc sets electronic mail environment and mail aliases .forward forwards electronic mail to another address .newsrc sets USENET news subscriptions .joverc initializes JOVE editor .exrc initializes Vi editor, if specified
Shell Variables
The Korn shell sets up some variables called shell variables which are read by Unix commands. One way that the Unix environment can be tailored is to change the values of these variables. A complete list of the shell variables set up by the Korn shell can be found on the ksh man page.A shell variable (also called an environment variable) is usually written in upper-case letters (remember that Unix is case-sensitive) and its value is often a pathname to a directory or file. Many Unix commands read the value of one or more Unix shell variables when they execute. For example, full screen editors like JOVE or Vi need to know the type of display you are using, so they read the value of the TERM shell variable to get the information needed for full-screen display.
Some shell variables are described below.
The PATH shell variable has special importance since it names the directories (separated by colons) that the system will search in for commands you type. The directories are searched in order from left to right. For example, if you invoke the ls command by typing the complete path /bin/ls, the value of PATH would not be used since there is a / in the command. However, if you type ls, the system searches the directories given in the PATH variable. The first executable file named "ls" found in one of the PATH directories will be executed. If no file is found, the error message ksh: ls: not found will appear.
EDITOR name of editor you use ENV specifies file to be executed at shell invocation;
good place for command aliasesEXINIT string value to set initial specifications for editor Vi HISTFILE name of file in which commands are stored (default is .history) HISTSIZE number of commands stored (default is 128) HOME default is current directory when you log in; e.g., /home/abc3f MAILCHECK time in seconds for system to check for new mail PATH search path used to find executable files (commands) PRINTER default network printer PS1 prompt RNINIT string value to set initial specifications for trn program TERM type of terminal you are using To see the current value of the PATH, issue the command:
echo $PATH
The directory specified by "." represents the current directory.
If you create your own executable command files (e.g., C programs or Unix shell scripts), you could put them in a "bin" directory in your HOME directory. To get the system to search that directory, set your PATH value to
PATH=${HOME}/bin:${PATH}
which will cause the system to search your bin directory before searching other directories.
To see the current values of shell variables, issue the command set.
HOW TO USE COMMAND HISTORY
The Korn shell stores the commands you enter in a file named .history. There are two ways to make use of the .history file to re-execute commands you have previously typed.
- First method: Enter the command
history
which will produce a list of the sixteen most recent commands you have issued. When you locate the command you want to re-execute, note the number of the command, and enter
r command-number
- Second method: This method gives you the opportunity to edit your previous command.
For Vi Users: Put set -o vi in your .variables.ksh. Then when you enter <ESC> (the escape key on your keyboard) followed by the letter k, the most recent command you have executed will appear on the command line. Repeating the letter k will present each command in the .history file in the order from the most recent. You are going up in the file. By typing the letter j , you can go down in the file, provided you are not at the most recent command. The letter h moves the cursor one character to the left on the command line; the letter l moves the cursor one character to the right. The arrow keys do not work.For JOVE Users: Put set -o emacs in your .variables.ksh file. (NOTE: jove is not recognized as a command-line editor.) To retrieve commands, use Ctrl-p to bring back the most recently executed command. After you have brought back at least one command, you can use Ctrl-n to get the next command, Ctlr-f to go to the right and Ctrl-b to go to the left.For either editor: Once you have found the command you want to execute, you can edit it using some of the standard commands for the editor you use. When you have finished editing, enter a Return to execute the command.
JOB CONTROL
Background Jobs
To run any Unix command in the background so that you can continue other work at your terminal, append an ampersand (&) to the command. You will immediately be returned to the system prompt so that you can issue other commands. The command jobs will show you the status of your background jobs.If you log out before the process has finished, it will be killed. To avoid this, precede the command with nohup (no hangup). The syntax for the complete command is
nohup command and arguments&
If that fails, try
ksh
nohup command and arguments&
exit
If your command contains a pipe (|), issuing the command above will not work. Put the command into a file, make the file executable using chmod, hen use its filename in place of command and arguments.
To lower the priority of your background jobs, use the nice command described in the table below. You can also specify a time to run your job using the at command. See the man pages on nice and at for more information and usage examples.
Finally, if you forget to put a lengthy job in the background, Korn shell users can enter Ctrl-z to suspend the job. Then enter bg to put the job in the background; you will then get the system prompt so that you can do other work on the computer. Similarly, if one of your background jobs seems to be running excessively and you suspect it has gone awry, issue the jobs command to get the job number, then regain control of it by putting it into the foreground with fg %job_number. Then you can terminate the job with Ctrl-c if you want.
Terminating Runaway Processes
Occasionally you may lose keyboard control of your terminal. If this happens, go to another terminal and log on to the same computer you were logged on to when you lost control. Issue the command:/bin/ps -fu login_id
or
ps -ux | grep login_idThe screen for user abc3f with a runaway session might look similar to the figure shown below.
UID PID PPID C STIME TTY TIME CMD abc3f 34983 23462 0 May 08 pts/28 0:00 -ksh abc3f 43708 41403 0 15:51:47 pts/24 0:00 -ksh abc3f 54257 43708 40 16:07:52 pts/24 0:00 ps -fu abc3fEach login session can be identified by a line ending in "-ksh." The current session will be the one associated with the ps command (PID and PPID 43708 in the listing above); therefore, the runaway session above has PID 34983. Terminate the session by issuing the commandkill -9 34983
Job Control Command Summary
As mentioned before, you can get complete information on the above commands by using the man command. For job control commands specific to the Korn shell (ksh) interface, type
Command Result echo command_file | at time run command_file at specified time atq list all at jobs belonging to user (for SGI use at -l) atrm id remove at job with id (for SGI use at -r id) bg [%job_number] Put job_number in the background; default is
that has been suspended with Ctrl-z.Ctrl-z suspend current job fg %job_number put job_number in the foreground jobs list background jobs kill -6 processID Sends kill signal to process number processID,
which is determined by issuing ps command.
Gives the process a chance to exit cleanly.kill -9 processID Terminates process number processID, which is
determined by issuing ps command. Use with discretion.nice command Run command at lower priority;
use for CPU-intensive programs.nohup command No hang-up; use to keep a background job from
terminating at logout.ps [-eflu] or ps [x] lists running processes (RS, SGI, Solaris)
-e show all processes
-f full listing / show user ID
-l long listing
-uuser-id
show processes owned by user-id
x show processes with no controlling terminal (RS6000)ps [-alux] lists running processes (SunOS)
-a show all processes
-l long listing
-u show user ID / full listing
-x show processes with no controlling terminalman ksh
for more information.
Appendix A: Printing Commands
The following chart includes some of the ways to send your output to a printer. As for most Unix commands, output from a command can be sent directly to a printer by using the "pipe" to redirect the output, bypassing the need to place output into a file before printing it.For example, you could send the output from the ls command directly to a printer with the command:
ls | lpr
This is assuming that you have your PRINTER environment variable set. You can set your PRINTER environment variable in your .variables.ksh with PRINTER=printer_name. Workstations in the VLSI lab are set to print to the printer in the lab (unless you specify a PRINTER or use the -P option, as shown below).
Printing Commands Result enscript filename send to laser printer; many formatting
options are available; see man page for
complete information.lpq -Pprinter list print jobs for printer. lpr -Pprinter send files to printer. lprm job_id cancel print job job_id which is obtained
from the lpq command.manpage command send a copy of the manual page to default printer;
printer can be specified with -Pprinter_name
Appendix B: Some Utility Commands
Utility Commands Result cal month year return a calendar for month (digit 1-12)
and year (4 digits)calendar read a file named "calendar" in your HOME
directory containing dates and events; system
sends daily reminders for current dates via
electronic mailcut (-clist filename | -flist filename) return list of columns or fields from filename date return date and time of day df disk free; return available disk space in
kilobytes (Sun) or i-nodes and kilobytes (RS/6000)du directory disk usage; return number of blocks used by directory expand filename replace each tab character in filename with spaces file filename show the type of a file (e.g., ascii,
directory, data, C program, etc.)finger return information on machine users man [-k] command return a copy of the manual page for command;
with -k, does a keyword searchpaste file1 file2 concatenate corresponding lines of file1 and
file2 by joining them with a tab characternamtest machine return IP address of machine (RS/6000, Sun) rlogin machine log in to remote Unix machine script filename copy all subsequent screen output to
filename; type exit to end scriptingsort filename sort filename in ascending order;
many options availablespell filename return misspelled words stty return terminal status; can be used to
modify terminal optionsw return who is doing what on the system whence command search all the directories given in the PATH
environment variable for the first occurance
of command (Korn shell)who list who is logged on to the system whois string return information from user database