28 October, 2010

MCSL-045 UNIX LAB MANUAL SOLVED

Session 1

1) Explore all the UNIX commands given in this manual.


Answer- Information:-
1. Date- show date and time.
2. History- list of previously executed commands.
3. Man- show on-line documentation by program name.
4. Who- who is on the system and what are they doing.
5. Who am i- who is logged onto this system.

File Management:-
1. Cat- combine files.
2. Cp- copy file.
3. Ls- list of file in a directory and their attributes.
4. Mv- change file name or directory location.
5. Chmod- set file permission.

Display contents of files:-
1. Cat- copy file to display device.
2. Vi- Screen editor for modify text files.
3. More- show text files on display terminal with paging control.
4. Head- show first few lines of a file.
5. Tail- show last few lines of file or reverse line order.
6. Grep- display lines that match a pattern.

Directories:-
1. Cd- change to new directory.
2. Mkdir- create new directory.
3. Rmdir-remove empty directory.
4. Mv- change name or directory location.
5. Pwd- show present working directory.



2) Create a directory.
Answer- mkdir
For ex:- mkdir abc

3) Create a subdirectory in the directory created.
Answer- step-1: first create a directory
For ex:- mkdir rootdirectory
Step-2: then enter in rootdirectory
For ex: cd rootdirectory
Step-3: then create another directory
For ex: mkdir subdirectory.

4) Change your current directory to the subdirectory.
Answer -Mv

5) Display the calendar for the current month.
Answer -Cal
For ex: cal 2010

6) Get a directory listing of the parent directory.

Answer -Ls

7) How many users were logged onto your system?

Answer -Who

8) Display your name in the form of a banner.
Answer -Banner Abhi

9) Display the name of device name of your terminal.
Answer -Tty

10) Move to the root directory.

Answer -Cd\


Session 2


11) Change your directory to the directory exercises. Create a file called example1 using the cat command containing the following text:

water, water everywhere
and all the boards did shrink;
water, water everywhere,
No drop to drink.


Answer -Mv mydirectory exercise
Step-1: cat example
Step-2: type the text
Step-3: Press ctrl+D for save

12) Use the man command to obtain further information on the finger command.
Answer -Man finger

13) List all the processes that are presently running.
Answer -Ps

14) List the text files in your current directory.

Answer -Find/ usr –type –name *.txt print

15) Make a copy of any text file.

Answer -Cp

16) Rename one of your text files in the current directory.

Answer -Mv

17) Delete an unneeded copy of a file.
Answer -Rm

18) Print out any file on paper.
Answer -lp

19) Send a message to another user on your UNIX system, and get them to reply.
Answer -Write user

20) Create a small text file and send it to another user.
Answer -Mail

Session 3


21) When you receive a message, save it to a file other than your mailbox.
Answer -We can open all received message by
$mail
Then we save a mail message to a file by
$sx

22) Send a message to a user on a different computer system.
Answer -Mail mca11 ttyp5
Subject: Hello
How are you?
Then press ctrl+D .

23) Try to move to the home directory of someone else in your group. There are several ways to do this, and you may find that you are not permitted to enter certain directories. See what files they have, and what the file permissions are.
Answer -Cd user/mca11

24) Try to copy a file from another user’s directory to your own.

Answer -@pwd
User/mca11
$cd abhishek
$ cp xyz.txt/user2/mca12

25) Set permissions on all of your files and directories to those that you want. You may want to give read permission on some of your files and directories to members of your group.
Answer -$chmod –R 777 abc.txt

26) Create a number of hierarchically related directories and navigate through them using a combination of absolute pathnames (starting with "/") and relative pathnames.
Answer -$mkdir/user/mca11/helpme
$cd user/mca11/helpme

27) Try using wildcards (“*” and possibly “?”).
Answer -$ls *.txt
This command will display the listing of all files having extension .txt.

28) Put a listing of the files in your directory into a file called filelist. (Then delete it!)
Answer -$ls *.txt
$find filename.txt –printr> filename.txt
$vi as.txt
$rm file list

