Showing posts with label cat. Show all posts
Showing posts with label cat. Show all posts

Tuesday, 28 February 2012

Unix Basics session 2

Click here for Unix session 1

To go to Session 1 click here
To go to Session 2 click here
To go to Session 3 click here
To go to UNIX basic commands click here
Online sample UNIX TERMINAL click here



Visual editor
                Helps in              
                                Inserting and deleting text
                                Replacing text
                                Moving around a file
                                Finding and substituting string
                                Cutting and pasting text
                                Reading and writing to other text

                While working in vi editor the content of the current file is stored in buffer and we work on that only atlast we may save the changes or quit without saving
Vi editor mode
                Command mode – in this what ever been typed is interpreted as command only cant insert data into file
                Insert mode – to enter into insert mode press ‘I’ in this mode what ever u type is included as text into file to exit from this mode press escape
                Ex command mode – this allows us to give commands the end of the editor we these types of command and these command starts with :
Starting vi deitor
Vi filename – creates a new file
View filename – view file in read only mode
Exiting vi editor
                :w – write the buffer to disk
                :w filename – write the buffer in the given filename
                :wq - write the buffer to disk and quit
                :zz - write the buffer to disk and quit
                :q! – quit without writing edit buffer to disk.
To insert text vi editor
                a – append text after cursor
                A – append text at the end of line
                i – insert before cursor
                I – insert text beginning of line
                o – open a blank line after the current line for the text input
                O - open a blank line before  the current line for the text input
Deleting text
                x- delete charac at cursor
                dh- delete char before cursor
                nx-delete n char at cursor
                dw- delete next word
                db- delete previous word
                dnw- delete n words from cursor
                dnb- delete n words before cursor
                d0- delete to beginning of line
                d$- delete to end of line
                D- delete to end of line
                dd- delete current line
                d(- delete beginning of sentence
                d)- delete to end of sentence
                d{- delete to beginning of paragragh
                d}- delete to end of paragragh
                ndd- delete to lines(starts at the current line)
copy  lines
                yy- copy 1 line to buffer
                nyy- copy n lines ro buffer
                P- out the contents in buffer aft current line
                p- put the contents in buffer before current line
cut and paste
                ndd – cuts/deletes n lines and places it in buffer
                P- out the contents in buffer aft current line
                p- put the contents in buffer before current line
Find and replace
                /str – search forward for str
                ?str – search backward for str
                N – repeat the previous search in reverse direction
                n- find the next occurrence of current string
:s/old/new find and replace first occurrence in current line
:s/old/new/g  find and replace all occurrence in current line
%s/old/new/g find and replace of all occurrence in current file
%s/old/new/gc find and replace of all occurrence in current file with y/n option prompt before every replace.

Setting options
                :Set ai – set auto indent
                 :Set noai– set off auto indent
                :set nu- set line number on
                :set nonu- set off  line number
                :set scroll=n set the number of lines to be scrolled
                :set sw=n set width to n;
                :set wm= set wrapmargin to n .

Unix access and permissions
                Unix is a multi user system.  Each file and directory In unix can be used by many users.               
Permissions  for file or folder are
                r – read
                w- write
                x- execute
permissions are controlled at three level
                u- user
                g- group
                o-others
ls –l displays file/folder permission
                drwxrwxrwx  in this d denotes  directory, rwx denotes (read,write,execute)  permission for user ,  rwx denotes (read,write,execute)  permission for group, , rwx denotes (read,write,execute)  permission for owner
chmod command
                used to change the permission of a file or folder
                chmod a+r jc.txt adds read permission to all users
                chmod o-r jc.txt removes/revokes read permission for others
                chmod og+rx jaya.txt adds read and execute permissions for group and others
                chmod +w * add write permission to all files for users.
Permissions by numerical or octal
                File access permissions can also be given using numerical or octal values
Permission and values:
                r- 4(read only)
                w-2
                x-1
0 – no permission
1 – execute
2 – write only
3 – write and execute (1+2)
4 – read only
5 – read and execute (4+1)
6- read and write(4+2)
7 – read,write and execute(4+2+1)
E.g chmod 777 jayachandran.txt
The above example gives read,write and execute permission to all (owner,user,others)

Process
                Instance of an executing a program is called process.
                A program called SCHEDULER runs in the memory and decides which process should get the CPU

Process ID
In unix every process has a unique id called process id.
Command ps:
                Ps – shows list of process running with its id time and command
                optiions
                                -a process of all users
                                -u username process of a particular user
                                -t tty –process of a particular terminal
                                -f full listing of information
                                -e listing of the system process (Daemons)
Kill command
                Kill – command is used to send a signal to a process mostly this will be kill signal.
                Syntax
                                Kill [signal] pid
                Signal
                STOP
                CONT
                -l
SHELL
                When ever you log in you are placed in a shell, shell works as an interpreter , every command we enter is passed to operating system.
                To determine your shell
                                Echo $SHELL
Types of shell
                Bourne shell
                C shell
                Korn shell
                Tc shell
                Bourne again shell

Communication in UNIX
                Write – two way communication with any person who is currently logged on.
                                write – username
                mesg – controls the messages sent by other users
                                mesg n – prevents others user to write to your terminal.
                                mesg y – allows other users to write to your terminal
                mail – to send mail to user you can send mail even if that user is not logged in.
                                mail username
                                subject: <subject of mail>
                                  <text>
                               <ctrl+d to exit from mail >
                                to do this you need mailx in your system

