Introduction to Unix


CONTENTS


INTRODUCTION

This document is intended to help the new user of computers that run a version of the Unix operating system. It is primarily aimed at users of the ITC clusters, which run a version of Unix called Linux.

The command interpreter described in this document is the bash shell, which acts as an interface between the user and the Unix system. Bash is standard on Linux systems, but currently many users use the Korn shell (ksh). Users may change shell on Blue, but the commands typed at the command line are nearly identical for ksh and bash; the main difference is in support for features such as filename completion and history.


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. Make sure your caps-lock key is not engaged. 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.


GETTING STARTED

Connecting to a Unix Computer

To connect to a remote Unix machine, you must install a client for Secure Shell (ssh) on your local computer. For Windows we recommend SecureCRT, available for UVa affiliates through Software Central. Mac OSX has ssh built in; start the Terminal application and type ssh mst3k@hostname.itc.virginia.edu. Replace the examples with your own information, of course.

Logging In and Changing Your Password

If you wish to change your password, log on to blue.unix.virginia.edu and type passwd. It will ask for your old password and after verifying it, will ask for your new choice. 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 confirmation.

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 not by itself 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 the first letters of a phrase you can remember, with digits and special characters added and/or substituting for letters.

Terminating and Pausing a Command, Halting Screen Output and Logging Out

Terminating and Pausing a Command

  • If you want to stop a command before it completes execution, press the control and c keys simultaneously (usually denoted by ^C or control-c). This should return you to your initial prompt.

  • To pause a process, type control-z. To resume it, type fg (foreground). To send it to the background, i.e. to have it continue to run but to regain the prompt, type bg.

  • 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.

Halting Screen Output

  • If you want to temporarily halt output to your screen without terminating the execution of the command, enter Ctrl-s. Start output again by entering Ctrl-q.

Logging Out

  • When you are ready to log out, type exit.

Tutorial

  • We strongly recommend that beginners go through the tutorials, especially the first four.


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.

      Unix file system structure

In the Unix system, all the commands you type, as well as peripheral devices such as disk, 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

/home

user directories

/uva

locally installed utilities

You will be assigned a directory with your login ID as its name when you are given an account. The user accounts reside in the /home directory. When you complete the login process, you will be attached at the directory /home/your-login-id in the file tree. Your login directory is called your HOME directory, where $HOME is one of the Unix system's environment variables. Environment variables are a type of shell variable that apply to the user's current shell and all subshells.

You can create other directories and files below your HOME directory as you need them. For instance, if your login ID is "mst3k," you may be assigned a HOME directory with the name /home/mst3k. If you create a subdirectory 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

/home/mst3k/docs/chap1.thesis


Unix Command Syntax

The typical syntax of a Unix command is

command arguments files

where the command options, also called arguments, are usually preceded by a hyphen (-). More than one file name may sometimes be given. In our notation, optional arguments will be surrounded by square brackets ([ ]) in the documentation on each command.

The table below 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 or more) 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


Table of File Commands

Command

Meaning

Result

cat filename(s)

catenate

list contents of filename(s) to the screen,
one after the other

cd directory

change directory

change working directory to directory;
if directory is omitted, changes to your HOME directory

cp file1 file2

copy

make a new copy of file1 called file2

diff file1 file2

difference

list the differences between file1 and
file2

find dir -name file -print

find

find files beginning search at directory
dir matching file and print the entire
pathname

grep [-i] string file(s)

global regular
expression processor

search file(s) for string and list
path and line if found; use -i option
to ignore case of string

head filename(s)

 

list first ten lines of filename(s)

ls [-AlpsCF]

list

return a list of the files in
the current directory (see example below)

-A all entries, including files beginning with period (.)
-l long format
-p put slash (/) after directories
-s give size in kilobytes
-C multi-column listing (with short
     format)
-F put slash (/) after directories, asterisk (*) after executables

mkdir dir

make directory

create a new directory named dir

more filename(s)

 

list contents of filename(s) a
screenful at a time; scroll
forward with the space bar, back
with b, end with q or Ctrl-c.

mv name1 name2

move

rename file name1 to become
name2 or move a file name1 to
directory name2

page filename(s)

 

same as more

pwd

print working directory

list the pathname of the working
(current) directory

rm [-i]

remove

delete a file; the -i option queries
for y or n response; default n
(do not delete)

rmdir directory

remove directory

delete directory;
directory must be empty

tail filename(s)

 

list last ten lines of filename(s)

tee filename(s)

 

input to tee is sent to both
filename and standard output

wc filename(s)

word count

list the number of lines, words
and characters in filename(s)


The ls Command