29) Create a text file containing a short story, and then use the spell program to check the spelling of the words in the file.
Answer -$cat>story.txt
Type the text
Press ctrl+D to save.
$spell story.txt

30) Redirect the output of the spell program to a file called errors.
Answer -$spell story.txt>error

Session 4

31) Type the command ls -l and examine the format of the output. Pipe the output of the command ls -l to the word count program wc to obtain a count of the number of files in your directory.
Answer -$ls –l |wc -c

32) Use cut to strip away the reference material and leave just the text field.
Answer -$cat –F 1-3-d

33) Use tr to strip away any tags that are actually in the text (e.g., attached to the words), so that you are left with just the words.
Answer -$tr –d –c setting1.

34) Set a file to be read-only with the chmod (from change mode) command. Interpret the file permissions displayed by the ls -l command.
Answer -$chmod 444
$ls –l

35) Delete one or more directories with the rmdir (from remove directory) command. See what happens if the directory is not empty. Experiment (carefully!) with the rm -r command to delete a directory and its content.
Answer -$rmdir
$rm –r
$rm –r abc

36) Experiment with redirecting command output (e.g., ls -l >file1). Try ">> " instead of " >" with an existing text file as the output.
Answer -$ls –l>> file
This will append the text in existing file.

37) See whether upper-case versions of any of these commands work as well as the lower-case versions.
Answer -$ls –l>file name
Not found.
38) Use the who command to see users logged into the system.
$who
Mca1 ttyp7 mar20 15:14
Mca2 ttyp5 mar19 11:11
Mca2 ttyp6 mar16 11:10
Mca3 ttyp7 mar11 13:11

39) Pipe the output of the who command to the sort command
Answer -$who|sort -4


40) Search for your login name in whofile using the grep command.

Answer -$who >who file
$grep mca22 whofile
Or
$who |grep mca22

Session 5


41) Compare two text files with the diff command.
Answer -$diff file1 file2

42) Count lines, words, and characters in a file with the wc command.
Answer -$wc –c for characters
$wc –l for lines
$wc –w for wordss

43) Display your current environment variables with the following command: set or env.

Answer -$set or $env both are same
$set or $env
Output:-
HOME=/user1/mca22
HUSHLOGIN= FALSE
HZ=100
TFS=
LOG NAME= mca22
MAIL= /user/spool/mail/mca22
MAILCHECK= 600
MF_ADM=odm.cat@unix
Msg-Mail=1
Ms_PROFILE=1
OPTLND=1
PATH=/bin:/usrer1/mca22/bin:
PS1=$
PS2=>
SHELL=/bin/sh
TERM=ansi
TZ=ESTSEDT

44) Concatenate all files in a directory redirected to /dev/null and redirecting standard error to “errorFile”?
Answer -$cat *>>file name
Show error
Cat: input error: is a directory
Total 68
Dr wxh –xh –x2bca22 9-bca siz Dec 30 12:50
$cat file name


45) Display information on yourself or another user with the finger command.

Answer -$finger
Login name tty idle login time Where
Mca1 *P20 15:49 22:11 192.168.22.3
Mca2 *P21 12:22 21:11 192.168.2.3
Mca3 *P23 14:49 12:11 192.168.2.5
Mca4 *P26 11:49 22:11 192.168.2.2

47) Delete all the files in the current directory whose name ends in “.bak”.
Answer -$cat>a.back
Sss
Ctrl+D
$ls –l *.back
$rm *.back

48) Display lines 10 to 14 of any file which contains 25 lines.
Answer -$tail +10 file name|head -5
$head -10 file1| tail -14 file

49) Count how many lines contain the word science in a word file science.txt.
Answer -Cat> science.txt
- - - - - - - - - - - - - -
- - - - - - - - - - - - - - -
- - - - science- - - - - -
predd ctrl+D to save
$grep –c “science” science.txt |wc -l

50) List the statistics of the largest file (and only the largest file) in the current directory.
Answer -$ls –l| grep ‘n’ |sort>m1
$cat –d –f 4|sort m1;tail -l

