The UNIX Operating System

The following is a (very) brief summary of the UNIX (or Linux) Operating System. It describes a small subset of the commands available under the shells sh (or its linux version, bash) and csh (or tcsh, an extended version of csh). These shells offer a convenient syntax for executing interactive commands. The command structure in bash is in many ways more powerful, and is particularly useful in writing scripts where efficiency is important.

The File System

In UNIX (and Linux), physical disks are divided into logical ``partitions'' (blocks of space of specific size) with the format command. A file system is then imposed on these partitions, allowing a directory structure to exist. Directories (``folders'' in Windows or MacOS terminology) can contain files of arbitrary types.

Some commands allowing a user to manipulate files are:

pwd
displays the name of the current directory (``.'')

ls
lists the files in the current directory

ls dir
lists the files in the directory ``dir''

ls
displays the contents of the current directory; ``ls -lF'' is a useful extension providing additional information about the files

cp file1 file2
copies file1 to file2

cd /dir1/dir2
go to specified directory; cd .. means go to the directory above the current directory; cd ../foo means go up one level, then down into directory foo. The current directory is always denoted by ``.''

mv file1 file2
rename file1 as file2, within the same directory (changing the name of the file), or move file1 into some other directory

cat file
display file onto standard output (the screen, usually)

more file
display file onto standard output (screen), one screenful at a time. For obscure UNIX reasons, the command "less" does much the same thing...

>
>>
redirect output to a file; e.g. cat file1 > file2 copies file file1 into file2, and cat file1 >> file2 appends file1 onto file2.

|
``pipe'' standard output to standard input e.g. cmd1 | cmd2 sends the output of command cmd1 into the input of cmd2

touch file
updates the ``date and time stamp'' of a file, making it believe that it has just been modified

rm file
delete a file

