UNIX

1. Unix : History
2. Purpose of Unix
3. Features of Unix
4. Kernel and Shell
5. Unix Commands - Directory , General and File Commands.
6. File permission / Access Modes.


UNIX

Unix (all-caps UNIX for the trademark) is a family of multitasking, multiuser computer operating systems that derive from the original AT&T Unix, developed in the 1970s at the Bell Labs research center by Ken ThompsonDennis Ritchie, and others.


HISTORY

Initially intended for use inside the Bell System, AT&T licensed Unix to outside parties from the late 1970s, leading to a variety of both academic and commercial variants of Unix from vendors such as the University of California, Berkeley (BSD), Microsoft (Xenix), IBM (AIX) and Sun Microsystems (Solaris).

AT&T finally sold its rights in Unix to Novell in the early 1990s, which then sold its Unix business to the Santa Cruz Operation (SCO) in 1995, but the UNIX trademark passed to the industry standards consortium The Open Group, which allows the use of the mark for certified operating systems compliant with the Single UNIX Specification (SUS). Among these is Apple's OS X, which is the Unix version with the largest installed base as of 2014.



2. PURPOSE OF UNIX

WHY UNIX ?


1. PORTABILITY:
  • The UNIX OS were written in assembly language that used less memory and executed instructions faster than those written in other languages. 
  • The UNIX system can be placed on different machines in relatively easy way. 
  • Its design has a very small portion that depends on actual H/w of underlying machine.



2. PORTABLE APPLICATION PROGRAMS:
  • A program written on one machine can be put to another machine and executed there without great difficulty.



3. BACKGROUND PROCESSING:
  • With UNIX user can execute more jobs simultaneously and thus increase productivity.



4. HIERARCHICAL FILE SYSTEM:
  • With UNIX a user can organize the information in structured fashion so that he/she can handle with ease or confidence.



5. COMMAND INTERPRETER:
  • UNIX in addition to execution of commands typed by the user acts as a programming language, so the user can store a number of commands to be executed in command file and execute all of them by giving the name of the file.



6. PIPES AND FILTERS:
  • User can connect two commands together so that the output from one program becomes the input of the next program. Two or more commands connected in this way form a pipe.
  • To make a pipe, put a vertical bar (|) on the command line between two commands.
    When a program takes its input from another program, performs some operation on that input, and writes the result to the standard output, it is referred to as a filter.
  • By piping user can offer the output of one command as the input to the another. Thus various commands can be combined in useful ways.


7. UTILITIES:
  • UNIX provides a large collection of useful programs that are already written by different people.




3. FEATURES OF UNIX:


1. A multi user system:
  • In UNIX, the resources are actually shared between al users, be it the memory (RAM), the CPU or the hard disk. 
  • The computer breaks up a unit of time into several segments, and each user is allotted a segment. 
  • So, at any point of time, the machine will be doing the job of a single user. 
  • The moment the allocated time expires, the previous job is kept in a abeyant, and the next user‘s job is taken up. 
  • This process goes on till the clock has turned full circle and the first user‘s job is taken up once again. 
  • The Kernel does this several times in one second and keeps all ignorant .


2. A multi tasking system:
  • In UNIX, a single user can run multiple tasks concurrently. 
  • User can switch jobs between background and foreground, suspend, or even kill them. 
  • This is done by running on job normally and the others in the background.


3. Pattern Matching:
  • This feature of UNIX makes it so attractive to the end user. 
  • Match number of file names, match number of similar patterns, etc.


4. The tool Kit:
  • UNIX is a collection of tools, which lets you dispense with programming work for many applications.


5. Programming facility:
  • UNIX is highly programmable, it was designed for a programmer, not a casual end user. 
  • The UNIX shell programming language has all the necessary ingredients like control structures, loops and variables that establish it as a programming language in its own right.


6. System calls and Libraries:
  • UNIX is written in C. 
  • Though there are a couple of hundred commands handling specialized functions, they all do that by using a handful of functions call system call. 
  • These calls are built into the Kernel and all library functions and utilities are written using them.


7. Windowing Systems:
  • Currently, UNIX is a fully commercial product used by different users with varied background and aptitudes. 
  • Now UNIX had to come up with its own GUI.

8. Documentation:
  • Today, there is no feature of UNIX on which a separate textbook is not available.


