0% found this document useful (0 votes)
108 views

Unix Lab Record

The document describes various commands used in the vi text editor and Linux shell. It explains how to insert and paste text, move the cursor, search, save and exit in vi. For the shell, it covers commands to create and remove directories and files, view file contents, copy and move files, and get information about users logged in and the terminal being used. The document serves as a tutorial for basic vi and Linux shell commands and their usage.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
108 views

Unix Lab Record

The document describes various commands used in the vi text editor and Linux shell. It explains how to insert and paste text, move the cursor, search, save and exit in vi. For the shell, it covers commands to create and remove directories and files, view file contents, copy and move files, and get information about users logged in and the terminal being used. The document serves as a tutorial for basic vi and Linux shell commands and their usage.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 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 myfile Mic 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

Experiment No: Regd no:

O: Opens an empty text line for new text before the current line. Output: This is a sample file. I Mic College of Technology. :set nu- It shows the line numbers. Hello Mic 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

Experiment No: Regd no: CURSOR MOVE 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. COMMANDS:

Devineni Venkata Ramana & Dr.Hima Sekhar

MIC College of Technology

Experiment No: Regd no: -: Moves the cursor to previous line. Output: Hello. Mic College of Technology. This is a sample file. the beginning of the

+: 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

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

Experiment No: Regd no: To search a word: :/word- Here the word is, to search in the file.

the word which we want

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

Experiment No: Regd no:

5) cat command: (Concatenate) The cat command is mainly used to display the contents of a small files on the terminal. E.g.: $ cat file1 This 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 the filename 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 rmdir command 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

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 he or she logged in. Syntax: $ who system217 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 -r This command prints the current run level. run level 5 2011-12-16 09:38 last 3) $ who -q Quick mode, lists user names and a count. $ who -q system217 system216 system215 #users=3 4) $ who am i system217 pts/2 2011-12-16 14:28 (172.16.12.26) The system returns your user id. Administrators use the command to find out who has left the terminal unguarded. It is also used to show the user id on report. 9) echo command: The echo command copies its argument back to the terminal. $ echo Hello World Output: Hello World. 10) tty command: (Terminal) The tty utility is used to show the name of the terminal you are using. $tty output: /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 not provided, it displays the contents of the working directory. 1) $ ls box1 filex pgm1

Devineni Venkata Ramana & Dr.Hima Sekhar

MIC College of Technology

Experiment No: Regd no:

2) $ ls -l This command lists the contents in long format, individual file size, permissions and other data. _rw_rw_r_ _ l abc abc 21 2011-12-16 _rw_rw_r_ _ l abc abc 13 2012-01-01 _rw_rw_r_ _ l abc abc 30 2012-01-04

which shows 15:22 11:16 15:35 box1 filex 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 group names. $ ls -nd drwxr_xr_x 2 3988 24 512 Dec16 15:22 5) $ ls -1 File names displays in column wise. Output: box2 file1 file2 . . . . word.dat 6) $ ls -p: We can identify directories in a long list by the file type, which is the first 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

Experiment No: Regd no:

7) $ ls | more Pagewise filenames are displayed. file1 file2 . . . [more] filex . . . 8) $ -li Print inode for all files. 12) cp command: (Copy Command) The copy command creates a duplicate of a file, a set of files or directory. If the source is a file, the new file contains an exact copy of the data in the source file. If the source is a directory, all the files in the directory are copied to the destination, which must be directory. If the destination file already exists, its contents are replaced by the source file contents. Syntax: $ cp src dest. E.g.: $ cat student1 101 abc qwe 12 102 xyz ert 45 103 pqr ert 45 $ cp student1 student2 $ cat student2 101 abc qwe 12 102 xyz ert 45 103 pqr ert 45 23 45 56

23 45 56

101 102 103

$ cat student2 abc qwe 12 xyz ert 45 slddk skld 12

23 45 23

Devineni Venkata Ramana & Dr.Hima Sekhar

MIC College of Technology

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 found at the destination. Syntax: $ mv src dest. $ mv student2 book cat: student2: no such file or directory. $ cat book 101 abc 102 xyz 103 pqr qwe 12 ert 45 ert 45 23 45 56

