Sunday, August 5, 2012

Unix Fundamentals


In this session, you learn about:
•          The functions of OS
•          The history of Unix
•          The features of UNIX
•          The Unix architecture
•          Process management
•          File management
Operating System (OS)
•          OS is a system software
•          OS can be defined as an organized collection of software consisting of procedures for operating a computer
•          OS provides an environment for execution of programs
•          OS acts as an interface between the user and the hardware of the computer system.
•          Operating system interacts with user in two ways
•          Operating system commands
                               Enables user to interact directly with the operating system.
•          Operating system calls
                               Provides an interface to a running program and the operating system. System calls in UNIX are written in C.
History of UNIX
•          Ken Thompson of AT&T Bell Laboratories designed UNIX in late 1960s
•          Two versions of UNIX that emerged are AT&T Unix and BSD Unix
•          In 1989, AT&T and Sun Microsystems joined together and developed system V release 4 (SVR4)
•          Two of the main standards mainly in use are POSIX (Portable Operating System Interface) and  X/open standard. In 1988, MIT formed Xconsortium developed vendor-neutral Xwindow System.
Features of UNIX
•          UNIX is multi tasking and multi user operating system
•          UNIX is a character user interface (CUI ) operating system
•          UNIX is a open source operating system
•          Portability – Can we use it on any OS
•          Modularity – Can change the source code
•          File structure
•          Security
•          Strong networking support & advanced graphics
Layered Architecture
Shell
•          Acts as an interface between user and kernel
•          User commands will be interprets and carried out to kernel.
•          Its features include control flow primitives, parameter passing, variables and string substitution.
Kernel
•          Kernel will act as a interface between shell and hardware components.
•          Core of the operating system
•          Collection of system calls mostly written in C language
•          Interacts directly with the hardware
•          Allocates time and memory to programs and facilities IPC’s
•          Helps in I/O services.

Standard directories in UNIX
                                                                                                                                                                            
•          /etc             stores the system administration utility files
•          /bin             stores the most commonly used unix commands
•          /usr              stores all user home directories and some Unix commands
•          /dev             stores all device files
•          /lib                stores library files for C programming
•          /tmp             used for temporary storage
File access permissions
•          A- total number of disk blocks occupied by the files
•          B- type of the file
–        - ordinary file
–        d – directory
–        c- character device
–        b – block device
•          C- its access permissions
•          D- number of links to the file
•          E- the owner of the file
•          F- the group of the owner of the file
•          G- the size of the file in bytes
•          H- the date and time of last modification
•          I – the file name
Absolute mode
•          Based on octal numbers
–        No access                                                                                                                              0
–        Execute access only                                                                                                          1
–        Write access only                                                                                                               2
–        Read access only                                                                                                                4
–        Write and execute access                                                                                              2+1=    3
–        Read and execute                                                                                                            4+1=    5
•          $chmod 400 mydoc.doc                                                                                                                  
•          Exercise
•          Write the command to change the permission of the file mydoc.doc
–        Read and write for the owner
–        Write and execute for the group
–        Execute only for others ( both in symbolic and absolute)
cat command
•          Helps you to create a file
•          $ cat > filename
•          cat filename helps to view the contents of the file.
touch command
•                -- Will allow to create multiple 0 byte files
•                     $ touch file1 file2 file3
VI command
•                  $ vi <filename>
•                  If the file doesn’t exist, it will create otherwise open the existing one.
Creating a directory:
•          You can create a directory by using the mkdir command. For example, to create a directory named dir1 within the current working directory:
–        $ mkdir  dir1
–        $ mkdir /usr/user1/dir1
–        Options
            cd command
–        To change the directory to dir1 type
–        cd dir1
            pwd
–        To see the current working directory
Removing a directory
•          Removes a directory
•          To remove a directory
–        Must be empty
–        Should not be the current directory or at a higher level
–        Should not be the home directory
–        -p -> Removes all directories along the path name specified
–        $ rmdir  -p /home/demo/mydir
Removing files
•          To delete a file
                                                                            $ rm <file_name>
•          You can remove more than one file at a time.
                                                                           $ rm oldbills oldnotes oldjokes
            would remove the three named files.
                                                  Options
                                                              -r à recursive
                                                              -i à interactive mode
                                                              -f à forceful
Basic commands for file handling
•         ls command
–        Lists the contents of your current working directory
–        Few useful options with ls are:-
•         -l             list in long format
•         -a            all files including those which begin with dot
•         -I             list the inode number in the first column
•         -x             sorts in multi column output across the page
•         -C            displays file in columns
•         -R            recursively list all directories and  subdirectories
•         -F             indicates the type if files using symbols like   */=@|
•         -ld           long listing of files in a directory
File management utilities
•         cp             : copy a file
                               $cp <file1> <file2>
•         mv            : move / rename a file
                               $ mv  <file1> <file2>
                               $ mv   file1 file2
                               $ mv   dir1/f ile1 dir2/fil2
Wc
wc  word count
•         -c character count
•         -w word count
•         -l line count
•         $ wc employee
•         To count the number of files in a directory
 To count the number of users currently logged in
