Introduction to UNIX

Most of the material in this section comes from the UW site…..

http://www.washington.edu/computing/unix/

Please go to this site to see a more complete introduction to UNIX and using UNIX at the UW.

New users

UNIX Commands

Shells

UNIX file system

 

 

Unix for New Users

If the Unix operating system is new to you, there are certain commands and characters it is useful to know about since they are used on a regular basis. You should be aware that the entire "Unix User's Manual Reference Guide" to commands is available for viewing online. So, for detailed information about any of the commands described below, as well as all other Unix commands, at the Unix prompt you can enter

man commandname

Where commandname is the name of the command about which you wish information.

If you are interested in a particular function and it is not a command name, or you wish to see all commands that apply, you can enter

apropos keyword

Where keyword is the identifying string you wish to know about.

Be careful using this command - if you use something very common as the keyword argument, you will be shown every possible place the string appears. For example, `apropos ls' will show you every command that has the letters ls somewhere in its description, whether these apply to `listing' something or not.

Another source of information is the C&C help notes, of which this note is one. To see a complete list of the available notes enter help -l or help index

To view a particular note, enter

help note

where note is the name of the topic.

Remember, UNIX COMMANDS ARE CASE SENSITIVE!

Commonly Used Unix Commands

      cat             - concatenates files and displays them
                        on your screen
      more            - displays named file(s), pausing for
                        each page to be read
      ex              - invoke basic Unix line text editor
      vi              - invoke full screen-oriented version
                        of ex
      finger          - user information lookup program
      ls              - list contents of directory
      mail            - send and receive mail
      msgs            - system messages and junk mail program
      passwd          - change login password
      ts, tset        - set terminal modes; use ts, it is a useful alias
      who             - who is on the system