File name completion is supported under csh and tcsh (turn it on by issuing the command ``set filec.'' In csh, use the ESCAPE key; in tcsh, use the TAB key.

UNIX also supports ``wildcards.'' A ``?'' will be replaced by any matching single character, while a ``*'' will be replaced by any number of matching characters. For example, ls abc*.c will list all files whose name starts with ``abc'' and ends in ``.c''.

System Access and Security

UNIX provides a multi-user computing environment. This means that it shares its resources among many users, as well as the various tasks that each user might be performing. It also implies that UNIX must provide a security safegard against users peeking into each others' ``home directories.''

To gain access to a UNIX system, a user must first identify his/her account in response to the login prompt,

	Login:     username 
	Password:  password 
then give a unique password. A user can change his/her password by issuing the command passwd, followed by the old and new passwords.

The user can exit the system with the command logout . On login, UNIX starts up a new shell interpreter (e.g. the C-shell interpreter csh, or bash), and sets up the computing environment for the user by scanning the ``hidden'' (i.e. not normally listed by ls) set-up files, .login and .cshrc (.orifile and .bashrc in bash). The .cshrc file is also scanned each time a new ``task'' (e.g. a new window) is launched. These hidden files are how a user can customize his/her computing environment at will, typically with commands like

	alias name definition
in csh, or
        alias name=definition
in bash, which defines ``name'' (usually something short) to mean ``definition'' (often long and/or complicated).

Each file is marked for ownership by a user and as belonging to a group of users. Each file is also marked for ``reading'', ``writing'' and ``execute'' permissions by its owner, members of its group and by ``others'' The directory command ``ls -l'' lists these permissions as ``r'', ``w'' and ``x'' if they are allowed. For instance, the following dialog

	> ls -l
	total 6
	-rw-rw-r--  1 steve     4322 Jan 10 12:30 file1
	drwxrwxr-x  2 steve      512 Jan 10 12:29 file2
	> 
shows that file1 has can be read and written by the owner and all group members, and read by all, while file2 is unprotected, except that it cannot be written by ``others''. Furthermore, file2 is a directory (name of a sub-directory within the current one -- indicated by a ``d'' in the first column). For a full description of ls and its options, type 'man ls'.

The protections of a file or group of files can be modified using the chmod command. For example, to make all files whose names begin with abc executable by your group and unreadable by others, type

	> ls -l abc*
        -rw-r--r--  1 steve  staff   123 Mar 31 10:38 abc1
        -rw-r--r--  1 steve  staff  2331 Mar 31 10:38 abc2
        -rw-r--r--  1 steve  staff     2 Mar 31 10:38 abc34
	> chmod g+x,o-r abc*
	> ls -l abc*
        -rw-r-x---  1 steve  staff   123 Mar 31 10:38 abc1
        -rw-r-x---  1 steve  staff  2331 Mar 31 10:38 abc2
        -rw-r-x---  1 steve  staff     2 Mar 31 10:38 abc34
To change the default file permissions, i.e. the permissions applied to every new file you create, use the umask command:
	> umask 022
        > touch xyz
        > ls -l xyz
        -rw-r--r--  1 steve  staff  0 Mar 31 10:57 xyz
	> umask 026
        > touch pqr
        > ls -l pqr
        -rw-r-----  1 steve  staff  0 Mar 31 10:58 pqr
The algorithm is a little obscure: take the number following the umask command, subtract it from 666, and the result is the user/group/other permissions, in octal! As a practical matter, 022 gives everyone read permission, 026 denies read access to others (outside your group), and 066 means that noone else can read or execute your files. The umask command is normally placed in your .login or .profile file, so that it is executed every time to log in to the system.

The ownership of a file can be modified by the command chown ; this is a restricted command, however -- it can be executed only by the system ``super-user.''

There are many other commands that facilitate the users' interaction with the computer in small ways; for instance:

date
gives you the date
time
gives you the time
hostname
reminds you of the system name
finger
gives a list of users and what they are doing
ps
display the tasks running on the computer; try ps -aux
history
lists your most recent commands
!!
recalls the last command
!str
recalls the last command starting with the string str
!n
recalls the nth command

Help on any UNIX command can be obtained via the man command. The command ``man cmd'' prints to standard output the content of the manual pages concerning the UNIX command ``cmd''. You can also search by context with man -k cmd .

Executing Commands: Running Programs

A file can be of the special ``executable'' type (e.g. resulting from a compilation of C code followed by linking to the appropriate libraries), meaning that it contains instructions for the computer to execute. For instance, each UNIX command is itself an ``executable'' residing on the disk in a directory accesible by all. These files are executed by specifying their name, followed by any necessary argument. For instance,

	ls -lFg file

will execute the ``ls'' command, with arguments ``-lFfg'' (a switch) and ``file'', resulting is a very complete directory listing for the file ``file''. The same goes for any executable file that you might create.

Automating Tasks: Scripts

Any set of UNIX commands can be put into a ``script'' file to be executed all at once as a single command. The language is very complete, including control structures and looping. As an example, consider the following script:

#!/bin/csh
#
#          example of a C-shell script
#          --------------------------- 
#
#		store the wordlist resulting from
#		the ls command in variable files
#		
set files = `ls`
#
#		foreach is a loop; files takes on the
#		value of each entry in files
#
#		$variable_name is the content of
#		that variable
#
foreach file ( $files )
      echo 'File name:' $file
end

The script explicitly calls for the C-shell interpreter; the ``set'' command transfers the result of the ls command into the variable ``files'' as a wordlist. Then the ``foreach ... end'' loops over the wordlist, assigning each member to the variable ``file''. The command ``echo'' writes a message to standard output. The syntax ``$variable'' means the ``content'' of the variable; $var[i] means the content of the ith position in the variable.

At the script level, the differences between bash and csh become more evident. A bash version of the above script might look like:

#!/bin/bash
#
#          example of a bash script
#          ------------------------
#
#		store the wordlist resulting from
#		the ls command in variable files
#		
files=`ls`
#
#		for is a loop; files takes on the
#		value of each entry in files
#
#		$variable_name is the content of
#		that variable
#
for file in $files; do
      echo 'File name:' $file
done