Sample output from the command ls -Al 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    mst3k   usr   4193     Jun 10   10:11    .history
   -rw-r--r--   1    mst3k   usr    626     Jun  6   12:08    .kshrc
   -rw-r--r--   1    mst3k   usr    338     Mar 17   16:43    .mailrc
   -rw-r--r--   1    mst3k   usr   9156     Nov  9   13:06    .newsrc
   -rw-r--r--   1    mst3k   usr    800     Jun  6   14:28    .profile
   -rw-r--r--   1    mst3k   usr   1424     Jun  6   14:28    .variables.ksh
   drwxr-xr-x   2    mst3k   usr    144     Jun  9   16:21    News
   drwxr-xr-x   4    mst3k   usr    144     Apr 16   14:34    docs
   drwxr-xr-x   2    mst3k   usr     64     Jun  3   10:34    fortran_progs
   -rw-r--r--   1    mst3k   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 = directory
l = symbolic link to a file
- = ordinary file

The next nine characters specify the file permissions given to user mst3k (first three characters), the group to which mst3k belongs (next three), and the other users on the system (last three). At UVa, the group to which the user belongs will generally be the same as all other users.

The permission characters are as follows:

r for read
w for write
x for execute
- for permission denied

If all permissions are given for a file, these entries would be

rwxrwxrwx

which 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 and 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          (mst3k in the example above)
               g = group         (usr in the example above)
               o = other
               a = all (default)

    and

               + = gives permission
               - = denies permission

    and permission is

               r = 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 commands:

chmod ugo+rx printit chmod a+rx printit


Some Special Characters

Some characters have special meaning to the Unix system's 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).


INPUT/OUTPUT AND REDIRECTION

Every Unix process has three files that are normally associated with the console (in modern usage, the user's terminal); these files are called standard input (the keyboard, file descriptor 0), standard output (the screen, file descriptor 1), and standard error (file descriptor 2). One of the features provided by the Unix shell is the ability to redirect these "invisible" files to named disk files. Some of the characters used for redirection by the bash 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 another

The pipe is an example of how Unix commands can be chained together to accomplish very complex tasks in a single line.

Examples

  • To send all the Fortran compiler error messages to a file called "compile.out" rather than have them come to the screen:

    gfortran myprog.f >& compile.out

  • 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


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.

When you log in, the system reads .profile (ksh, bash) or .bash_profile (bash), or .login (tcsh) to set your environment. All subsequent non-login shells read .kshrc (ksh), .bashrc (bash), or .tcshrc (tcsh).

The following are some of the reconfiguration files you may see when you issue the ls -A command.

.profile

used by the Korn shell to set the user's environment at login

.variables.ksh

used by ITC to set values for certain shell variables

.kshrc

sets user's command aliases, if specified

.history

stores commands you have issued

Default .profile, .variables.ksh, and .kshrc files exist, but you can create your own versions of these files to set your initialization preferences. You can look at and modify any of these files, though you should not normally modify .bash_history, which holds a list of your previous commands.


Shell Variables

The shell sets up some special variables, called shell variables, that 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.

An an environment variable is a shell variable that is exported to child shells. It is conventionally written in upper-case letters (remember that Unix is case-sensitive) and its value is often a path to a directory or file. Envirionment variables are usually set in the .profile, .bash_profile, or .login files since children of the login shell will inherit them. (Note that Unix distinguishes between login and non-login shells.) Ordinary shell variables apply only to a given shell and are conventionally set in the .kshrc, .bashrc, or .[t]cshrc file.

Some shell variables are described below.

EDITOR

name of editor you use

ENV

specifies file to be executed at shell invocation;
good place for command aliases

HISTSIZE

number of commands stored (default is 128)

HOME

default is current directory when you log in; e.g., /home/abc3f

PATH

search path used to find executable files (commands)

PS1

prompt

The PATH environment variable has special importance since it names the directories (separated by colons) that the system will search 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 command not found will appear.

To see the current value of the PATH, issue the command:

printenv $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 all environment variables, issue the command printenv (bash or tcsh) or (ksh or bash) env.

The shell provides the user with command aliasing, which is a way to rename a command along with your favorite options to a name of your choice. To see the current aliases, issue the command alias with no parameters. The default ITC startup scripts define several aliases.


Terminating Runaway Processes

The ps command

The kill-9 processID command terminates process number processID, which is
determined by issuing the ps command. Use with discretion.


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 terminal



APPENDIX: 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 mail

cut (-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

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.)

man [-k] command

return a copy of the manual page for command;
with -k, does a keyword search

sort filename

sort filename in ascending order;
many options available