WC –l   file1
WC –c   file1
Wc –w   fil1
Tr
•         Translates the input into some other form
Eg:
•         Convert all lowercase to uppercase
–        tr “[a-z]” “[A-Z]” file1
FIND
•         find command
–        Helps to find a file in the specified directory
–        The syntax is find <path> -name <filename>
•         find . –name doc1.doc
•         find .  /tmp/project –name new_data
•         find /usr –type d –name backup
•          
File Comparisons
•         comm : displays common lines in the two files
•         cmp    : to compare two files
•         diff      : reports the difference between two files
•         uniq    : display the duplicate and unique lines of a file
comm.
Cmp
•         Compares two files
              $ cmp file1 file2
    Eg:
        document1                                            document2
        This is a document.                              This is a document.
         $cmp document1 document2
        document1 document2 differ: char 6, line 1

diff
•         Reports the difference between two files
                             $diff [options] [file1][file2]
                    -b ignore trailing blanks
                   -h used for files of unlimited length
Eg:
              file1                                                           file2
              cal                                                              cal
              cp                                                               cp
              crypt                                                          crypt
               z                                                               cut
$ diff file1 file2
                      4c4
                      <z
                      -----
                     >cut
Uniq
•         Displays the duplicate and unique line of a file
•         $ uniq [option..] <filename>
–        -u        display only the unique lines
–        -d        display only the duplicate lines
–        -c        display all lines each preceded by the count of the records.
Miscellaneous Sending and receiving mails
•         $ mail <login name >
mail neeraj
Subject : …. <ctrl+d>
Input/ output redirection
•         Input redirection
•         Output redirection
•         Standard error redirection
Input redirection
•         Changing the default input source
•         Input redirection operator ‘<‘
•         $ command <filename
•         After the execution the standard input is reassigned to the default device
•         Eg :
–         $ cat <emp.dat ( similar output will be displayed by cat emp.dat)
Output redirection
•         Changing the default destination of the output
•         The output redirection operator : ‘ >’
•         $ command > filename
•         After the execution the standard output is reassigned to the default device
•         Eg:
–        $ cat emp.dat > emp.out
–        $ date > todays_date
–        $ cat > file1
–        $ cat >> file2
–         
Standard error redirection
•         0 standard input
•         1 standard output
•         2 standard error
•         $ command 2 > err_file
•         Eg :
–        $ cat emp.dat > newemp  2 > emp_err
Pipes and filters
•         Pipes :Connects two or more commands
•         Filters : Unix command which takes its input from standard input processes or filters it and generates and output.
–        grep
–        sort
–        cut
–        paste
–        head
–        tail
–        wc
–        pg
–        More
–        tr
Filters: grep
•         grep – ( global search for regular expression and print)
•         grep is the standard file searching and selection utility
•         $ grep [options] “pattern” <filename>
•         Options :
–        -n : prints line numbers
–        -v: the reverse search criterion
–        -c: display only a count of matching patterns
•         grep “madhu” emp.dat
–        Display the line(s) containing the string madhu.
•         grep –n “anurag” emp.dat
–        Display the lines containing the string anurag preceded by the line number
•         grep –v “Madhu” emp.dat
–        Display the lines of the file excluding the lines contatining the string Madhu.
Sort
•         sort [options] [-o output file name] [file name]
•         options
–        d         letters, digits and blanks
–        n         sort on the numeric field
–        r         reverse sort
–        u         eliminate duplicate lines                                                          
–        t           filed separator
–        o          stores the output
cut
•         Retrieves selected fields from a file
•         $ cut [options] <filename>
•         Options
–        -c      selects characters specified by list
–        -f       selects fields specified by list
–        -d      field delimiter
•         $ cut –c4-9 employee
–        Display characters 4 to 9 from each record
•         $ cut –d " " –f2-4 employee
–        Display 2-4 fields
•         $ cut –f 4- employee
–        Display 4th field onwards
Paste
•         Merges files horizontally.
•         $ paste <file1> <file2>
•         Options –d  delimiter
Head and tail
•         head employee will display first 10 lines
•         head -5 employee
•         tail employee last 10 lines from the employee
•         tail -7 employee
•         tail +10 employee display lines from 10th till the end of the file.

Others
The Unix manual
•         Information on all the commands can be found in the on-line manual pages.
•         $man <command>
•         Shows the details (syntax and description) of the command
•         help, info
•         $            end of the current line
•         ^ or 0    beginning of the current line
•         W         beginning of the next word
•         W         same as w, but ignores the punctuation
•         B           back to the beginning of the current word
•         E           end of the current word
•         Moving the cursor over the paragraph
{   to the beginning of the current paragraph
}   to the end of the current paragraph
n{   nth paragraph above the current paragraph
n}   nth paragraph below the current paragraph
•         Moving the cursor over sentences
(   first character of the sentence
)  last character of the sentence
Screen movements
Ctrl+D      scrolls down half a window
Ctrl+U      scrolls up half window
Ctrl+B      moves to the previous window
Ctrl+F       moves to the next window                                                                         
•         What is the command to move the cursor to the right by five words?


Modifying text
r, R          replace character
cw           change current word
nc            wchange n words
cc             change current line
nc            cchange n lines
c$            changes remainder of the line
Cutting and pasting
Yy       yanks the current line and puts in the buffer
Nyy    yanks n lines
Yw      yanks a word
Nyw    yanks n words
y$        yanks remainder of the line
P, p     copy the contents of the current buffer
U         undo all changes to the current line
U         undo the last command


No comments:

Post a Comment

Thank you :
- kareem