4. KERNEL AND SHELL


Whenever anything is typed through the keyboard, it is thoroughly examined and processed by a separate agency. The command will be executed only when the examiner finds it suitable for execution. In this case, the examiner, in the course of processing, decided that all multiple spaces are to be reduced to a single one. This agency is called the Shell’. No command can be executed unless it obtains the clearance of the shell.

The Shell, when analyzing the command and its associated arguments, often modifies and simplifies its input before it forwards it to another agency that actually executes the command. This agency is called the ‘Kernel’.

UNIX permits the use of complex command structures that can‘t be understood by the Kernel, the Shell has to take on the role of interpreter to translate them into form that the Kernel can understand.


TYPES OF SHELL

Different Shell: 
The shell runs like any other program under the Unix System. Hence, one shell program can replace the other, or call another shell program as it would call any other program. 
Due to this feature, a number of shells have been developed in response to different needs of user. 

Some of the popular shells are : -

1. Bourne Shell: 
  • This is the original command processor developed at AT & T named after its developer, Stephen R. Bourne. 
  • This is the official and most widely used shell at present. 
  • It is fasted UNIX command processor available and can be used on all UNIX systems. 
  • It used the dollar sign ($) as a prompt. The executable file name is sh.
2. C Shell: 
  • This is developed by William Jay and other at the University of California at Berkeley, and gets its name from its programming language, which resembles the C programming language in syntax. 
  • It used the percent Sign (%) with a counter that keeps track of how many command have been executed. 
  • The executable file name is csh.

3. Korn Shell: 
  • Developed by David Korn, this combines the best features of both the above shells.
  •  Although very few systems currently offer this shell, it is slowly gaining popularity. 
  • The executable file name is ksh.

4. Restricted Shell: 
  • This is a restricted version of the Bourne Shell.
  •  It is typically used for guest logins- (users who are not part of the system). 
  • The executable filename is rsh.


5. UNIX COMMANDS

1. Directory commands 2. General commands 3. File commands.


DIRECTORY COMMANDS

A directory is a file whose sole job is to store file names and related information. 
All files, whether ordinary, special, or directory, are contained in directories.
UNIX uses a hierarchical structure for organizing files and directories. 
This structure is often referred to as a directory tree . The tree has a single root node, the slash character ( /), and all other directories are contained below it.

Home Directory

The directory in which you find yourself when you first login is called your home directory.
User will be doing much of your work in your home directory and subdirectories that you'll be creating to organize the files.

1. Identify the current Directory (Working Directory) :

  • pwd -- Print Working Directory.
  • This command is used to display the full path-name of the current directory i.e. shows the directory in which user is currently working.
$ pwd
/user / bin         (i.e. /user / bin is the directory in which the user is       $_                 currently working.)




(i). The slash character (/) at the beginning of a directory path specifies the root directory; other slashes join the name of the directories in the path in order.

(ii). The path /user / bin says to start at the root, go to directory user & then go to directory bin. If this directory contains another directory name xyz, the path would be /user/bin/xyz.

(iii). The home directory is referring by the name $ HOME as well. Typing $ HOME/xyz is the same as typing /user/bin/xyz (if login name is bin) i.e. $HOME is equivalent to beginning with the root directory and progressing to the current user‘s home directory.

2. Changing Directories (chdir or cd): 

The chdir (change directory) command lets you change the working directory to a different one that already exists; some versions of UNIX require the abbreviated command cd.

User can specify either an absolute or relative path in the command, depending on your needs. 

For example, entering the command $cd /user/Carson/project has the same effect as entering $ cd project if the working directory is /user/Carson.

$pwd
/user/Gomes
$cd /user
$pwd
/user
$_
$pwd
/user/Gomes
$cd.. 
$pwd
/user
$_

(The two dots refer to the parent directory of the current directory)

3. Creating a Directory :
The mkdir (make directory) command is used to create directories.

                           MBA              
       |                       |                  |
MBA-I-D      MBA-I-B    MBA-III
    |                          |                |
ITM                   FOM           Specialisation          
                                         |                    |               |
                                        Systems     Finance   Marketing

$mkdir MBA
$_

The subdirectory MBA is created under the current directory. However, the new directory does not become the current directory.

