You are on page 1of 15

OS Practical

File

Arvind C.
Roll no. - 14002
Section - CS1

2
Unix Commands :

1. $ clear - clear screen


2. $ who - details of users logged in. in the format: username terminal id date
time. For eg,
$ who
Kumar console May 9 09:31 (:0)
Vipul pts/4 May 9 09:31 (:0.0)
Raghav pts/5 May 9 09:31 (saturn.harvest.com)
3. $ who am i - your login details. For eg,
$ who am i
Kumar pts/10 Aug 1 07:56 (pc123.heavens.com)
4. $ cal - calendar of the current month. For eg,
$ cal
February 2008
S M Tu W Th Fr S
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29
5. $ cal 2008 - calendar of 2008.
6. $ cal 2 2008 - calendar of February 2008.
7. $ pwd - displays path of current working directory. For eg,
$ pwd
/home/student
8. $ ls - displays list of all files and directories of current working directory. For
eg,
$ls
08_packets.html
TOC.sh
Calendar
Cptodos.shdept.lst

3
9. $ ls –l - displays same list as $ ls with detailed information. For eg,
Total 72
-rw-r—r-- 1 Kumar metal 19514 May 10 13:54
chap01
-rw-r—r-- 1 Kumar metal 4174 May 10 15:04
chap02
drwxr-xr-x 2 Kumar metal 512 May 09 10:31 helpdir
10. $ cd [directory name] - change directory. For eg,
$pwd
/home/kumar
$ cd progs
$pwd
/home/kumar/progs
11. $ cd .. – takes you back to previous directory
$pwd
/home/kumar/progs
$ cd ..
$pwd
/home/kumar
12. $ cat - create and view contents of a file. For eg,
$ cat fl.txt
This is a text file.
13. $ cp oldfilename newfilename - copies contents from old file to new file
14. $ rm filename - removing a file
15. $ mv file1 file2 - move file from one location to another.
16. $ date - specifies system date. For eg,
Thur Feb 14 17:31:51 IST 2008
17. $ mkdir dirname – make directory
18. $ rmdir dirname - remove directory
19. $ wc[-l/-w/-c] filename - displays number of line words and character in a file.
For eg,
$ wc fl.txt
1 5 20 fl.txt

4
20. $ sort abc - it is used to arrange the files alphabetically in ascending or
descending order.
21. $ grep <pattern> <filename> - searching a pattern in a file. For eg,
$ grep “is” fl.txt
This is a text file.
22. $ shutdown now - shutdown.
23. $ echo “message” - to print a message. For eg,
$ echo Unix
Unix
24. $ printf “message” - to print message, it doesn’t take the next line command. For
eg,
$ printf Unix
Unix
25. $ man ls - displays complete information about ls, page by page.
26. $ man ls | - displays complete information about ls, line by line
27. $ tty - displays terminal name. For eg,
$ tty
/dev/pts/10
28. $ ls –a – shows hidden files.
29. $ ls –x - shows output in column format.
30. $ ls –F - all executable files.
31. $ ls –d - directory.
32. $ ls –afx - hidden and executable files are displayed in column format.
33. $ echo $shell - shell state.
34. $ set - system variables and know environment. For eg,
$ set
HOME=/home/henry
IFS=’

LOGNAME=henry
MAIL=/var/mail/henry
MAILCHECK=60
PATH=/bin:/usr/bin:.:/usr/ccs/bin

5
PS1=’$ ‘
PS2=’> ‘
SHELL=/usr/bin/bash
TERM=xterm
35. $ chmod - change mode.
36. $ compress filename - file is compressed as filename.Z.
37. $ uncompressed abc.Z - to uncompress abc.
38. $ gzip - to compress HTML.
39. $ gunzip - to uncompressthe file.
40. $ zip fin *.html - creates an archive.

6
VI Editor :
$ vi

1. i - to type anything (insert mode)