Session 6


51) Kill any process with the help of the PID and run any process at the background.

Answer -$pwd
$kill 22556
$vi abc.txt
$date &

52) Select a text file and double space the lines.
Answer -$cat>a11.txt
Abc efg hig
Ctrl+d to save.
$ps -d>plog1.txt [second file]
Output:
Abc efg hig

53) List all the users from /etc/passwd in the alphabetically sorted order.
Answer -$cat /etc/password|sort

54) Create a file with duplicate records and delete duplicate records for that file.
Answer -$cat b11.txt
Abhishek abhishek abhishek
Ctrl+D to save.
$uniq –c b11.txt
Abhishek
$uniq abc.txt>ab1.txt
$Cp ab.txt abc.txt
$cat abc.txt

55) Use the grep command to search the file example1 for occurrences of the string “water”.
Answer -$cat>prog.sh
For I in cat example1
Do
Echo $i>>ex1
Done
Ctrl+D
$sh prog.sh
$cat ex1|grep –c “water”
$grep water example1

56) Write grep commands to do the following activities:

• To select the lines from a file that have exactly two characters.
$grep ‘^.$’ n1
• To select the lines from a file that start with the upper case letter.
$grep ‘[A-Z] ^1
• To select the lines from a file that end with a period.
$grep $n1
• To select the lines in a file that has one or more blank spaces.
$grep ‘ ‘n1
• To select the lines in a file and direct them to another file which has digits
as one of the characters in that line.

$grep ‘[0-9]’ n1>n2

57) Make a sorted wordlist from the file.
Answer -$tr ‘A-Z’ ‘a-z’ < file1|tr –cs ‘a-z’ ‘\02’|sort |uniq –c>file2

58) Try to execute the example shell scripts given in this manual.
Answer -$vi nv
Echo “Enter any no.”
Read a
Echo “Number is 4a”


59) Write a shell script that searches for a single word pattern recursively in the current directory and displays the no. of times it occurred.
Answer -$grep –r ‘ABC’


Session 7

61) Write a shell script that accepts a string from the terminal and echo a suitable message if it doesn’t have at least 5 characters including the other symbols.
Answer –
$vi abc
Echo “Enter the string”
Read s
C=`expr $s |wc –c`
C=`expr $c -1`
If Test $c –ge 5
Then
Echo “String is valid”
Else
Echo “String is invalid”
Fi
:wq

62) Write a shell script to echo the string length of the given string as argument.
Answer –
Vi xyz
Echo “enter the String”
Read a
C=’echo $a |wc –c’
C=`expr $c -1`
Echo $c
:wq

63) Write a shell script that accepts two directory names as arguments and deletes those files in the first directory which are similarly named in the second directly. Note: Contents should also match inside the files.
Answer –
Vi abc
Echo “enter the fist Dir”
Read a
Echo “Enter the second Dir”
Read b
Ls $a >> aa
Ls $b >> bb
For ‘in comm. -1-2 aabb’
Do
Q=’emp –s/usrer/bca/hour/s1/user1/bca1/abhi/$i’
Echo $q
If Test $q –eq 0
Sun/user1/bca1/hour/$ls
Sun/user1/bca1/abhi/&i
Else
fi
Done
:wq

64) Write a shell script to display the processes running on the system for every 30 seconds, but only for 3 times.
Answer –
Vi process
I=”0”
While Test $i –le 3
Do sleep 5
Ps
i=`expr $i +1`
done

65) Write a shell script that displays the last modification time of any file.

Ls –l put|cat –c -45 -48

66) Write a shell script to check the spellings of any text document given as an argument.
Echo “enter the file”
Read a
Spell $a> error
Cut error

69) Write a shell script which reads the contents in a text file and removes all the blank spaces in them and redirects the output to a file.
Answer –
W=`wc –w Test`
Echo “No of word $w”
C=1
Set ‘cat jay’
While test $c –le $w
Do
Echo –n $i>>jay
C=`expr $c +1`
Done
Cut jay