$pwd
/user/MBA
$mkdir MBA-I-D
$mkdir MBA-I-B
$mkdir MBA-I-D/ITM
$mkdir MBA-I-B/FOM
$mkdir MBA-III
$mkdir MBA-III/Specialization
$mkdir MBA-III/Specialization/Systems
$mkdir MBA-III /Specialization/Finance
$mkdir MBA-III /Specialization/Marketing
$pwd
/user/MBA
$_


4. Removing a Directory: (rmdir or rd)

The rmdir (remove directory) command removes the directory specified. Some versions of Unix require the form rd instead
$rmdir xyz
$_

A directory can be deleted only if it is:
*Empty (does not contain files or subdirectories)
*Not the current directory.
Complete path – names may also be specified with rmdir
$rmdir /user/bin/xyz
$_


5. Listing the contents of Directory :

The ls command is used to display the name of the files & Subdirectories in a directory.
  1. $ ls List the contents of the subdirectory & files on the screen.
  2. $ ls –l display a long (detailed) list of files and directories.
  3. $ ls –d display only directories.
  4. $ ls –ld display detailed list of directories.
  5. $ ls –a lists all files, including hidden file.
  6. $ ls –r recursively lists sub-directories, list in reverse order.
  7. $ ls –g show group information with listing
  8. $ ls –s list in order of increasing size
  9. $ ls –t list in order of time, most recent first
  10. $ ls –l then output is:


total 3
-rw-r- - r- -       1            Gomes       Student           13         Jan 1              10:39                data1
-rwxrwxrwx     1            Gomes       Student           19        Feb 10            21:35                data2
drwxrwxr         2            Gomes       Student           32        Jan 16             16:50               prgdir
File type and  Number    File            Group           File        Date           Time last         File name
File access     of            owner or      owner or       size in    last              modified
permission     links          user          group name     bytes     modified

The first character in column one can be ‗- ‗(Ordinary File), ‗d‘ (directory) or ‗b‘(block file), ‗c‘(character file), ‗p‘(special file).


GENERAL COMMANDS

1. Checking the Date & Time :
All UNIX systems maintain the current date and time. 

Users can display the current data and time with the date command.

$ date
Thu Feb 18 11:30:05 IST 2011

2. Displaying a Calendar:

UNIX lets user display a Calendar of one or more months by typing the appropriate cal command. 

After typing cal some Unix systems show you the calendar for the current months and one month before and after it. 

Other Systems don‘t recognize cal without any arguments.

$ cal

Format:
Year value in 4 digits
$ cal [month] year
Month value from 01 to 12

3. Listing the users on the system with who :

The who command is used to display the names of all users who are currently logged in. 

It displays login names, terminal file-names & login date and time of the users currently log on the system.

$ who

Edward tty02 Feb 19 11:45
Dollys tty03 Feb 19 08:40
Amita tty01 Feb 19 09:00
$_

The who am i command displays the name of the current user. i.e. identifies the user who invoked the command.
$ who am i
Gomes tty02 Feb 19 11:45
$_


4. Clear the Screen:

The command tput clear, cleared the standard out device the screen and positions the cursor on the top left corner on the screen.

$tput clear
Note: Some Unix systems only require clear to clear the screen.


FILE COMMANDS

All data in UNIX is organized into files. All files are organized into directories. These directories are organized into a tree-like structure called the file system.
When user work with UNIX, one way or another you spend most of their time working with files. 
In UNIX there are three basic types of files −
  • Ordinary Files − An ordinary file is a file on the system that contains data, text, or program instructions. In this tutorial, you look at working with ordinary files.
  • Directories − Directories store both special and ordinary files. For users familiar with Windows or Mac OS, UNIX directories are equivalent to folders.
  • Special Files − Some special files provide access to hardware such as hard drives, CD-ROM drives, modems, and Ethernet adapters. Other special files are similar to aliases or shortcuts and enable you to access a single file using different names.

1. Copying Files: 

  • The cp command duplicates the contents of the source file into the target file. The original file is unchanged.
  • $cp data1 data2 - In this example, the contents of data1 are copied to a new file data2. If data2 already exists, its contents will be overwritten by the contents of data1.
  • Complete path-names can be specified with the cp command to copy files across directories.

