unix lab record(1)

Upload: baluchandrashekar2008

Post on 03-Apr-2018

239 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/28/2019 Unix Lab Record(1)

    1/23

    Experiment No:Regd no:

    Experiment I:

    Shell Commands:

    VI EDITOR

    vi editor: The vi utility is an interactive screen oriented editor.$vi filename: Used to create a new file.

    $vi myfileMic College of Technology.This is a sample file.

    INSERT COMMANDS:

    i: Insert text before the current character.This is a sample file.Output:

    This is a sample file.

    I: inserts text at the beginning of the current line.Output:Hello.Mic College of Technology.

    This is a sample file.

    a: Appends text after the current character.

    This is a file.Mic College of Technology.Output: After pressing a.

    This is a sample file.Mic College of Technology.

    A: Appends text at the end of the current line.Output: After pressing A.This is a sample file. Mic College of Technology.

    o: Opens an empty text line for new text after the current line.Output:

    This is a sample file.Mic College of Technology.I

    Devineni Venkata Ramana & Dr.Hima Sekhar

    MIC College of Technology

  • 7/28/2019 Unix Lab Record(1)

    2/23

    Experiment No:Regd no:

    O: Opens an empty text line for new text before the current line.

    Output:This is a sample file.IMic College of Technology.

    :set nu- It shows the line numbers.HelloMic College of Technology.

    This is a sample file.Output:1 Hello.

    2 Mic College of Technology.3 This is a sample file.

    :setnonu- It removes the line numbers.Output:Hello.Mic College of Technology.

    This is a sample file.

    %- Locate the matching braces of the loop.main(){

    printf(Hai);}Output:main(){

    printf(Hai);}

    Devineni Venkata Ramana & Dr.Hima Sekhar

    MIC College of Technology

  • 7/28/2019 Unix Lab Record(1)

    3/23

    Experiment No:Regd no:

    CURSOR MOVE COMMANDS:

    Horizontal Moves:

    h: Moves the cursor one character to the left.This is a sample file.Mic college of Technology.Output:

    This is a sample file.Mic College of Technology.

    l: Moves the cursor one character to the right.Output:

    This is a sample.

    Mic College of Technology.

    ^: Moves the cursor to the beginning of the current line.Output:

    This is a sample file.Mic College of Technology.

    $: Moves the cursor to the end of the current line.Output:

    This is a sample file.Mic College of Technology.

    Vertical Moves:

    k: Moves the cursor one line up.Hello.Mic College of Technology.

    This is a sample file.Output:Hello.Mic College of Technology.

    This is a sample file.

    j: Moves the cursor one line down.Output:Hello.Mic college of Technology.

    This is a sample file.

    Devineni Venkata Ramana & Dr.Hima Sekhar

    MIC College of Technology

  • 7/28/2019 Unix Lab Record(1)

    4/23

    Experiment No:Regd no:

    -: Moves the cursor to the beginning of theprevious line.Output:Hello.

    Mic College of Technology.This is a sample file.

    +: Moves the cursor to the beginning of the next line.Output:Hello.Mic College of Technology.

    This is a sample file.

    COPY AND PASTE COMMANDS

    p: To paste the word after the character.Hello.Mic College of Technology.Output: After pressing p.Hello Mic.Mic College of Technology.

    This is a sample file.P: Paste the line after the current line.Output:Hello.Mic College of Technology.

    This is a sample file.Hello.

    yw: Yanking a word.Mic College of Technology.Output:Hello.Mic College of Technology.

    yy: Yanking the entire line.Mic College of Technology.Output:

    This is a sample file.Mic College of Technology.

    Devineni Venkata Ramana & Dr.Hima Sekhar

    MIC College of Technology

  • 7/28/2019 Unix Lab Record(1)

    5/23

    Experiment No:Regd no:

    DELETION COMMANDS:

    x: Deletes the current character.

    Hello.Mic College of Technology.Output:ello.Mic College of Technology.

    dd: Deletes the current line.Hello.Mic College of Technology.Output:Mic college of Technology.

    UNDO COMMANDS:

    u: Undoes only the last edit.ic College of Technology.Output: After pressing u.Mic College of Technology.

    U: Undoes all changes to the current line.Technology.Output: After pressing U.Mic College of Techology.

    SAVE AND EXIT COMMANDS

    :w Saves the content of the buffer without quitting.

    :w filename-Writes the content of the buffer to new file and continues.

    ZZ: Saves the contents of the buffer and exits.

    :wq-saves the contents of the buffer and exits.

    :q-Exits vi.

    :q!-Exits the vi without saving.

    Devineni Venkata Ramana & Dr.Hima Sekhar

    MIC College of Technology

  • 7/28/2019 Unix Lab Record(1)

    6/23

    Experiment No:Regd no:

    To search a word::/word- Here the word is, the word which we wantto search in the file.

    1) pwd Command : (Print Working Directory)This command is used to output the path of the current working

    directory.$pwd/home /system217.

    The directory that the user is currently is in /home/system217.

    2) mkdir : (Make Directory)The mkdir command in the unix operating system is used to make a

    new directory.

    Syntax:mkdir nameofdirectory.

    E.g.: mkdir abc[abc@unixserver] $

    3) cd Command: (Change Directory)cd is a command line used to change the current working

    directory.Syntax:

    cd nameofthedirectory.Eg: cd system217[abc@unixserver system217] $

    $pwd/home/abc/system217

    4) cd..:This command will move up one directory.

    [abc@unixserver system217] $ cd..[abc@unixserver~] $

    Devineni Venkata Ramana & Dr.Hima Sekhar

    MIC College of Technology

  • 7/28/2019 Unix Lab Record(1)

    7/23

    Experiment No:Regd no:

    5) cat command: (Concatenate)

    The cat command is mainly used to display the contents of a smallfiles on the terminal.E.g.:

    $ cat file1This is a sample file.

    Cat also accepts more than one file as arguments.$ cat file1 file2

    This is a simple file.This is a simple file2.

    cat is also used to create files.

    Enter the command cat, followed by > character and the filename.$ cat > file3

    This is a simple file no 3.^d.A > symbol following the command means that the output goes to thefilename following it.^d: Enter the data and finally press ctrl + d to signify the end of input

    to the system.The file is written and the prompt returned.

    6) rmdir: (Remove Directory)when a directory is no longer needed, it should be removed. The

    remove directory (rmdir) command deletes directories. The rmdircommand cannot delete a directory unless it is empty.$ rmdir system217.rmdir: failed to remove system217: directory is not empty.

    7) rm: (Remove file)The rm command used to delete a file.

    Syntax:$ rm filename

    E.g.:[abc @ unixserver system217] $ rm file3[abc @ unixserver system217] $cd..[abc @ unixserver ~] $ rmdir system217[abc @ unixserver ~]

    Devineni Venkata Ramana & Dr.Hima Sekhar

    MIC College of Technology

  • 7/28/2019 Unix Lab Record(1)

    8/23

    Experiment No:Regd no:

    8) 1) who command:The who command displays all users currently logged into the system.

    The who command returns the users name (id), terminal, and time heor she logged in.Syntax:

    $ whosystem217 pts/1 2011-12-16 14:16 (172.16.12.4)system216 pts/2 2011-12-16 14:16 (172.16.12.22)system215 pts/4 2011-12-16 14:16 (172.16.12.6)

    2) $ who -rThis command prints the current run level.run level 5 2011-12-16 09:38 last

    3) $ who -qQuick mode, lists user names and a count.

    $ who -qsystem217 system216 system215

    #users=3

    4) $ who am isystem217 pts/2 2011-12-16 14:28 (172.16.12.26)The system returns your user id. Administrators use the command tofind out who has left the terminal unguarded. It is also used to show theuser id on report.

    9) echo command:The echo command copies its argument back to the terminal.

    $ echo Hello WorldOutput: Hello World.

    10) tty command: (Terminal)The tty utility is used to show the name of the terminal you are

    using.

    $ttyoutput: /dev/pts/12

    11) ls command: (List Directory)The list (ls) command lists the contents in a directory. It can list

    files, directories or subdirectories. If the name of the directory is notprovided, it displays the contents of the working directory.

    1) $ lsbox1 filex pgm1

    Devineni Venkata Ramana & Dr.Hima Sekhar

    MIC College of Technology

  • 7/28/2019 Unix Lab Record(1)

    9/23

    Experiment No:Regd no:

    2) $ ls -lThis command lists the contents in long format, which showsindividual file size, permissions and other data._rw_rw_r_ _ l abc abc 21 2011-12-16 15:22 box1_rw_rw_r_ _ l abc abc 13 2012-01-01 11:16 filex_rw_rw_r_ _ l abc abc 30 2012-01-04 15:35 pgm1

    3) ls -ld:The directory option (-d) displays only the working directory name.

    If used with the long list, it displays the working directory attributes.Output:

    drwx_ _ _ _ _ _ 6 abc abc 3584 2012-01-04 15:35

    4) ls -nd:The list user and group id option (-n) is same as a long list except that

    the user and group ids are displayed rather than the user and groupnames.$ ls -nd

    drwxr_xr_x 2 3988 24 512 Dec16 15:22

    5) $ ls -1File names displays in column wise.Output:

    box2file1file2....

    word.dat

    6) $ ls -p:We can identify directories in a long list by the file type, which is thefirst character of each line. If the file type is d, the file is a directory.

    The -p option appends each directory name with a slash (/).

    abc/ box1 box2 pgm1 filex

    Devineni Venkata Ramana & Dr.Hima Sekhar

    MIC College of Technology

  • 7/28/2019 Unix Lab Record(1)

    10/23

  • 7/28/2019 Unix Lab Record(1)

    11/23

    Experiment No:Regd no:

    13) mv command: (Move Command)The move command is used to move either an individual file, a list of

    files, or a directory.After a move, the old file filename is gone and the new filename is foundat the destination.

    Syntax:$ mv src dest.

    $ mv student2 bookcat: student2: no such file or directory.

    $ cat book101 abc qwe 12 23102 xyz ert 45 45103 pqr ert 45 56

    14) ln Command: (Link Command)ln command is used to create links for files and allowing them to beaccessed different names.

    $ ln file1 file2

    Hard link:To create a hard link to a file, we specify the source file and thedestination file. If the destination file does not exist, it is created. If itexists, it is first removed and then recreated as a linked file.

    $ ls lnDir$

    $ln file1 lnDir/linkedfile$ls -i file179914 file1$ls -i lnDir/linkedfile79914 lnDir/linkedfile.

    $ln -s file1 file2Inodes are not same.

    Devineni Venkata Ramana & Dr.Hima Sekhar

    MIC College of Technology

  • 7/28/2019 Unix Lab Record(1)

    12/23

    Experiment No:Regd no:

    Symbolic link:We must also use symbolic links when we are linking to directories.Hard links can be used only with files.Soft links can be used with both files and directories.

    1) $ ln -i:If the destination file already exists, its contents are alreadydestroyed unless we request to be warned by using the interactiveflag.

    When the interactive flag is on, link asks if we want to destroy theexisting file.

    $ cat sample6 $ cat sample71 abc xyz This is a file2 xyz abc Mic college of Technology.asd1234

    $ ln -i sample6 sampl7$ln: replace sample7? Y$ cat sample71 abc xyz2 xyz abc inode nos of sample6 and sample7 are same.asd1234

    2) $ ln -s box2 box3 must not existOutput: ln:creating symbolic link box3: file exists.$ ln -s box2 box4Success $ appears.

    $ ln -li box2Output: same inode numbers.

    15) ps command: (Process Status)The ps command displays information about the individual processes

    that executing on the system.$psPID TTY TIME COMMAND271 01 0:03 sh468 01 0:00 ps

    Devineni Venkata Ramana & Dr.Hima Sekhar

    MIC College of Technology

  • 7/28/2019 Unix Lab Record(1)

    13/23

    Experiment No:Regd no:

    16) Redirecting Output (>):When we redirect standard output, the commands output is copied to

    a file rather than displayed on the monitor.$ ls > book$ cat > bookbookbox1box2

    .

    .^d

    17) at command:at command allows the user to specify the time when a command is

    to be executed.The at is used to run a series of commands at a time specified by theuser.

    The at command takes 2 arguments, the time at which the command isto be executed, and the command to be executed.Syntax:

    $ at time[date][increment]$at now+1 dayat > ^d

    job 123 at 2012-01-04 15:54

    18) Find Command:The find command is used to search for the file.

    Syntax:$ find pathnames(s) condition(s)

    The first argument is the path that we want to search.The second argument is the criterion that finds needs to complete itssearch.E.g.:

    xyz dir system217 dirfile1 file sample filepqr fileabc dir

    Devineni Venkata Ramana & Dr.Hima Sekhar

    MIC College of Technology

  • 7/28/2019 Unix Lab Record(1)

    14/23

    Experiment No:Regd no:

    1)$ find xyz -name sample1 -printxyz |system217| sample1 (in xyz dir, cmd is given)

    2)$ find system217 -name sample1 - printfind: system217: No such file or directory.

    3)To retrieve the modified | created files from 20 days ago$ find xyz -type f -mtime -20

    Output:xyz | file1xyz | system217 | sample1.

    $ find xyz -type f -mtime -30Output:xyz | pqrxyz | file1xyz | system217 | sample1

    19) Translating Characters (tr):It copies standard input to standard output by performing

    substitution of characters from string1 to string2 or deletion ofcharacters in string1.1)$ tr ege EGE

    collegeCollEGEeggEGG

    2)$ tr abcd AB (cd also replaced with B)

    abbb abbbb abbb ccccc ddddABBB ABBBB ABBB BBBBB BBBB3)$ tr ege EGE < sample1.txt

    collEGE4)$ tr ab AB < sampl1.txt

    cabcAB

    5)Deleting Characters:Delete characters in string 1 from output.

    $ tr -d ege < file1.txtMic coll of Tchnology.

    Devineni Venkata Ramana & Dr.Hima Sekhar

    MIC College of Technology

  • 7/28/2019 Unix Lab Record(1)

    15/23

    Experiment No:Regd no:

    6) Squeeze out repeated characters into a single one.$ tr -s a x < file1.txtaaaaa bbbbb aaaaa bbbbbx bbbbb x bbbbbComplement: (Reverse translation)

    Complement character in string1 with ascii.

    file1.c aaaaa bbbbb aaaaa bbbbb$ tr -c ab * < file1.caaaaa * bbbbb * aaaaa * bbbbb

    7)cmd to convert words into lines$ tr -c a-z \n < file1.txtaaaaabbbbb or $ tr -c [:alpha:] \n < file1.txtaaaaa

    20) WC:The wc command used to determine the length of the given file.Syntax:

    $ wc filename$ wc < file1.txt

    1 4 24 lines words characters

    21) head command:This command prints the first 10 lines of one or more files by default.

    Syntax:$ head -1 file(s)-n is used to print the no of lines which we specify.

    Student1101 abc qwe 12 23102 xyz ert 45 45103 pqr ert 45 56

    Head command displays required no of lines.

    Devineni Venkata Ramana & Dr.Hima Sekhar

    MIC College of Technology

  • 7/28/2019 Unix Lab Record(1)

    16/23

    Experiment No:Regd no:

    1) $ head -2 student1101 abc qwe 12 23102 xyz ert 45 45

    2) $ head -1 student1101 abc qwe 12 23

    22) Tail command:It prints last 10 lines of one or more files by default.Syntax:

    $ tail option file(s)$ tail -2 student1

    102 xyz ert 45 45103 pqr ert 45 56

    23) cut command:Cut command is used to select a list of column or fields from one ormore files.

    Sample1HiI am sindhuStudying mtech 1st year cseMic college of technology$ cut c1-11 sample1

    Output:HiI am sindhuStudying mt

    Mic college

    24) Paste Command:Paste command takes two tables and combines them, side by side to forma single wide table as output.Sample6 sample71 abc xyz this is a file2 xyz abc this is xyzAsd

    $ paste sample6 sample7

    Devineni Venkata Ramana & Dr.Hima Sekhar

    MIC College of Technology

  • 7/28/2019 Unix Lab Record(1)

    17/23

    Experiment No:Regd no:

    1 abc xyz this is a file2 xyz abc this is xyzasd

    25) cmp command: (Compare Command)This command compares two files byte by byte and they need to be

    sorted. The difference byte number is displayed to the user. If similarnothing is displayed.

    box1 box21 abc 123 1 abc 123

    2 cde 345 2 cde 345Kj kj df po qw

    1) $ cmp box1 box2box1 box2 differ: byte 21, line3

    2) $ cmp -lFor each difference, print the byte number in decimal and thediffering byte in octal.$ cmp -l box1 box221 153 16022 152 15724 153 16125 152 167Cmp: EOF of box2

    3) $ echo $?0 if both are identical1 not identical

    26) diff command: (Difference Command)diff command reports lines that differ between file1 and file2.

    Student1 student2101 abc qwe 12 23 101 abc qwe 12 23102 xyz ert 45 45 102 xyz ert 45 45103 pqr ert 45 56 103 slddk skld 12 231) $ diff student1 student2

    3c3105 slkdk skld 12 13

    Devineni Venkata Ramana & Dr.Hima Sekhar

    MIC College of Technology

  • 7/28/2019 Unix Lab Record(1)

    18/23

    Experiment No:Regd no:

    2) diff delete(d)Indicates what lines must be deleted

    from the file1 to make it the same as file2.

    Delete can occur only if file1 is longer than file2.

    file1 file212 23 34 12 23 34

    56 67 78 56 67 781 1 1 1

    $ diff file1 file23d2 6 7 8 9> 1 2 3 4

    27) Common Command:Comm. Command compares lines common to the sorted files file1 andfile2. It produces three column outputs.First column shows unique to first file.Second column shows lines unique to second file.

    Third column shows common to both files.

    Devineni Venkata Ramana & Dr.Hima Sekhar

    MIC College of Technology

  • 7/28/2019 Unix Lab Record(1)

    19/23

    Experiment No:Regd no:

    file1 file23 x and y 4 same4 same 3 x and y$ comm file1 file2

    3 x and y 4 same

    28) tee command:Any output from a command that gts piped into another command is notseen at the terminal.

    $ cat sortf1 $cat sortf2

    Australia CaliforniaChina ChinaIndia India

    Japan NepalNewzealand Tanzania

    Uganda

    $ comm sortf1 sortf2AustraliaCaliforniaChinaIndiaJapanNepalNewzealandTanzaniaUganda

    29) Command Executions:1) sequence executions:

    $who: date, file 9 cat file1Output:20 tty7 2012-01-0411:13(:0)abc pts/2 2012-01-04 11:15(172.16.32.32)sat Jan4 11:22:04 IST 2012

    total 64_rw_rw_r_ _ 1 abc 21 2011-11-16

    $ who; date: file2Already data and date is stored.

    Devineni Venkata Ramana & Dr.Hima Sekhar

    MIC College of Technology

  • 7/28/2019 Unix Lab Record(1)

    20/23

    Experiment No:Regd no:

    2) Grouped Execution:$ (who; date; ls -l) >> file2who command outputdate outputls -l o/p all will be appended to file2.

    3) Chained Execution:$ who | grep bindu

    Output: bindu pts/8 2011-01-0810:03 (172.10.12.5)

    4) Conditional Execution(&&,||):1) $cp file1 file2 && echo file copied successfully.

    Output: file copied successfully.2)$ cp file1 file2 || echo file copied unsuccess.

    Output: cp: cannot state file1; no such file or dirFile copied unsuccessfully.

    Quotes:

    30) 1) Backslash: \

    1) $echo 2*3Printing just filenames.Output: 2 box1 box2 file1 file2 filex

    2) $ echo 2*3

    Output:2*3

    3) echo hello worldOutput:

    Hello world.

    4) $ echo < > \$Syntax error.

    Devineni Venkata Ramana & Dr.Hima Sekhar

    MIC College of Technology

  • 7/28/2019 Unix Lab Record(1)

    21/23

    Experiment No:Regd no:

    2) Double Quotes:$ x=hello.$ echo < > $x y ? &

    Output:< > hello y ? &$ echo < > \ &x y ? &

    Output:

    < > $x y ? &

    3)Single Quotes:$echo < > $x y ? &

    Output:< > &x y ? &

    31) Command Substitution:

    1) $ echo today date is: dateOutput: Today date is: date.

    2) $ echo today date is ($dte)Output: Today date is: Thu jan4 12:02:53 IST 2012

    32) Creation of user defined values$name=sindhu$echo $nameSindhu

    33) Creating alias:$alias aliasname=ls$alias namebox1 box2 simple1 simple2

    Remoing alias:$unalias alias name$aliasnameError

    Devineni Venkata Ramana & Dr.Hima Sekhar

    MIC College of Technology

  • 7/28/2019 Unix Lab Record(1)

    22/23

    Experiment No:Regd no:

    34) grep:The original of the file-matching utilities. Grep handles most of theregular expressions.Grep allows regular expressions but is generally slower than egrep.Grep is used to find all the lines that end in a semicolon (;) and thenpipe the results to head and print the first five.

    $grep n -3i[a-z] sample6

    Output:1: 1 abc xyz2: 2 xyz abc sample63: asd

    -n: shows line numbers of each line before the line.

    35) awk Script:To find totals of each record in a file.

    Total.awk

    BEGIN{ Print Totals}{total= $1+$2+$3}{print $1+$2+$= total}END{ print END TOTALS}

    $awk -f total.awk total.datOutput:

    Print Totals total.dat22+78+44=144 22 78 44

    66+31+70=167 66 31 7052+30+44=126 52 30 4488+31+66=185 88 31 66

    END TOTALS

    Devineni Venkata Ramana & Dr.Hima Sekhar

    MIC College of Technology

  • 7/28/2019 Unix Lab Record(1)

    23/23