Basic Unix Guide
Linda DeBula
Linda Condron
Frank G. Fiamingo
University Technology Services
The Ohio State University
(C) University Technology Services, The Ohio State University, 406 Baker Systems, 1971 Neil Avenue, Columbus, OH 43210.
All rights reserved. You may reproduce all or parts of this document for personal, non-commercial use, so long as you attribute the work to the authors.
This publication is provided ``as is'' without warranty of any kind. This publication may include technical inaccuracies or typographical errors.
Getting Started
Logging in
After connecting with a UNIX system, a user is prompted for login, then password. The login is a username, or the user's unique name on the system. At the login prompt, the user should enter this username; at the password prompt, the assigned password is typed. Note: UNIX is case sensitive. Therefore, the login and password should be typed exactly as issued; the login at least will normally be in lower case.
Terminal Type
Most systems are set up so the user is by default prompted for a terminal type, which should be set to match the terminal in use before proceeding. Most computers work if you choose "vt100". Users connecting using a Sun workstation may want to use "sun"; those using an X-Terminal may want to use "xterms" or "xterm".
The terminal type indicates to the UNIX system how to interact with the session just opened.
Should you need to reset the terminal type, enter the command:
- setenv TERM <term type> - if using the C-shell (see Section 3.)
- (On some systems, e.g. MAGNUS, it's also necessary to type "unsetenv TERMCAP".)
- -or-
- TERM=<term type>; export TERM - if using the Bourne shell (see Section 3.)
where <term type> is the terminal type, such as vt100, that you would like set.
Getting Help
Manual (man) pages are available to explain the usage of the UNIX system and commands. To use a man page, type the command "man" at the system prompt followed by the command for which you need information.
Passwords
When your account is issued, you will be given an initial password. It is important for system and personal security that the password for your account be changed to something of your choosing. The command for changing a password is "passwd". You will be asked both for your old password and to type your new selected password twice. If you mistype your old password or do not type your new password the same way twice, the system will indicate that the password has not been changed.
Some system administrators have installed programs that check for appropriateness of password (is it cryptic enough for reasonable system security). A password change may be rejected by this program.
When choosing a password, it is important that it be something that could not be guessed -- either by somebody unknown to you trying to break in, or by an acquaintance who knows you. Suggestions for choosing and using a password follow:
- Don't use a word (or words) in any language
use a proper name
use information that can be found in your wallet
use information commonly known about you (car license, pet name, etc)
use control characters. Some systems can't handle them
write your password anywhere
ever give your password to *anybody*
- Do use a mixture of character types (alphabetic, numeric, special)
use a mixture of upper case and lower case
choose a password you can remember
change your password often
make sure nobody is looking over your shoulder when you are entering your password
Exiting
- ^D - indicates end of data stream; can log a user off. The latter is disabled on many systems
- ^C - interrupt
- ^Z - suspend the current process
bg - runs the suspended process in the background
fg - brings suspended or backgrounded process back to the foreground
- logout (or exit) - leaves the system
Directory Navigation
The UNIX file system is set up like a tree branching out from the root. The the root directory of the system is symbolized by the forward slash (/). System and user directories are organized under the root. The user does not have a root directory in UNIX; users generally log into their own home directory. Users can then create other directories under their home. The following are some directory navigation commands:
- mkdir (not md) make (or create) a directory
chdir (or cd) change directory
cd changes to user's home directory
cd / changes directory to the system's root
cd .. goes up one directory level
cd ../.. goes up two directory levels
cd /full/path/name/from/root changes directory to absolute path named (note the leading slash)
cd path/from/current/location changes directory to path relative to current location
(no leading slash)
cd ~username/directory changes directory to the named username's indicated directory
(Note: the ~ is not valid in the Bourne shell; see Section 3.)
rmdir (not rd) remove directory
pwd print working directory (where you are)
ls - list directory contents
- ls -[options] [argument]
When no argument is used, the listing will be of the current directory. There are many very useful options for the ls command. A listing of many of them follows. When using the command string the desired options together preceded by "-".
- -a lists all files, including those beginning with a dot (.).
- -d lists only names of directories, not the files in the directory
- -F indicates type of entry with a trailing symbol:
directories /
sockets =
symbolic links @
executables *
- -g displays UNIX group assigned to the file, requires the -l option (BSD only)
- -or- on a System V machine, e.g. Solaris, this option has the opposite effect
- -L if the file is a symbolic link, lists the information for the file or directory the link references, not the information for the link itself
- -l long listing: lists the mode, link information, owner, size, last modification (time). If the file is a symbolic link, an arrow --> precedes the pathname of the linked-to file.
- Each line begins with the mode field, which consists of 10 characters. The first character is one of the following:
- CHARACTER IF ENTRY IS A
d directory
- plain file
b block-type special file
c character-type special file
l symbolic link
s socket
- The next 9 characters are in 3 sets of 3 characters each. They indicate the file access permissions: the first 3 characters refer to the permissions for the user, the next three for the users in the UNIX group assigned to the file, and the last 3 to the permissions for other users on the system. Designations are as follows:
- r read permissions
w write permissions
x execute permissions
- no permissions
- There are a few less commonly used permission designations for special circumstances. These are explained in the man page for ls.
File Permissions
Each file, directory, and executable has permissions set for who can read, write, and/or execute it. To find the permissions assigned to a file, the ls command with the -l option should be used. Also using the -g option with "ls -l" will help when it is necessary to know the group for which the permissions are set.
When using the "ls -lg" command on a file, the output will appear as follows:
-rwxr-x--- user unixgroup size Month nn hh:mm filename
The area above designated by letters and dashes (-rwxr-x---) is the area showing the file type and permissions as defined in the previous Section. Therefore, a permission string, for example, of -rwxr-x--- allows the user (owner) of the file to read, write, and execute it; those in the unix group of the file can read and execute it; others cannot access it at all.
Changing Permissions
The command to change permissions on an item (file, directory, etc) is chmod (change mode). The syntax involves using the command with three digits (representing the user (owner) permissions, the group permissions, and other user's permissions) followed by the argument (which may be a file name or list of files and directories).
Each of the permission types is represented by a numeric equivalent:
- read=4, write=2, execute=1
A permission of 4 would allow read permissions. If the permissions desired are read and write, the 4 (representing read) and the 2 (representing write) are added together to make a permission of 6. Therefore, a permission setting of 6 would allow read and write permissions.
The syntax for chmod is
- chmod nnn [argument list]
where nnn are the three numbers representing user, group, and other permissions.
Example: If the permission desired for file1 is user: read, write, execute, group: read, execute, other: read, execute, the command to use would be
- chmod 755 file1
Reminder: When giving permissions to group and others to use a file, it is necessary to allow at least execute permission to the directories for the path in which the file is located. The easiest way to do this is to be in the directory for which permissions need to be granted:
- chmod 711 .
where the dot (.) indicates "this directory"
UNIX Utilities
One of the most important contributions UNIX has made to Operating Systems is the provision of many utilities for doing common tasks or obtaining desired information. UNIX utilities are written in such a way that output from one utility can be piped as input to another utility. Following is a brief sampling of the many utilities you can expect to find on all UNIX systems.
Output Redirection
A redirect takes the output of a command and places it into a named file
- > output redirect
cat file1 file2 > file3
- The above command concatenates file1 then file2 and redirects (sends) the output to file3. If file3 doesn't already exist it is created. If it does exist it will either be truncated to zero length before the new contents are inserted, or the command will be rejected, depending on the settings for the shell (see the noclobber option in Section 3). The original files, file1 and file2, remain intact as separate entities.
- >> append output
cat file1 >> file2
- Appends the contents of file1 to the end of what already exists in file2. (Does not overwrite file2).
- < input redirection
- | pipe output
command | command
- Makes the output of the first command the input of the second command
UNIX Command Line Structure
Command A command is a program that tells the UNIX system to do something
Command [options] [arguments]
An argument indicates on what the command is to perform its action
An option modifies the command, changing the way it performs
Options are generally preceded by a dash (-), and for most commands, more than one option can be strung together, in the form:
- command -[option][option][option]
e.g.:
- ls -alR
will perform a long list on all files in the current directory and recursively perform the list through all sub-directories.
Options and syntax for a command are listed in the man page for the command.
DOS Equivalents
Viewing a File
- cat concatenate - displays (without pause) the entire file(s) requested
- more displays (one screen at a time) the file requested
<space bar> to view next screen
<return> or <CR> to view one more line
q to quit viewing the file
h help
See the man page for additional options
- less similar to "more"; see the man page for options
- pg the System V equivalent of "more" (page)
- head displays the first few (default 10) lines of a file.
head -n displays the first n lines of a file
- tail displays the last few (default 10) lines of a file
tail -n displays the last n lines of a file
-
Frequently Used Commands
In general, for most Unix commands, file, as used in the table below, could be an actual file name, or a list of file names, or input/output could be redirected to/from the command.
Shells
The shell sits between you and the operating system, acting as a command interpreter. It reads your terminal input and translates the commands into actions taken by the system. The shell is analogous to command.com in DOS. When you log into the system you are given a default shell. When the shell starts up it reads it's startup files and may set environment variables, command search paths, and command aliases and executes any commands specified in these files.
The original shell was the Bourne shell, sh. It's still the only shell you can be certain will be on every Unix platform. It has very good features for controlling input and output, but is not well suited for the interactive user. To meet the latter need the C-shell, csh, was written and is now found on most Unix systems. It uses C type syntax, the language Unix is written in, but has a more awkward input/output implementation. It has job control, so that you can reattach a job running in the background to the foreground. It also provides a history feature which allows you to modify and repeat previously executed commands.
Numerous other shells are available from the network. Almost all of them are based on either sh or csh with extensions to provide job control to sh, allow in-line editing of commands, page through previously executed commands, provide command name completion and custom prompt, etc. Some of the more well known of these may be on your favorite Unix system: ksh and bash based on sh, tcsh and cshe based on csh. Below we'll describe some of the features of sh and csh so that you can get started.
Environment Variables
These are used to provide information to the programs you use. New shells normally inherit the environment of their parent shell. You can list the current environment variables with the "env" or "printenv" commands. Some common ones are:
- HOME Path to your home directory, e.g. /home/frank
- PATH Paths to be searched for commands, e.g. /usr/bin:/usr/ucb:/usr/local/bin
- SHELL The login shell you're using, e.g. /usr/bin/csh
- LOGNAME The name you login with, e.g. frank
- USER Your username, e.g. frank
- GROUP Your login group, e.g. staff
- EDITOR The path to your default editor, e.g. /usr/bin/vi
- TERM Your terminal type, e.g. xterm
- HOST The hostname of your system, e.g. nyssa
- DISPLAY The graphical display to use, e.g. nyssa:0.0
Many environment variables will be set automatically when you login. You can modify them or define others with entries in your startup files or at anytime within the shell. Some variables you might want to change are PATH and DISPLAY.
sh
Sh uses the startup file .profile in your home directory. There may also be a system-wide startup file, e.g. /etc/profile. If so, the system-wide one will be sourced (executed) before your local one.
A simple .profile could be the following:
- PATH=/usr/bin:/usr/ucb:/usr/local/bin:. # set the PATH
- export PATH # so that PATH is available to subshells
- # Set a prompt
- PS1="{\Qhostname\Q \Qwhoami\Q} " # set the prompt, default is "#"
- # functions
- ls() { /usr/bin/ls -sbF "$@";}
- ll() { ls -al "$@";}
- # Set the terminal type
- stty erase ^H # set Control-H to be the erase key
- eval \Qtset -Q -s -m ':?xterm'\Q # prompt for the terminal type, assume xterm
- #
- umask 077
Whenever a # symbol is encountered the remainder of that line is treated as a comment. In the PATH variable each directory is separated by a colon (:) and the dot (.) specifies that the current directory is in your path. If the latter is not set it's a simple matter to execute a program in the current directory by typing:
- ./program_name
It's actually a good idea not to have dot (.) in your path, as you may inadvertently execute a program you didn't intend to when you cd to different directories.
A variable set in .profile is set only in the login shell unless you "export" it or source the file from another shell. In the above example PATH is exported to any subshells. You can source a file with the built-in "." command of sh, i.e.:
- . ./.profile
You can make your own functions. In the above example the function ll results in an "ls -al" being done on the specified files or directories.
With stty the erase character is set to Control-H (^H), which is usually the Backspace key.
The tset command prompts for the terminal type, and assumes "xterm" if we just hit <CR>. This command is run with the shell built-in, eval, which takes the result from the tset command and uses it as an argument for the shell. In this case the "-s" option to tset sets the TERM and TERMCAP variables and exports them.
The last line in the example runs the umask command with the option such that any files or directories you create will not have read/write/execute permission for group and other.
For further information about sh type "man sh" at the shell prompt.
csh
Csh uses the startup files .cshrc and .login. Some versions use a system-wide startup file, e.g. /etc/csh.login. Your .login file is sourced (executed) only when you login. Your .cshrc file is sourced every time you start a csh, including when you login. It has many similar features to .profile, but a different style of doing things. Here we use the set or setenv commands to initialize a variable, where set is used for this shell and setenv for this and any subshells. The environment variables: USER, TERM, and PATH, are automatically imported to and exported from the user, term, and path variables of the csh. So setenv doesn't need to be done for these.
A simple .cshrc could be:
- set path=(/usr/bin /usr/ucb /usr/local/bin ~/bin . ) # set the path
- set prompt = "{`hostname` `whoami` !} " # set the primary prompt, default is "%"
- set noclobber # don't redirect output to existing files
- set ignoreeof # ignore EOF (^D) for this shell
- set history=100 savehist=50 # keep a history list and save it between logins
- # aliases
- alias h history # alias h to "history"
- alias ls "/usr/bin/ls -sbF" # alias ls to "ls -sbF"
- alias ll ls -al # alias ll to "ls -sbFal" (combining these options with those for "ls" above)
- umask 077
Some new features here that we didn't see in .profile are noclobber, ignoreeof, and history. Noclobber indicates that output will not be redirected to existing files, while ignoreeof specifies that EOF (^D) will not cause the login shell to exit and log you off the system.
With the history feature you can recall previously executed commands and re-execute them, with changes if desired.
An alias allows you to use the specified alias name instead of the full command. In the "ls" example above, typing "ls" will result in "/usr/bin/ls -sbF" being executed. You can tell which "ls" command is in your path with the built-in which command, i.e.:
- which ls
- ls: aliased to /usr/bin/ls -sbF
A simple .login could be:
- # .login
- stty erase ^H # set Control-H to be the erase key
- set noglob # prevent file name expansion
- eval `tset -Q -s -m ':?xterm'` # prompt for the terminal type, assume "xterm"
- unset noglob # re-enable file name expansion
Setting and unsetting noglob around tset prevents it from being confused by any csh filename expansions.
Should you make any changes to your startup files you can initiate the change by sourcing the changed file. For csh you do this with the built-in source command, i.e.:
- source .cshrc
For further information about csh type "man csh" at the shell prompt.
Job Control
With csh you can put jobs into the background at anytime by appending "&" to the command, as with sh, or after submitting the command you can type ^Z (Control-Z) to suspend the job and then "bg" to put it into the background. To bring it back to the foreground type "fg".
The built-in jobs command then allows you to list your background jobs. You can bring any to the foreground with "fg %n", replacing "n" with the job number from the output of "jobs".
Changing your Shell
To change your shell you can usually use the "chsh" or "passwd -s" commands. Sometimes this feature is disabled. If you can't change your shell check with your System Administrator. The new shell must be the full path name for a valid shell on the system.
A Short UNIX Bibliography
Highly Recommended
UNIX for the Impatient, Paul W. Abrahams & Bruce R. Larson (Addison-Wesley Publishing Company, 1992, ISBN 0-201-55703-7). (A current favorite. Recommended in the CIS Department for UNIX beginners.)
UNIX in a Nutshell for BSD 4.3: A Desktop Quick Reference For Berkeley (O'Reilly & Associates, Inc., 1990, ISBN 0-937175-20-X). (A handy reference for BSD.)
UNIX in a Nutshell: A Desktop Quick Reference for System V & Solaris 2.0 (O'Reilly & Associates, Inc., 1992, ISBN 0-56592-001-5). (A handy reference for SysV and Solaris 2.)
The UNIX Programming Environment, Brian W. Kernighan & Rob Pike (Prentice Hall, 1984). (A classic. For serious folks.)
When You Can't Find Your UNIX System Administrator, Linda Mui (O'Reilly & Associates, Inc., 1995, ISBN 1-56592-104-6).
Assorted Others
Understanding UNIX: A Conceptual Guide, James R. Groff & Paul N. Weinberg (Que Corporation, 1983).
Exploring the UNIX System, Stephen G. Kochan & Patrick H. Wood (SAMS, a division of Macmillan Computer Publishing, 1989, ISBN 0-8104-6268-0).
UNIX for Dummies, John R. Levine & Margaret Levine Young (IDG Books Worldwide, Inc., 1993, ISBN 0-878058-58-4).
A Practical Guide to UNIX System V, Mark G. Sobell (The Benjamin/Cummings Publishing Company, Inc., 1985, ISBN 0-80-530243-3).
UNIX Primer Plus, Mitchell Waite, Donald Martin, & Stephen Prata, (Howard W. Sams & Co., Inc., 1983, ISBN 0-672-30194-6).
An Introduction to Berkeley UNIX, Paul Wang, (Wadsworth Publishing Company, 1988).
Unix Shell Programming, Stephen G. Kochan & Patrick H. Wood (Hayden Book Co., 1990, ISBN 0-8104-6309-1).
The Unix C Shell Field Guide, Gail Anderson and Paul Anderson (Prentice Hall, 1986, ISBN 0-13-937468-X).
A Student's Guide to UNIX, Harley Hahn. (McGraw-Hill, 1993, ISBN 0-07-025511-3).
vi Quick Reference Guide
All commands in vi are preceded by pressing the escape key. Each time a different command is to be entered, the escape key needs to be used. Except where indicated, vi is case sensitive.
emacs Quick Reference Guide
Emacs commands are accompanied either by simultaneously holding down the control key (indicated by C-) or by first hitting the escape key (indicated by M-).
Last Modified: 02:50pm EDT, September 27, 1995