2. Renaming Files:

  • The mv command moves the content of the source file to target file with a new name and erase the original file. This has the effect of renaming the original (source) file, resulting in a single file under the target name. 
  • This command can also be used to move or rename a file into a different directory and directory into another directory.
$ mv fiel1 file2              | mv data1 data2
$ mv dir1 dir2              | mv /user/Gomes/prog.files /user/Gomes/programs
$ mv file … directory  | mv data2 /user/Gomes/programs

3. Deleting or Removing Files:

  • The run command is used to delete files or directories.
$ rm data1
$_

  • If the file is not located in the currently directory, the complete path-name have to be given.

$ rm /user/gomes/data1
The rm command allows an option.
$ rm [option] file
  • When user specify option -i, UNIX asks for confirmation before deleting each file.

4. Redirection:

  • The redirection symbol is greater than sign (>). 
  • By using redirection user can send any screen output, to a file. 
  • If user type cal 2000>year2000, the calendar for the year is not sent to the screen; instead it is sent to a file named year 2000 in your directory.
  • When user redirect output to a file, & if you name a file that doesn‘t exist yet, UNIX creates that file and stores the screen output in it.
  • If user name a file that already exists to hold the redirected output, UNIX quietly destroys the contents of the existing file by replacing it with the command output.

5. Append:

  • Another redirection option lets user specify that the new file contents should be added to the end (appended) of an existing file. 
  • User do this by doubling the greater than sign (>>) used for redirection. The command ‗cal 2002>>months‘ generate a calendar for 2002 and place it at the end of the file months.

DISPLAY, CONCATENATE & CREATE FILES

Displaying File contents

The cat command displays the contents of any text file specified.
$ cat data 1
A simple file.
$_

  • The command assumes that data1 is in the current directory; complete the path-names can also be specified to display a file in another directory.
  • When user enter cat this.months at the keyboard, the files is displayed on the screen until the end of the file is reached; the early part of the file may scroll out of view if the file contains more than one screenful of information.
  • If user want to cause the display to stop before it reaches the end of the file, just press Ctrl+S & the display pauses. 
  • Press Ctrl+Q to start it up again. In many systems, pressing any key restarts the display following ctrl+s.

Concatenates or combining files :

  • The cat command used to concatenate or combine the contents of several files into a single file. 
  • The files are named in sequence. The contents of each file are copied to the screen in sequence. The entire file is displayed, line-by-line, without a pause.
  1. $ cat myfile yourfile-- welcome to UNIX This is your first session. 
  2. $ cat myfile--welcome to UNIX
  3.  $ cat yourfile-- This is your first session.
  • The cat command is also use with the redirection and append.

  1. cat this.year > one.year has exactly the same effects as cp this.year one.year
  2. cat this.year next.year > two.years (in this case, the two files are sent to the redirected file in sequence).
  3. cat another.year >> two.years (to adds the contain of another.year file to the end of two.years.)
  • The redirection (>) or append (>>) symbol precedes the target file, which is always the last file in the list. All source file named before the symbol are sent to the target file in sequence.

Creating a file from the terminal: -

  • If user use cat with no arguments, it simply repeats what user type to the screen. i.e. it use as the standard input and output devices. 
  • User type a line at the keyboard and press enter, then the line typed is repeated on the screen. 
  • UNIX continues to give a cursor at a new line for entering text and then sends whatever you enter back to the screen. 
  • User can terminate this effect of cat by pressing ctrl+D.
  • If user use redirection, user can use cat to send the lines user enter to a file instead of the to the screen and thus create a new file directly from keyboard entries.
$cat >my.address 
Scheme No 1
India
$_

After pressing ctrl+D, that cat command is terminated &  the newly created file is closed. Use cat my.address  command to displayed the contents.

The More Command:

  • The more command displays the content of the file in the first screenful and then pauses with a message such as -- more -- and a percentage value that indicate how much of the file has been displayed. 
  • If user press the key, the next line is displayed. 
  • If user press key, another full screen is displayed with a new percentage value.
  • $ more filename

The Man Command:
  • The man command displays pages from the UNIX reference manual.
  • For example to get a detailed information about the ls command :  $ man ls