14) ln Command: (Link Command) ln command is used to create links for files and allowing them to be accessed different names. $ ln file1 file2 Hard link: To create a hard link to a file, we specify the source file and the destination file. If the destination file does not exist, it is created. If it exists, it is first removed and then recreated as a linked file. $ ls lnDir $ $ln file1 lnDir/linkedfile $ls -i file1 79914 file1 $ls -i lnDir/linkedfile 79914 lnDir/linkedfile. $ln -s file1 file2 Inodes are not same.

Devineni Venkata Ramana & Dr.Hima Sekhar

MIC College of Technology

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 already destroyed unless we request to be warned by using the interactive flag. When the interactive flag is on, link asks if we want to destroy the existing file. $ cat sample6 1 abc xyz 2 xyz abc asd 1234 $ cat sample7 This is a file Mic college of Technology.

$ ln -i sample6 sampl7 $ln: replace sample7? Y $ cat sample7 1 abc xyz 2 xyz abc inode nos of sample6 and sample7 are same. asd 1234 2) $ ln -s box2 box3 must not exist Output: ln:creating symbolic link box3: file exists. $ ln -s box2 box4 Success $ appears. $ ln -li box2 Output: same inode numbers. 15) ps command: (Process Status) The ps command displays information about the individual processes that executing on the system. $ps PID TTY TIME COMMAND 271 01 0:03 sh 468 01 0:00 ps

Devineni Venkata Ramana & Dr.Hima Sekhar

MIC College of Technology

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 > book book box1 box2 . . ^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 the user. The at command takes 2 arguments, the time at which the command is to be executed, and the command to be executed. Syntax: $ at time[date][increment] $at now+1 day at > ^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 its search. E.g.: xyz dir system217 dir file1 file sample file pqr file abc dir

Devineni Venkata Ramana & Dr.Hima Sekhar

MIC College of Technology

Experiment No: Regd no:

1) $ find xyz -name sample1 -print xyz |system217| sample1 (in xyz dir, cmd is given) 2) $ find system217 -name sample1 - print find: 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 | file1 xyz | system217 | sample1. $ find xyz -type f -mtime -30 Output: xyz | pqr xyz | file1 xyz | system217 | sample1 19) Translating Characters (tr): It copies standard input to standard output by performing substitution of characters from string1 to string2 or deletion of characters in string1. 1) $ tr ege EGE college CollEGE egg EGG 2) $ tr abcd AB (cd also replaced with B) abbb abbbb abbb ccccc dddd ABBB ABBBB ABBB BBBBB BBBB 3) $ tr ege EGE < sample1.txt collEGE 4) $ tr ab AB < sampl1.txt cab cAB 5) Deleting Characters: Delete characters in string 1 from output. $ tr -d ege < file1.txt Mic coll of Tchnology.

Devineni Venkata Ramana & Dr.Hima Sekhar

MIC College of Technology

Experiment No: Regd no:

6) Squeeze out repeated characters into a single one. $ tr -s a x < file1.txt aaaaa bbbbb aaaaa bbbbb x bbbbb x bbbbb Complement: (Reverse translation) Complement character in string1 with ascii. file1.c aaaaa bbbbb aaaaa bbbbb $ tr -c ab * < file1.c aaaaa * bbbbb * aaaaa * bbbbb 7) cmd to convert words into lines $ tr -c a-z \n < file1.txt aaaaa bbbbb or $ tr -c [:alpha:] \n < file1.txt aaaaa 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. Student1 101 abc qwe 12 23 102 xyz ert 45 45 103 pqr ert 45 56 Head command displays required no of lines.

Devineni Venkata Ramana & Dr.Hima Sekhar

MIC College of Technology

Experiment No: Regd no:

1) $ head -2 student1 101 abc qwe 12 102 xyz ert 45 2) $ head -1 student1 101 abc qwe 12

23 45 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 45 103 pqr ert 45 56 23) cut command: Cut command is used to select a list of column or fields from one or more files. Sample1 Hi I am sindhu Studying mtech 1st year cse Mic college of technology $ cut c1-11 sample1 Output: Hi I am sindhu Studying mt Mic college 24) Paste Command: Paste command takes two tables and combines them, side by side to form a single wide table as output. Sample6 sample7 1 abc xyz this is a file 2 xyz abc this is xyz Asd $ paste sample6 sample7