2. escape key – command mode
3. : - last line mode
4. : sh - exits to UNIX shell
5. : x – saves files and quits editing mode
6. : w - saves files and remains in editing mode
7. : wq - same as :x
8. : q! - quits editing mode, but after abounding changes
9. : q - quits editing mode when no changes are made to the file
10. Ctrl Z – suspends current session and exits to UNIX shell
11. I – takes you to the beginning of the line to insert
12. a/A – append mode
13. o/O - insert blank line (Open mode)
14. r/R – replace character
15. s/S - deleting words/ lines.
16. : 5w - 5 lines saved
17. 20i* - prints * 20 times
18. h - to remove character
19. l - space
20. 5l – move 5 spaces ahead
21. k - move cursor up
22. j – move down
23. $ - move to end of line
24. e – move to end of word
25. w - moves forward to beginning of word
26. 10G - go to line 10
27. Ctrl+F - page forward
28. Ctrl+B - scrolls full page backward
29. Ctrl+D – half page forward
30. Ctrl+u - half page backwards

7
31. Ctrl+L – redraws screen
32. d - delete a char
33. y – yanking
34. c – change char
35. ! - to act on text
36. dd - to delete a line
37. x – deletes a char on which the cursor is
38. X – deletes a char just before the cursor
39. 3J – joins 2 lines with 1st
40. D$ or D or dd – delete line
41. yy – copy complete line
42. 5yy – copy 5 lines
43. y$ - copies text from current position of cursor to end of line
44. y1G – copies text from current position to beginning of the file
45. p - paste
46. :1, $s/message/msg/g = search and replace message with msg in 1st line
47. fch = move in forward direction
48. /pat = forward direction
49. Fch = backward direction
50. ?pat = backward direction
51. U = undoing in the current line
52. /n = forward
53. /N = backward
54. Ctrl+pat = search for pattern at beginning of line.
55. $pat = end of line
56. : 3, 10s/message/msg/g = replace message by msg from line 1 to 10
57. :1,$s/message/msg = replaces 1st occurrence in every line.
58. \< = used to match a pattern at the beginning of a word
59. \> = to match a pattern at the end of a word
60. \msg\> = end of a line
61. : r abc = reads file abce below current file
62. : r !date = reads output of date command below current line
63. : e abc = stops editing current file and will start editing the file abc
8
64. : e! abc = same as e abc but after abounding changes made to the current file
65. : e! = loads last saved addition of the file
66. : n = edits next file when vi is invoked with multiple files
67. : rew = rewinds file list to start editing 1st file
68. Ctrl+q = return to original file
69. : e# = return to original file
70. mq = used to mark a location with character q.
71. “2p = to recover 2 deletions.
72. : ab who world health organization = for abbreviating world health organization as
who.
73. : set aw = autosave when jumping files.

9
Shell Programming:

Program-1