Unix Basics


For best entertainment links

To go to Session 1 click here
To go to Session 2 click here
To go to Session 3 click here
To go to UNIX basic commands click here
Online sample UNIX TERMINAL click here


Unix basics
File types
1.ordinary files – contains text or image does not have any other files in it
2.directories – branching points in hierarchy level directory tree. May contain other files , special files, other directories
3.special files – any devices connected to unix like(tape drives,printer, and terminal)
                By default unix consider terminal as stdin as well as stdout(standard input and output)
                Special files are normally stored under the directory /dev
4.pipes – temporary file used to link two commands together. (E.g) cat jai1.txt|more output of cat command is stored in a temp file pipe and sent to more command as input.
Symbol
File type
-
Normal file
D
directory
|
Symbolic link
B
Block device file
C
Character device FILE
P
FIFO or named pipe

File names
Unix file name can be upto 256 char, allows to use most char except spaces, tab spaces, and special symbols, unix is case sensitive.
Extensions can be used to identify the type of files.
.a archive file
.c c program fiile
.f forton file
.o object/executable file
.z compressed file
Hidden files begin with .(dot) example .login .mailrc .mwmrc
File names are unique that is under one directory two files cant have the same name.
Path names:
Path – location of a specific file or folder
Types of pathname
                1.absolute pathname -  starts with the root folder /
                2.relative path name – does not start with root folder (E.g) ../bin ~/proj/rep.text
 Unix commands:
                OS is a set of programmed instruction that tells how to work.
An user interaction with OS can take place via command line interpreter (or shell) /terminal window.
Command cat – concatenate
                This command concatenate file or files and puts it in the standard output device.
e.g 1:     cat file1
                cat file1 file2 concatenate file1 and file2 and puts in standard output.
                Cat file1 file2 > file3 concatenate file1 and file2 and makes the file3.
Ls or directory command
                Lists files and sub-directories in directory
                                ls – contents of directory
                                ls filename
                                ls directory (lists contents of directory)
                                ls –a list all files including hidden one
                                ls – al detail list of contents
                                ls –F marks directories with / and executable files as *
                                ls *.doc list all files with extention doc
                                ls –lt list with time of last modification
                                ls –lut show time of last time access
more command
                one screen at a time
                                some helpful tips using more command is
                                                h for help
                                                space for page
                                                b for back
                                                q for quit
                                                /string to search string
                E.g  cat file1.c|more

cp command
                cp command used to copy files. This over writes a file until unless specified.
                Syntax : cp [options]  <source file/s> <destination directory/file>\
               
Options
function
-i
Promotes before overwriting
-r
Copies entire directory

cp f1 f2 copies f1 to f2
cp  *.text dir copies all files with extention .text to directory dir
cp f1 f2 f3 f4 here f4 is taken as destination f1,f2,f3 as source
cp f1 dir copies file f1 to dir directory

cmp command
                cmp command used to compare two files and find is that files identical
                syntax:
                                cmp [options] file1 file2
                                E.g:         cmp jc.txt jaya.txt
                                                cmp –l jc.txt jaya.txt
mv command
                move command mv moves file(s) to specified directory/file. To do this destination should have permission.
                mv file1 file2
                mv *.txt dir moves all files with extension .txt to dir directory
rm command
                 removes or deletes files or folders if permission allowed.
                rm file1 removes file
                rm ja?.txt deletes all text files with first two char ja and last four char .txt
                rm –i * deletes all files in the current directory but asks for each file before deleting
                rm –r/dir recursively delete all files including the directory
date,cal command
                date displays the date and time
                cal displays the current month calendar
                cal 2012 displays the calendar 2012
                cal march 2012 displays march calendar of 2012
                the above can also be given as cal 3 2012
who command
                shows all the user who have logged in
                                with login name,terminal type and number,date and time (* note this info is displayed to users those who r not logged in from the server)
                whoami – displays self user name
                who –q who without detail.
Command finger
                This utility used to get list of user names,full name,terminal name,write status,idle time,login time,machine address.
Word count command
                wc filename list the number of lines,words,char from a file
head command
                Displays first n lines of a file
Tail command
                Displays last n lines of a file
Uname command
                Displays the name of the current system
                Uname [-a] [-i] [-m] [-p][-r] [-s] [-v][-x][-s systemname]
Clear
                To clear the screen
File
                Determines the type of file
Which
                Determines where is the executable for file for the given program
                E.g – which perl , which java
Whereis
                Locates binary,source and manual page files for a command
                Syntax : whereis [-bmsu][-BMS directory] file name
                -b  searches only for binaries
                -m  searches only manual sections
                -s searches only for sources
Directory Related commands
                Pwd – current working directory
                Mkdir – make directory
                Rmdir – remove directory
Redirection and piping
                Redirection – redirecting a command result to a file
                Piping – result of one command is sent as input for another command
Redirection operators
Ø  Indicates redirection of out put., send the output of a command to a file or printer.,
>> indicates redirection of out put of a command to a file which will be appended at last.
Unix wont show the out put in screen/std out put but redicted to a file
E.g
                Cat >file
                Cat file>file1
                Cat file>>file1

<  Indicates redirection of in put., takes the input from a file for a command rather than keybord.,
E.g
                Cat<file
Piping Example
                $ls |wc –l
                                Output of ls is given as output of wc command.
                Pipe with redirection
                                $ls|wc –l>countfile
                $who|sort
                $who|sort>sorted

Click here for Unix Session 2