Controlling messages:

  • The mesg command controls the display of messages on the screen. 
  • That is, it decides whether a user can send messages to user or not.
  • $ mesg $ mesg [y/n]
  •  To prevent any real-time messages, use mesg n to say ‗no‘ to the write command.
  • To allow message again, type mesg y at the shell prompt.
  • If user is not sure what their message status is, type mesg by it self.
$ mesg
is y
$_

The response tells user if they can receive message (y) or not (n); user can reset it with another mesg command if they want.

Note: The –T option with the who command will display a list of user accepting or denying messages.


$ who –T


guest -tty01 Sep 12 14:07
Clinton +tty02 Sep 12 12:07

A minus sign (-) before the terminal port indicates, that the user is not accepting messages. 

A plus Singh (+) indicates that the user can receive messages.


6. FILE PERMISSION / ACCESS MODES

File ownership is an important component of UNIX that provides a secure method for storing files. Every file in UNIX has the following attributes −
  • Owner permissions − The owner's permissions determine what actions the owner of the file can perform on the file.
  • Group permissions − The group's permissions determine what actions a user, who is a member of the group that a file belongs to, can perform on the file.
  • Other (world) permissions − The permissions for others indicate what action all other users can perform on the file.

Three types of file access can be granted or denied:

  1. Read: Denoted by r : Allows read (including listing and printing) i.e. displaying, copying and compiling a file. But editing and deleting of a file are not possible.
  2. Write: Denoted by w : Allows read, editing (modification) and deleting of a file.
  3. Execute: Denoted by x : Allows execution of a file. This is only meaningful with programs or script file.

Directory and special files also have access permissions. In case of directory:

  1. Read: The user can only list file in the directory
  2. Write: New file can also be added to the directory, remove file from directory, rename files in directory, make subdirectory and remove subdirectory.
  3. Execute: Allow the user to make the directory, their own working directory and display a file in the directory.

Each of the three user types can have any combination of the three-access type for each file or directory.

Checking FAP:

User can check FAP by using the ls –l command.

The first set of character in each line of the long listing represents the access permission.


$ ls –l processing
drwxrw-r-- 2 carson 784 May 15 08:53 processing
file group others
Owner Owner

The first characters indicate whether the file is directory (d) or not (-).

The next nine characters specify the permission; the first set of three indicate the owner access, the next set of three indicate the group access, and the final set of three indicate the access for everybody else.

The maximum access is represented by rwx, indicating read, write and execute.

Wherever a dash (-) appears, an access permission has not been given.

Default Access:

Installation has default setup for all newly created files and directories.
$ ls –l
drwxrw-r-- 2 carson 784 May 15 08:53 processing
-rw-rw-r-- 1 carson 1128 May 17 10:14 realty.data
-rw-rw-r-- 1 carson 9176 May 15 11:12 weather.data

For the directory,

The owner has rwx access (i.e. read, write and execute permission)

The group has rw access (i.e. read, write permission)
The others has r access (i.e. only read permission)

For the file
The owner and group has rw access (i.e. read and write permission)
The other has r access (i.e. only read permission)


CHANGING ACCESS PERMISSIONS

Access permissions associated with a file or directory can be changed using the chmod command. 

Permissions associated with a file can be changed only by the owner of the file.
$ chmod User-Type [Operations] [Permission] Filename

User-Type:
u owner or user of the file
g group owner or member of the same group
o all other users
a all three user types

Operations:
+ add the permission
- remove the permission
= set the particular permission while removing all other permissions.

Permissions:
R read
W write
X execute
S set ID to owner during execution.


Examples:
  1. $ chmod u + x newprog add execute access to the owner for the file named newprog
  2. $ chmod u – w finance.data write permission for owner removed for one file named finance.data, probably to prevent changes by accident.
  3. $ chmod a = r newfile provide read access permission for newfile to all three user types, and removes any write & execute access permission.
  4. $ chmod o + r newyork/* read access given to ―other‖ users for all files in the newyork directory.
  5. $ chmod g – x /home/mis execute permission removed for group from /home/mis dir
  6. $ chmod go = exam all access permissions for the group and other users are gone.
  7. $ chmod - w abc.x remove write permission from all users, for abc.x file.


Limitation of chmod:

- If user decide to let one member of a group have write access to the directories, they must give write access to entire group.
- If user want to shut a particular member of their group out of one of the files, they have to exclude the entire group.