Or

Vi xyz
For I in ‘cat jay’
Do
Echo –n 4i
done

70) Write a shell script that changes the name of the files passed as arguments to lowercase.

Echo $i>temp
Tr “[:uppr:]” “[:lowe:]” [temp]
Mv /user/mca2/$i/user/mca2/$a
Session 8

71) Write a shell script to translate all the characters to lower case in a given text file.
Answer –
Echo enter a text file
Read file
If [1 $file]
Then
Echo 4file not a file
Exit
Fi
Cat $file |tr ‘[A-Z]’ ‘[a-z]’

72) Write a shell script to combine any three text files into a single file (append them in the order as they appear in the arguments) and display the word count.
Answer –
# //bin/bash
File1=$1
File2=$2
File3=$3 out- ‘output $1’
Count=0
If [$# -ne 3]
Then
Echo “$(base name $o) file1 file 2file3”
Exit1
Fi
If [!=$file]
Then
Echo ‘$file! Not a file’
Exit2
Fi
If[! –f $file2]
Then
Echo “$file2 not file!”
Exit 3
Fi
If [1- f $file]
Then
Echo $file3 not a file!”
Exit2
Fi
$file1 $file 2$file3?? $out
Count=4(cat $out|wc –w)
Echo “count words written to out!”

18 comments:

  1. thanks for solutions

    ReplyDelete
  2. Thanks. Can u give the solution of Unix session-8,9,10

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. Hi there i am kavin, its my first time to commenting anyplace, when i read this paragraph i thought i could
    also make comment due to this sensible paragraph.

    My blog - http://www.moodlesocial.com/blog/index.php?postid=159763

    ReplyDelete
  5. Hey! I know this is kinda off topic nevertheless I'd figured I'd ask.
    Would you be interested in trading links or maybe guest authoring a blog post or vice-versa?
    My site covers a lot of the same subjects as yours
    and I believe we could greatly benefit from each other.

    If you are interested feel free to send me an email. I look forward to
    hearing from you! Great blog by the way!

    My webpage ... Buy Adorna

    ReplyDelete
  6. I've been browsing online more than three hours nowadays, but I by no means found any attention-grabbing article like yours. It is beautiful worth sufficient for me. In my opinion, if all site owners and bloggers made just right content material as you probably did, the web shall be a lot more useful than ever before.

    Stop by my web site; enduros

    ReplyDelete
  7. Undeniably consider that that you stated. Your favourite reason seemed to be
    at the net the easiest factor to consider of. I say to you, I definitely get annoyed at the same time as other
    people consider concerns that they just don't know about. You controlled to hit the nail upon the top and also defined out the entire thing with no need side-effects , other folks could take a signal. Will likely be back to get more. Thanks

    Stop by my weblog; WEight Loss Products

    ReplyDelete
  8. These are genuinely enormous ideas in on the topic of blogging.

    You have touched some good points here. Any way keep up wrinting.


    My blog - working online from home

    ReplyDelete
  9. You really make it seem so easy together with your presentation
    however I find this matter to be actually something which I think I'd never understand. It kind of feels too complex and very broad for me. I am taking a look forward on your subsequent put up, I will try to get the grasp of it!

    Take a look at my web page :: Muscle BUilding Supplement

    ReplyDelete
  10. hello there and thank you for your info – I've certainly picked up something new from right here. I did however expertise several technical points using this web site, as I experienced to reload the site many times previous to I could get it to load correctly. I had been wondering if your hosting is OK? Not that I am complaining, but slow loading instances times will sometimes affect your placement in google and can damage your high quality score if ads and marketing with Adwords. Anyway I am adding this RSS to my email and can look out for much more of your respective fascinating content. Ensure that you update this again very soon.

    My weblog; Buy revolyn

    ReplyDelete
  11. This is a very good tip particularly to those new to the blogosphere.
    Brief but very precise information… Many thanks for sharing this one.
    A must read post!

    My web blog - raspberry ketones

    ReplyDelete
  12. thankssssssssssssss

    ReplyDelete