Devineni Venkata Ramana & Dr.Hima Sekhar

MIC College of Technology

Experiment No: Regd no: 1 abc 2 xyz asd xyz abc this is a file this is xyz

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 similar nothing is displayed. box1 1 abc 2 cde Kj kj box2 123 345 df 1 2 po abc cde qw 123 345

1) $ cmp box1 box2 box1 box2 differ: byte 21, line3 2) $ cmp -l For each difference, print the byte number in decimal and the differing byte in octal. $ cmp -l box1 box2 21 153 160 22 152 157 24 153 161 25 152 167 Cmp: EOF of box2 3) $ echo $? 0 if both are identical 1 not identical 26) diff command: (Difference Command) diff command reports lines that differ between file1 and file2. Student1 101 abc qwe 12 102 xyz ert 45 103 pqr ert 45 23 45 56 101 102 103 student2 abc qwe 12 xyz ert 45 slddk skld 12 23 45 23

1) $ diff student1 student2 3c3 <101 pqr ert 45 45 ..... >105 slkdk skld 12 13

Devineni Venkata Ramana & Dr.Hima Sekhar

MIC College of Technology

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 12 56 1 1 1 23 67 1 34 78

file2 12 23 56 67

34 78

$ diff file1 file2 3d2 <1 1 1 1 \no new line at end of file. 3) diff append(a): Indicates what lines needed to be added to file1 make it same as file2. Append can take place only at the end of file1, they occur only when file1 is shorter than file2. file1 file2 12 23 34 12 23 34 56 67 78 56 67 78 111 1 2 2 2 2 6789 1234 $ diff file1 file2 3c3, 5 (3 to 5 cut & paste) <1 1 1 1 \n no new line at the end >2 2 2 2 >6789 >1234 27) Common Command: Comm. Command compares lines common to the sorted files file1 and file2. 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

Experiment No: Regd no: file1 3 x and y 4 same $ comm file1 file2 3 x and y 4 same file2 4 same 3 x and y

28) tee command: Any output from a command that gts piped into another command is not seen at the terminal. $ cat sortf1 Australia China India Japan Newzealand $cat sortf2 California China India Nepal Tanzania Uganda

$ comm sortf1 sortf2 Australia California China India Japan Nepal Newzealand Tanzania Uganda 29) Command Executions: 1) sequence executions: $who: date, file 9 cat file1 Output: 20 tty7 2012-01-04 11: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: file2 Already data and date is stored.

Devineni Venkata Ramana & Dr.Hima Sekhar

MIC College of Technology

Experiment No: Regd no:

2) Grouped Execution: $ (who; date; ls -l) >> file2 who command output date output ls -l o/p all will be appended to file2. 3) Chained Execution: $ who | grep bindu Output: bindu pts/8 2011-01-08 10: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 dir File copied unsuccessfully. Quotes: 30) 1) Backslash: \ 1) $echo 2*3 Printing just filenames. Output: 2 box1 box2 file1 file2 filex 2) $ echo 2*3 Output: 2*3 3) echo hello world Output: Hello world. 4) $ echo < > \$ Syntax error.

Devineni Venkata Ramana & Dr.Hima Sekhar

MIC College of Technology

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: date Output: 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 $name Sindhu 33) Creating alias: $alias aliasname=ls $alias name box1 box2 simple1 simple2 Remoing alias: $unalias alias name $aliasname Error

Devineni Venkata Ramana & Dr.Hima Sekhar

MIC College of Technology

Experiment No: Regd no:

34) grep: The original of the file-matching utilities. Grep handles most of the regular 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 then pipe the results to head and print the first five. $grep n -3i[a-z] sample6 Output: 1: 1 abc xyz 2: 2 xyz abc sample6 3: 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.dat Output: Print Totals total.dat 22+78+44=144 66+31+70=167 52+30+44=126 88+31+66=185 END TOTALS

22 66 52 88

78 31 30 31

44 70 44 66

Devineni Venkata Ramana & Dr.Hima Sekhar

MIC College of Technology

Experiment No: Regd no:

Devineni Venkata Ramana & Dr.Hima Sekhar

MIC College of Technology

You might also like