Special Unix Characters

      DELETE          Backspace over and erase the
                      last character typed in.
      ^C              Interrupt the current program.
      ^U              Kill current line and start over.
      ^S              Pause while printing something.
      ^Q              Continue after pause.
      ^R              Retype the current line.
      ^W              Erase last word typed in.
      ^Z              Suspend job (resume via `fg' -

Viewing a File

"More"-ing: Some programs (such as help and man) use a mechanism called "more" to write long files onto your terminal. If you enter the command

  more filename

a page of the file `filename' will appear on your terminal, followed by:

      --More--

To continue displaying the file, press `d' or the space bar. To stop, type `q' or press ^C.

Commands to Get Information

     man yyyy          Show UNIX Programmer's Manual entry
                       on topic `yyyy'
     apropos keyword   Lists commands relevant to keyword.
     msgs              Read system messages
     msgs 95           Read system  messages starting at
                       number 95.
     msgs -3           Read last 3 system messages

Commands for File Manipulation

     ls              List files in the current directory.
     ls -al          Long list of all files in current directory.
                     (this list includes files whose names
                      begin with `.')
     cat f1 f2 ...   Print the files `f1', `f2', ... on
                      the terminal.
     more f1 f2 ...  Print the files `f1', `f2', ... on
                      the terminal, one screenful at a time.
     cp f1 f2        Copy file `f1' to file `f2'
     ln f1 f2        Link file `f1' to the alias name `f2'
                      (note that ln provides two names for the
                      same file, whereas cp makes a separate
                      copy of the file).
     mv f1 f2        Move (that is, rename) file `f1'
                      to new name `f2'.
     rm f1 f2 ...    Remove (that is, delete) files `f1', `f2',
                      ...

Commands for Directory Manipulation

     pwd             Give full pathname of current directory.
     cd d1           Change to directory `d1'
     mkdir d1 d2 ...  Make new directories called `d1', `d2', ...
     rmdir d1 d2 ... Remove directories `d1', `d2', ...
                      the directories can be removed only if they
                      are empty (use `rm' to remove the files
                      first)
     ls d1 d2 ...    List files in directories `d1', `d2', ...

Special Shell Characters: < > >> | &

     a.out < f1      Program `a.out' reads from file `f1'
                      instead of terminal
     a.out > f2      Program `a.out' writes to `f2' instead
                      of the terminal.
     a.out >> f      `a.out' appends to file `f' instead
                      of overwriting it.
     a.out <f1 >f2   These may be combined; spaces don't matter.
     a.out | b.out   The output of `a.out' is the input for `b.out'
     a.out &         run `a.out' in background
     a <f | b >g &   Another possible combination.

C&C Commands

Computing & Communications has added several commands to its Unix systems:

     prt             'prt' is the standard C&C print utility
                     available on all the C&C computers
                     and is the preferred way to print file
                     from any of the C&C systems (use 'prt'
                     instead of 'lpr'). The 'prt' command is
                     described in the 'prt' man page.
     chkout          'chkout' is a command used to check out
                     temporary disk space for those times
                     when your short term need for disk space
                     exceeds your disk space allocation.
                     'chkout' is described in 'man chkout'
                     and 'help chkout'.
     ted             'ted' is a full-screen editor that some
                     people find easier to use than 'vi'.

 

More UNIX commands

 

Unix Commands

This document provides examples of the use of many of the most common Unix commands. The first thing to remember about Unix commands is that are case sensitive.

For more details on a command see the man page for the command. For example, to see more information about the command 'rm', enter the command

      man rm

To display a list of commands related to a topic enter

      man -k  topic

or

      apropos  topic

Additional information is available on the C&C Unix mainframes in the form of help notes. To see a list of all available help notes, enter the command

      help -l

To see a specific help note, enter

      help  topic

where 'topic' is a specific topic, such as 'tapes'.

You should also understand that some commands you may be looking for are not part of the basic Unix operating system but, instead, belong to the shell that interprets your commands. The most commonly used shell is the C-shell, which is described in the 'man csh' page. For example, the following commands are described in the 'man csh' page and do not have separate man pages of their own:

      repeat
      setenv
      history
      alias
      < << >> > | (I/O redirection and piping)

... and many others.

Remember: UNIX COMMANDS ARE CASE SENSITIVE!

Your Account

      passwd            ... set a new password for your account
      assets            ... view information on resource use
                        by your account, including connectivity
                        time, cpu use, and disk space. (Available
                        on C&C systems only.)

Directories

        DO:                        TO:
      ---------------   ---------------------------------------------
      pwd               ... find out what your current working
                        directory is
      mkdir playdir     ... create a new subdirectory called
                        'playdir'
      rmdir junkdir     ... remove subdirectory 'junkdir' (must be
                        empty of files and subdirectories)
      cd /usr/bin       ... change to '/usr/bin' directory
                        (directory specification is relative to
                        the root directory)
      cd class/cs108    ... change to a subdirectory named
                        'class/cs108' (directory specification is
                        relative to current working directory)
      cd ..             ... change to directory one level above
                        current working directory
      cd                ... change to home directory for the account
                        you are using
      du                ... display the number of disk blocks in use
                        (the total combined size of all files)
                        in each directory and subdirectory.

Files

        DO:                        TO:
      ---------------   ---------------------------------------------
      ls                ... list files in the current working
                        directory
      ls notes          ... list file in the subdirectory
                        named 'notes'
      ls -l             ... list all files in the current working
                        directory, along with each file's
                        permission, owner, size in bytes and date
                        of last modification.
      ls -a             ... list files in the current working
                        directory, including dot files (those
                        files with names beginning with a period)
      ls -F             ... list files in the current working
                        directory, indicating executable files
                        with an asterisk (*) and subdirectories
                        with a /
      find docs -name \\*\.memo -print
                        ... list all files in the directory
                        'docs' and any subdirectories of 'docs'
                        with filenames ending in '.memo'. The
                        \ is necessary before the * to insure
                        that is is interpreted as a wild character.
      cp file1 file2    ... copy the file 'file1' to a file named
                        'file2' (original file remains intact with the
                        same name)
      mv oldf newf      ... move the file 'oldfile' to a file named
                        'newfile' (equivalent to renaming a file)
      rm badfile        ... remove the file 'badfile' (the file is
                        permanently removed and cannot be recovered)
      rm -i *.c         ... remove all files in the current directory
                        with the suffix '.c' and be asked for
                        confirmation on each file
      chmod a+r resul   ... grant read access of the file 'resul'
                        to all users
      cat shortfile     ... display the file 'shortfile'
      more longfile     ... display the file 'longfile', a
                        screenful at a time
      head big          ... display the first ten lines of
                        file 'big'
      head -25 big      ... display the first 25 lines of file 'big'
      tail big          ... display the last ten lines of file 'big'
      tail -25 big      ... display the last 25 lines of file 'big'
      grep done tasks   ... display all lines within file 'tasks'
                        containing the string 'done'

Jobs

        DO:                        TO:
      ---------------   ---------------------------------------------
      ps                ... list the status of your jobs by
                        process identifier (PID)
      ps -aux           ... list the status of all jobs by process
                        identifier (PID)
      jobs              ... list the status of all jobs by job
                        number
      <CTRL>z           ... suspend the job currently running in
                        the foreground
      bg                ... resume the most recently suspended job
                        into the background
      bg %2             ... resume job number 2 in the background
      fg %2             ... resume job number 2 in the foreground
      fg %3             ... bring job number 3, which is running
                        in the background, into the foreground
      kill %2           ... kill job number 2
      kill 1234         ... kill job with PID 1234

Printing from C&C Uniform Access Computers

        DO:                        TO:
      ---------------   ---------------------------------------------
      prt -printers     ... list available printers
      sed 's/^/     /' Memo | prt
                        ... shift the file 'Memo' five columns
                        and send it to the printer. The following
                        is a detailed description of the command:
                             sed 's/^/     /' Memo | prt
                                  ^ ^ -----    ^   ^  ^
                                  | |   |      |   |  |
                         substitute |   |      |   |  |
                          at beginning  |      |   |  |
                          of each line  |      |   |  |
                              five spaces      |   |  |
                                       file name   |  |
                                             pipe to  |
                                                  print
      repeat 5 prt class.notes
                        ... print five copies of the files
                        'class.notes'. The command 'repeat'
                        is a C shell command.

Networking

        DO:                        TO:
      ---------------   ---------------------------------------------
      telnet becker.u   ... establish an interactive session on
                        computer 'becker'
      ftp sun.latin.washington.edu
                        ... transfer a file to or from computer
                        'sun.latin.washington.edu'

Communicating With Others

        DO:                        TO:
      ---------------   ---------------------------------------------
      who               ... find out who is logged on to the
                        computer
      w                 ... find out who is logged on to the
                        computer and what they are doing
      finger suzz       ... display information about user 'suzz'
      biff y            ... be notified if mail arrives. To turn
                        off notification, enter the command 'biff n'
      pine		 ... start pine mailer.  Pine is an alternative
                 to 'mail' that many people find easier to use
                 than 'mail'

Miscellaneous

        DO:                        TO:
      ---------------   ---------------------------------------------
      cal 6 1990        ... display a calendar of June, 1990.
      date              ... display current date and time
      script            ... start recording of all screen
                        interactions
      exit              ... stop script recording (text from
                        recorded session will be in a file named
                        typescript )
      alias dir ls -Fal ... alias 'dir' to represent the command
                        'ls -Fal'
      unalias dir       ... remove the alias for 'dir'
      alias             ... show all current aliases

Control Codes (Used When at Unix Command Line)

        DO:                        TO:
      ---------------   ---------------------------------------------
      <CTRL>u           Delete entire line
      <CTRL>w           Delete the preceding word
      <CTRL>h           Delete the preceding character
      <CTRL>c           Abort the program currently running
      <CTRL>z           Suspend the program currently running (use
                        fg or bg to resume the program in the
                        foreground or background, respectively)

Redirecting and Piping

      cc myprog.c > listing
                       ... run C compiler on 'myprog.c' source file,
                       redirecting compilation messages to a file
                       named 'listing'. The resulting file would NOT
                       include any diagnostic messages.
      cc myprog.c >& listing
                       ... run C compiler on 'myprog.c' source file,
                       redirect compilation messages, including
                       diagnostic messages, into a file named 'listing'.
      ps -aux | more   ... list all current jobs on the system,
                       piping the result to more for viewing one
                       screen at a time
      cat frog >> rat
                       ... concatenate the contents of file 'frog'
                       onto the end of file 'rat'

Shells and Shell Scripts

A Unix shell is a command language interpreter, the primary purpose of which is to translate command lines typed at a terminal into system actions. The shell itself is a program through which other programs are invoked. Although there are several different Unix shells, among them the C shell (csh), the Bourne shell and the Korn shell, the one most frequently used on Blake within Berkeley UNIX is the C shell.

The shell has some built-in functions, which it performs directly, but most commands that you enter cause the shell to execute programs that are external to the shell. This sets the shell apart from other command interpreters, because it is just another user program at the same time that it functions almost exclusively as a mechanism for invoking other programs.

For more information about the C shell, refer to the article ``An Introduction to the C Shell,'' by William Joy, in the Unix User's Manual Supplementary Documents volume. Also see `man csh' online.

What is a Script?

One handy thing you can do with the shell is to use standard UNIX commands as building blocks to create your own new commands. To do this, you write a `shell script', which can contain a number of commands, and then the file can be executed as one command.

Following is an example of a very simple shell script, which gives a brief indication of the types of things you might do. To use this feature to its maximum potential, however, you will need to refer to the documentation mentioned above, or other Unix manuals.

Suppose your name is joanx and occasionally you want the system to tell you your user id, what directory you are in, list the files in that directory, and display the date. You want each of these pieces of information to be preceded by an appropriate statement. The individual Unix commands you need are `echo,' `who am i,' `pwd,' `ls,' and `date.' So you would create a a regular text file (let's call it `me') with the following lines in it:

     echo you are
     who am i
     echo The directory you are in is
     pwd
     echo The files in this directory are
     ls
     echo The date is
     date

You (joanx) can execute the file called me, when you are in the same directory it is in, simply by entering

     sh me

When you do this, the following information might be displayed on your screen:

     you are
     joanx   ttyB7   Jun  9 14:51
     The directory you are in is
     /b2/joanx/mine
     The files in this directory are
     herfile  hisfile  myfile  ourfile
     The date is
     Fri Jun  9 16:10:50 PDT 1989

Once you learn to create and use them, you should see that shell scripts are useful Unix tools that might save you a lot of time in your day-to-day computing.

More on Scripts

Virtually any command which can be executed at the unix prompt can be executed within a shell script.

Structure

Each line of the script file is a single command. There are two exceptions to this statement. The first is when a line ends with a backslash "\\", since the backslash removes the significance of the newline character as a command terminator. Therefore, a backslash at the end of a line is the indication of command continuation on the following line, just as it is at the prompt.

The second exception is when the "<<" I/O redirection symbol is used in a script file. This symbol will be discussed below.

Comments

Comments in C shell scripts begin at a pound sign "#" and continue to the end of the line. Since most Unix system support several shells, it is recommended that each shell script start with a comment indicating which shell the script was written for.

Output

Text can be sent to standard output from within a shell script by using the c-shell command 'echo'. This command will print the value of its arguments on standard output appending a newline. The -n option to echo will suppress the newline at the end of the text.

Examples

The following script will print out the name and contents of the current working directory.

     # print the name and contents of the current
     # working directory
     echo "This is from a shell script:"
     pwd     # print the directory name
     ls      # print the directory contents

The following script will edit a C source code file named "count-c", compile and link it producing an executable version named "count", and finally run count.

     # edit compile link, and run
     vi count.c
     cc count.c -o count
     count

Running Scripts

There are three ways to execute a c-shell script.

Direct Execution

The simplest way to execute a shell script is to used chmod to make the text file executable. The script can then be executed by typing its name if it is in the current working directory or is in the path for that shell.

Executing a script creates a new shell and executes each command in the script within the new shell. When the end of the script file is encountered, the new shell exits. Any changes in the new shell caused by the script are lost when the shell exits.

For example, if the file
/usr/samples/script/change contains...

     cd/usr/games
     pwd
     fortune

Then the command sequence would yield the following output:

     saul> cd   /usr/samples/script
     saul> pwd
     /usr/samples/script
     saul>  chmod  +x change
     saul>  change
     /usr/games
     To err is human. To forgive, divine.
     saul>  pwd
     /usr/sample/script

csh Execution

Another way to execute a script within the context of a new shell is to run csh and give it the name of the script file as its first argument. This is, in fact, how direct execution is implemented. By executing csh directly, the default csh options can be modified. Then the command sequence would yield the following output:(user input is in bold) (the -x option causes echo to be set, i.e each command will be echoed before it is executed)

     saul>  cd  /usr/samples/script
     saul>  pwd
     /usr/samples/script
     saul>  csh  -x change
     cd /usr/games
     pwd
     /usr/games
     fortune
     To err is human. To forgive, divine.
     saul>  pwd
     /usr/samples/script

source Execution

The third way to execute a script will execute it within the context of the current c-shell, using the c-shell command source and giving it the name of the script file as its first argument. Since execution takes place within the context of the current shell, any changes in the shell are retained following the completion of the shell. Then the command sequence would yield the following output:

     saul>  cd  /usr/samples/script
     saul>  pwd
     /usr/samples/script
     saul>  source change
     /usr/games
     To err is human.  To forgive, divine
     saul>  pwd
     /usr/games

Non-script Execution

There are several ways to execute commands within shells or at the command prompt.

SEQUENTIAL COMMANDS

A series of independent commands can be grouped together as a single command by separating them by semicolons ";".

PIPES

Pipes "|" also cause the execution of multiple processes from a single command line. With a pipe, the standard output of the process before the pipe is redirected to the standard input of the process after the pipe. This is also covered below, in I/O Redirection.

     saul>  /fortune | tee temp  ; wc temp ; rm temp
     Save the whales.  Collect the whole set.
            1       7      41

INLINE EXECUTION

Grave accents "`" (on the same key as the tilde "~") will force inline execution of the enclosed command. Like the quotation marks "''" and the apostrophes "'", grave accents must be used in pairs. Grave accents are used where a command "is not expected".

     saul>  set code = ( `ls *.c`)

SUBSHELLS

Any commands enclosed within parentheses are run in a separate shell, similar to the "direct" or "csh" execution of a shell script. As you can see, technically, this is not a means of running a shell script at all, but it is similar. The following sequence of commands illustrates this. Recall the use of the semicolon to separate sequential commands.

     saul>  cd   /usr/samples
     saul>  pwd
     /usr/samples
     saul>   (cd  ; pwd)
     /users/unix0
     saul>  pwd
     /usr/samples
     saul>   cd  ; pwd
     /users/unix0
     saul>  pwd
     /users/unix0

DEFERRED EXECUTION

There are times when variable substitution must occur before command execution. The c-shell command eval causes execution to be deferred until after variable substitution. Variable substitution is discussed in greater detail in the next section.

Redirection

Input and output redirection is available within shell scripts.

Input and Output Redirection

Input redirection will read from the named file instead of from the terminal. Input redirection uses the symbol "<". Output redirection uses the symbol ">" to create the named file to contain the output of the process. Output redirection can clobber (overwrite) an existing file. Output will be appended to the named file when the output redirection symbol ">>" is used.

Pipes and Filters

Piping connects the output of one process to the input of a second. Piping uses the symbol "|". A filter is a process which is between two pipes. It simply changes the information coming down the pipe.

Standard input for a process can be drawn from the script file itself. This uses the special redirection symbol "<<". The name which follows the redirection symbol is a tag for the end of the input. That is, the contents of the script file will be read as standard input to the process until a input line matching the name following the redirection symbol.

If the file /usr/samples/script/redirect contains:

     cd
     echo "Here are some commands:"
     sort << end > temp
     pwd
     ls
     clear
     passwd
     end
     cat temp

The following is an example of using the 'redirect' script:

     saul>  cd /usr/samples/script
     saul>  redirect
     Here are some commands:
     clear
     ls
     passwd
     pwd
     saul>  pwd
     /usr/samples/script


 

The UNIX file system

http://www.geek-girl.com/Unixhelp/concepts/strctfile1.html

 

UNIX system directories

The standard system directories are shown below. Each one contains specific types of file. The details may vary between different UNIX systems, but these directories should be common to all. Select one for more information on it.

                              /(root)
                                 |
 --------------------------------------------------------------
 |         |       |       |        |       |       |         |
 /bin    /dev    /etc    /home    /lib    /tmp    /usr   kernel file


The /bin directory

This directory contains the commands and utilities that you use day to day. These are executable binary files - hence the the directory name bin.

Often in modern UNIX systems this directory is simply a link to /usr/bin.

 

The /dev directory

This directory contains special files used to represent real physical devices such as printers and terminals. One of these files represents a null (non-existent) device.


The /etc directory

This directory contains various commands and files which are used for system administration. One of these files - motd - contains a 'message of the day' which is displayed whenever you login to the system.


The /home directory

This directory contains a home directory for each user of the system.


The /lib directory

This directory contains libraries that are used by various programs and languages.

Often in modern UNIX systems this directory is simply a link to /usr/lib.


The /tmp directory

This directory acts as a "scratch" area in which any user can store files on a temporary basis.


The /usr directory

This directory contains system files and directories that you share with other users. Application programs, on-line manual pages, and language dictionaries typically reside here.


The Kernel

As its name implies, the kernel is at the core of each UNIX system and is loaded in whenever the system is started up - referred to as a boot of the system.

It manages the entire resources of the system, presenting them to you and every other user as a coherent system. You do not need to know anything about the kernel in order to use a UNIX system. This information is provided for your information only.

Amongst the functions performed by the kernel are: