3.0 Concepts


3.1 The File System

Unix uses a hierarchical file system. The top of the hierarchy is the root directory /. Below this directory are the subdirectories such as /bin, which is short for binary(executable) and contain many of the utility programs, and /tmp which stores temporary files.

When you log into the system you are placed in your home directory. The home directory is where your personal files and directories reside. The location of the home directory is determined by the system administrator. For instance when the user scot starts his session he will be placed in the /home/huron2/admin/scot directory.

3.2 The Shell

The shell is the most commonly used utility program in Unix. Each time you execute a command the shell interprets the comand and directs the work that needs to be done to perform the command. The shell is an interface between the user and the rest of the system. Since the shell is nothing more than a utility program, you have a choice of shells.

Currently there are five major shells: Bourne, Korn, C-Shell, T-Shell and Bash. All of these support both foreground and background processes, pipes, filters, input/output redirection and other similar standard features of Unix.

The Bourne Shell was the original shell. It is the smallest shell and lacks many of the features of the other four shells. The Korn shell retains the complete functionality of the Bourne shell and includes some enhancements.

The C shell was developed at Berkeley and is useful for C language programmers. The T Shell retains the complete functionality of the C shell while adding many useful features, such as name completion and command line editing.

Bash or the GNU "Bourne Again Shell" incorporates many features of the Korn and C shell and follows the IEEE POSIX standard. Currently the T-shell is the most popular because it has "the most bells and whistles", however bash is gaining in popularity.

When a shell is started it looks for startup files that sets it behavior. The shell looks for system startup files and then it looks in your home directory($HOME) for personal startup files. These home startup files allows you to customize the behavior of the shell.

Table3.2: Shells
--------------------------------------------------------------------
Shell 		Binary  Default Prompt  Home startup files
--------------------------------------------------------------------
Bourne Shell 	sh	$		$HOME/.profile
Korn Shell	ksh	$		$HOME/.profile
C-shell 	csh	%		$HOME/.cshrc
T-shell 	tcsh	%		$HOME/.tcshrc or $HOME/.cshrc
Bash		bash	bash$		$HOME/.bashrc
--------------------------------------------------------------
If you did not ask for a specific shell your system administrator has probably assigned you one. You can determine your shell, amoung other things, by using the finger command.

3.3 Command Line Options and Arguments

You can vary the operation of certain commands by using command options. Options are usually preceded by a minus (-) sign followed by a letter of the alphabet.
% ls -l
total 1094
-rw-r-----   1 scot     admin        242 Mar  7 17:00 Makefile
-rwxr-xr-x   1 scot     admin     542036 Mar  7 17:00 hello
-rw-r--r--   1 scot     admin         70 Mar  7 17:00 hello.cc
-rwxr-xr-x   1 scot     admin         48 Mar  7 17:00 hello.pl
The ls command is used to list files and directories. The -l options causes ls to give you a "long listing"of the files in a directory. If a command has more than one option they can be bundled together using a single minus sign. Not only can commands have options they can also have arguments. The command arguments are usually files. However, they can also be variables with values or possibly words to be match.
% grep hello greetings.txt
The grep command does pattern searching. This command will display every line in the file greetings.txt that contains the pattern hello.

3.4 Wildcards or MetaCharacters

One of the most powerful features of the Shell, the capacity to use "shorthand" notation for operating upon whole aggregates of files and directories in a single command. For instance using the Shells notation you can refer to "all files that begin with the letter a" , or "all files that end in a number ", and so on. The characters that specify these options are called `wild-card` or `meta` characters.


Table3.4:  Wildcards
----------------------------------------------------------------------
*   Represent any arbitrary character string                            
?   Represents any single character                                     
[]  Encloses a list of characters where the match is on `any single     
    character enclosed in the bracket.'  The hyphen character is used   
    inside the bracket to indicate a range of characters.               
----------------------------------------------------------------------
We can use wildcards to search for specific files
% ls [0-9]*.txt
This command will list all the files that begin with the character 0 through 9 and end with the character string ".txt". Wildcards can be used in any Unix command. They are not limited to the ls command.

3.5 File Permissions

Unix has a standard method of allowing access to certain files and directories. The ls command with the -l option will display file permissions.

You can set the permission of the file to allow access by certain classes of users. These three classes are the user, the users group and other. The user is the person who created the file. A group is a combination of users that can be used to restrict access. For instance, all faculty members could be in a group that allows them to see priviledged information. The last class is other, which is anyone with a username on your system.

These classes of users can have different combinations of read, write, execute (for a file) or search(for a directory) permissions. All together each file has nine permissions: three for owner, three for the owner's group, and three for others.

Table3.5: Permissions Examples
------------------------------------------------------------------------------------
Permission  Meaning                                                       
------------------------------------------------------------------------------------
 rw-r--r--   file is read/write for user, read only for both
             the users group and other users
 rwx------   file is read/write/execute for the user only                     
 r-xr-xr-x   file is read/execute for user, group and other
dr-xr-x---   directory is read/search for the user and his group              
------------------------------------------------------------------------------------
The chmod command is used to change the permissions of a file or directory.

3.6 Redirecting Input/Output

Usually you are inputing from the keyboard and the output is going to the screen. However, you can redirect the input and output with the less-than (<) and greater-than (>) symbols, respectively.

% mailx scot < memo
The mailx command is one way to send electronic mail.This command will send the file memo to user scot.

% ls > listfile
This command will place a listing of all the file and directories into a file called listfile. If listfile already existed, previous contents of the file will be deleted. If you do not want to lose the contents of the file you could append to the end of the file by using two greater than symbols (>>).

We have discussed the standard input and standard output. However, Unix provides another output stream called standard error. The standard error is used to output any error messages that should not appear in the standard output, it is usually used for debugging purposes.

% ls nofile > listfile
nofile not found
%
Here we have tried to list a file that does not exist. Notice that the error message is not placed in the file listfile, however the listfile is created.
% ls nofile >&2 listfile
%
The &2 tells the shell to send the standard output and the standard error to the file listfile.

Table3.6: Input/Output Redirection
------------------------------------------------------------------------
Symbol  	Action				Shell
------------------------------------------------------------------------
<	Redirect input from			All
>	Redirect output to			All
>>	Redirect output and append to		All
2>	Redirect standard error	and output	Korn,Bourne,Bash
>&2	Redirect standard error and output	C-shell, T-shell
-------------------------------------------------------------------------

3.7 Connecting Commands with Pipes

All the commands you have seen have been single commands. One of the most powerful features of Unix is the ability to connect these commands together to form a sequence of operations. The tool to accomplish this is called a pipe, which is denoted by a vertical bar |. With a pipe the standard output from one command can be the standard input to another command.
% ls -l | wc -l
29
% 
The wc command with the -l option gives you a count of lines. This command will pipe the output of our ls -l command to the wc(word count) command giving us the total number of files and directories in our current directory.

Pipes are not limited to two commands, by using filters you can have pipes of unlimited length.

3.8 Filters

A filter is a command that takes its input from standard input and sends it output to standard output. Most commands in unix are filters, taking some input performing an action and sending it to standard output. Filters are often used in pipes to change the data as it travels through the pipe. Some commands such as grep and sort are good examples of programs that can be used as filters. Whereas the ls command can not be a filter since it does not accept standard input. Actually ls can only be used as the first command in a pipe. The lp command cannot be used as a filter since it can only send to the printer and not to standard output. In fact, lp can only be used as the last command in a pipe.

3.9 Process Management

Another features of Unix is its multi-tasking ability. Unix is able to multi-task through the use of processes. A process is simply a program in execution. A typical Unix machine has anywhere from 10 to 150 processes running concurrently. Each process must share the one central processor. The system gives the illusion of simultaneously performing many tasks by switching the CPU, very rapidly, from one process to the next.

Each time you execute a command an independent process is created to perform the command. Every command that has been executed has been a process, even the shell is a process. The system does not use the command name to keep track of the process, instead it knows a process by its process identification number(PID). To see what processes you currently have running on the system use the ps command.

% ps
   PID TTY      TIME COMMAND
 10315 ttyp5    0:00 ps
  1548 ttyp5    0:00 tcsh
We can see there are two processes running. The ps command which we just used to view the processes and tcsh which is our shell.



Last Modified: 4/18/94 by Scott Miller