Objective:
If basic salary of the employee is less than Rs.1500 then H.R.A is equal to 10% of it`s
basic salary and D.A is 90% of basic salary. If basic salary either equal to or above
Rs.1500 , H.R.A will be 500 and D.A will be 98% of the basic salary . If the employ
salary input through the keyboard write a program in unix shell programming to find the
gross salary.

Code:
$vi abc
Echo “Enter basic salary :”
read bs
if [ $bs –lt 1500 ]
then
hra=`echo $bs \* 0.1|bc`
da=`echo $bs \* 0.9|bc`
else
hra=500
da=`echo $bs \* 0.98|bc`
fi
gs=`echo $bc+$hra+$da|bc`
echo “Gross salary is Rs.”$gs

Output:
$ sh abc
Enter basic salary :
1400
Gross salary is Rs.2800

Program-2

Objective:
The marks obtain a student in five different subjects are input through the keyboard. The
student gets,
1. first division if % above or equal to 60%
2. second division if % between 50%-59%
3. third division if % between 40%-49%
4. fail if % less than 40%
Write a program to calculate the division obtain by the student using logical operator.

Code:
$vi abc

10
echo “Enter the marks of five subject :”
read m1
read m2
read m3
read m4
read m5
per =`echo ($m1 + $m2 + $m3 + $m4 + $m5)/5|bc`
if [ $per –ge 60 ]
then
echo “First division.”
fi
if [ $per –ge 50 –a $per –le 60 ]
then
echo “Second division.”
fi
if [ $per –ge 40 –a $per –le 50 ]
then
echo “Third division.”
fi
if [ $per –le 40 ]
then
echo “Fail.”
fi

Output:
$ sh abc
Enter the marks of five subject :
100
90
100
99
100
First division.

Program-3

Objective:
Write a programme in shell programming to find whether the input year is leap year or
not.

Code:
$vi abc
echo “Enter the year :”
read yr
con1=`echo $yr \% 4|bc`
con2=`echo $yr \% 100|bc`

11
con3=`echo $yr \% 400|bc`
if [ $con1 –eq 0 –a $c –ne 0 –o $b –eq 0]
then
echo “Leap year.”
else
echo “Not a leap year”
fi

Output:
$ sh abc
Enter the year :
2000
Leap year

Program-4

Objective:
Write a programme in shell programming to find the factorial of a given no.

Code:
$vi abc
echo “Enter a no. :”
read n
i=1
fact=1
while [ $i –le $n ]
do
fact= `echo $fact \* $i|bc`
i=`echo $i + 1|bc`
done
echo $fact

Output:
$ sh abc
Enter a no. :
5
120

Program-5

Objective:
Write a programme to find the sum of five digit number.

Code:
$vi abc
echo “Enter five digit no. :”

12
c=0
read no
while [ $no gt 0 ]
do
b=`expr $no % 10`
no=`expr $no / 10`
c=`expr $b + $c`
done
echo “Sum of digits is ”$c

Output:
$ sh abc
Enter five digit no. :
12345
Sum of digits is 15

Program-6

Objective:
Write a programme to enter an integer through the keyboard and find whether the given
no. is even or odd.

Code:
$vi abc
echo “Enter a given no. :”
read choice
x=`echo $choice % 2|bc`
if [ $x –eq 1 ]
then
echo “No. is odd.”
else
echo “No. is even.”
fi

Output:
$ sh abc
Enter a given no. :
12
No is even.

Program-7

Objective:
Write a program to find the value of one no. raised to the power of anothor no.

13
Code:
$vi abc
echo “Enter a given no. :”
read a
echo “Enter a no. :”
read b
ans=1
while [ $b –gt 0 ]
do
ans=`echo $ans \* $a|bc`
b=`echo $b -1|bc`
done
echo The value of $a rasied to the power $b is $ans

Output:
$ sh abc
Enter a given no. :
10
Enter a no. :
2
The value of 10 rasied to the power 2 is 100

Program-8

Objective:
Write a programme to display the smallest no. between three given no.

Code:
$vi abc
echo “Enter three no. a,b and c :”
read a
read b
read c
if [ $a –gt $b –a $a –gt $c ]
then
echo “a is greater than b and c.”
fi
if [ $b –gt $a –a $b –gt $c ]
then
echo “b is greater than a and c.”
fi
if [ $c –gt $a –a $c –gt $b ]
then
echo “c is greater than a and b.”

14
fi

Output:
$ sh abc
Enter three no. a,b and c :
10
20
30
c is greater than a and b.

Program-9

Objective:
Write a programme to find the simple interest.

Code:
$vi abc
echo “Enter the principle amount :”
read p
echo “Enter the rate of interest :”
read r
echo “Enter the time :”
read t
si=`echo ($p \* $r \* $t )/100|bc`
echo “The simple interest is” $si

Output:
$ sh abc
Enter the principle amount :
1000
Enter the rate of interest :
4
Enter the time :
1
The simple interest is 40

15