You are on page 1of 66

Kirti College

T.Y.B.Sc.

Linux

Practical No 3
Part-I:File System Commands:touch,cat,cp,rm,mv,mkdir,cd,rmdir
Q:1) Create five files with the name a1,b2,c3,d4,e5 by using touch
command.
[user43@linserver user43]$ touch a1 b2 c3 d4 e5
Q:2) Create five files with the name f6,g7,h8 using cat command with
some meaningful contents with a
[user43@linserver user43]$ cat> f6
This is file f6
this is cat command.
hello.
hi.
tybsc.
[user43@linserver user43]$ cat> g7
thid is file g7
hello.
hi.
tybsc.
this is cat cmd.
[user43@linserver user43]$ cat> h8
this is file h8
hello.
hi.
tybsc.
this is cat cmd.
Q:3) Concatenate the contents of f6 and g7 to a file called new1.
(Without creating new1)
[user43@linserver user43]$ cat f6 g7 >>new1
[user43@linserver user43]$ cat new1
This is file f6
this is cat command.
hello.
hi.
tybsc.
thid is file g7
hello.
hi.
tybsc.
this is cat cmd.
Q:4) Append the contents of f6 and h8 to new1.
[user43@linserver user43]$ cat f6 h8 >> new1
[user43@linserver user43]$ 9]$ cat new1
This is file f6
this is cat command.

Department of Computer Science

Kirti College
hello.
hi.
tybsc.
thid is
hello.
hi.
tybsc.
this is
This is
this is
hello.
hi.
tybsc.
this is
hello.
hi.
tybsc.
this is

T.Y.B.Sc.

Linux

file g7

cat cmd.
file f6
cat command.

file h8

cat cmd.

Q:5) Copy contents of f6 to a1, g7 to b2, h8 to c3 by using cp


command.
[user43@linserver user43]$ cp f6 a1
[user43@linserver user43]$ cat a1
This is file f6
this is cat command.
hello.
hi.
tybsc.
[user43@linserver user43]$ cp g7 b2
[user43@linserver user43]$ cat b2
thid is file g7
hello.
hi.
tybsc.
this is cat cmd.
[user43@linserver user43]$ cp h8 c3
[user43@linserver user43]$ cat c3
this is file h8
hello.
hi.
tybsc.
this is cat cmd.
Q:6) Create two directions with the name dd1 and dd2.
[user43@linserver user43]$ mkdir dd1
[user43@linserver user43]$ mkdir dd2

Department of Computer Science

Kirti College

T.Y.B.Sc.

Linux

Q:7) Copy the files a1 and b2 to the directory dd1 by using cp


command. Copy the files f6, g7 to the directory dd2.
[user43@linserver user43]$ cp a1 b2 dd1
[user43@linserver user43]$ ls dd1
a1 b2
[user43@linserver user43]$ cp f6 g7 dd2
[user43@linserver user43]$ ls dd2
a1 b2
Q:8) Remove the files a1, b2 from dd1.
[user43@linserver user43]$ rm dd1/a1
[user43@linserver user43]$ ls dd1
b2
[user43@linserver user43]$ rm dd1/b2
[user43@linserver user43]$ ls dd1
Q:9) Remove the directory dd2 along with its contents.
[user43@linserver user43]$ rm -r dd2
Q:10) Rename the files f6,
command.
[user43@linserver user43]$
[user43@linserver user43]$
[user43@linserver user43]$

g7, h8, by newf6, newg7, newh8, using mv


mv f6 newf6
mv g7 newg7
mv h8 newh8

Q:11) Move the files newf6, newg7 to the directory dd1.


[user43@linserver user43]$ mv newf6 newg7 dd1
[user43@linserver user43]$ ls dd1
newf6 newg7
Part II: ls and ls with options
Change the directory to /bin and do the
commands in your journal.
(i) List all filenames.
[user43@linserver user43]$ cd /bin
[user43@linserver user43]$ ls
arch
cut
gawk
tcsh
ash
date
gettext
touch
ash.static
dd
grep
true
aumix-minimal df
gtar
umount
awk
dmesg
gunzip
uname

Department of Computer Science

following and enter the

ls

red

mail

rm

mkdir

rmdir

mknod

rpm

mktemp

rvi

Kirti College
basename
unicode_start
bash
unicode_stop
bash2
unlink
bsh
usleep
cat
chgrp
view
chmod
ypdomainname
chown
zcat
cp
cpio
csh

T.Y.B.Sc.

Linux

dnsdomainname

gzip

more

rview

doexec

hostname

mount

sed

domainname

igawk

mt

setfont

dumpkeys

ipcalc

mv

setserial

echo
ed

jpeg-6b
kbd_mode

netstat
nice

sh
sleep

egrep

kill

nisdomainname

sort

env

link

pgawk

stty

ex
false
fgrep

ln
loadkeys
login

ping
ps
pwd

su
sync
tar

vi

(ii) List all filenames with one screen at a time.


[user43@linserver bin]$ ls |more
arch
ash
ash.static
aumix-minimal
awk
basename
bash
bash2
bsh
cat
chgrp
chmod
chown
cp
cpio
csh
cut
date
dd
df
dmesg
dnsdomainname
doexec
--More
(iii) List all filenames with 2 characters, 3 characters.
[user43@linserver bin]$ ls ??
cp dd df ed ex ln ls mt mv ps rm sh su vi

Department of Computer Science

Kirti College

T.Y.B.Sc.

[user43@linserver bin]$ ls ???


ash awk bsh cat csh cut env
(iv) List all filenames
same time.
[user43@linserver bin]$
arch cpio df
ex
bash date echo gawk
cp
dd
ed
grep

pwd

Linux

red

rpm

rvi

sed

tar

with 2 characters and 4 characters at the


ls ??
gtar
gzip
kill

????
link
ln
ls

mail
more
mt

mv
nice
ping

(v) List all filenames starting with vowel


[user43@linserver bin]$ ls [aeiou]*
arch ash.static
awk
ed
env igawk
unlink
ash
aumix-minimal echo egrep ex
ipcalc
usleep

ps
rm
sh

sort
stty
su

sync
tcsh
true

vi
view
zcat

umount

unicode_start

uname

unicode_stop

(vi) List all filenames with the last character as a or b or c or d.


[user43@linserver user43]$ ls *[abcd]
fspaa fspac
newsed
prime.c
xaa xac
fspab masterdata prime1.c transdata xab
(vii) List all filenames with exactly three characters in which the
second character is a vowel.
[user43@linserver bin]$ ls ?[aeiou]?
cat cut red sed tar
(viii) List all filenames starting with character a.
[user43@linserver bin]$ ls [a]*
arch ash ash.static aumix-minimal awk
(ix) List all 4 character filenames whose first character is a and
the third character is b.
[user43@linserver bin]$ ls [a]?[b]?
ls: [a]?[b]?: No such file or directory
(x) List all filenames whose first character is m or r or is in
the range c to f or v to z.
[user43tybsc289@linserver bin]$ ls [mrc-fv-z]*
cat
cpio df
dumpkeys ex
mknod
mv
rvi
zcat
chgrp csh
dmesg
echo
false mktemp red
rview
chmod cut
dnsdomainname ed
fgrep more
rm
vi
chown date doexec
egrep
mail
mount
rmdir view
cp
dd
domainname
env
mkdir mt
rpm
ypdomainname
(xi) List all filenames whose first character is any thing other
than an alphabet in the range d to m.
[user43@linserver bin]$ ls [!d-m]*

Department of Computer Science

Kirti College
arch
unicode_start
ash
unicode_stop
ash.static
aumix-minimal
awk
basename
bash
ypdomainname
bash2
bsh

T.Y.B.Sc.

Linux

cat

nice

rpm

stty

chgrp

nisdomainname

rvi

su

chmod
chown
cp
cpio
csh

pgawk
ping
ps
pwd
red

rview
sed
setfont
setserial
sh

sync
tar
tcsh
touch
true

unlink
usleep
vi
view

cut
netstat

rm
rmdir

sleep
sort

umount
uname

zcat

(xii) Construct a command to display the total number of files with


exactly three characters in their filename.
[user43@linserver bin]$ ls ???|wc -l
13
(xiii) Construct a command to display the total number of files with
exactly two or three or four characters in their filename.
[user43@linserver bin]$ ls ?? ??? ????|wc -l
49
Change the directory to the home directory and do the following
(xiv) List the contents of the directory.
[user43@linserver user43]$ ls dir1
f1 file1
(xv) List the contents of the directory along with all hidden files.
[user43@linserver user43]$ ls -a dir1
. .. f1 file1

Department of Computer Science

Kirti College

T.Y.B.Sc.

Linux

(xvi) List the contents of the directory along with all hidden files
except . and .. files.
[user43@linserver user43]$ ls -A
+1
copy1
fcmp1_289
green
pract7.10
pract7.7
prime1.out
Student
date
fcmp2
.gtkrc
pract7.11
pract7.9
prime.c
Student289
ashwini
date1
fcmp2_289 .kde
pract7.12
pract8.1
.prime.c.swo t4
.bash_history dir1
fcmp3_289
line
pract7.13
pract8.11.2
prime.out
transdata
.bash_logout dir2
fex1
login
pract7.15
pract8.12.3 product_289
try2
.bash_profile dir3
.file
loop
pract7.17
pract8.14
result
tryTime
.bashrc
doll
file2
masterdata pract7.18
pract8.3
result1
.viminfo
bdata289
.emacs
fod1
merit289
pract7.3
pract8.5
Shweta289 yellow
check
emp
foreg
month
pract7.3.1
pract8.6
ss1
checkgrade
empdata289
fsp289
names
pract7.4
pract8.7
ss2
commandfor
fact
gre1
pract
pract7.5
pract8.8
ss3
commandline fcmp1
(xvii) List all files with their attributes and file permissions.
[user43@linserver user43]$ ls -l
total 204
-rw-r--r-1 tybsc289 tybsc
159 Dec 7 10:52 +1
-rw-r--r-1 tybsc289 tybsc
16 Jan 18 09:57 ashwini
-rw-r--r-1 tybsc289 tybsc
193 Jan 18 08:39 check
-rw-r--r-1 tybsc289 tybsc
307 Jan 10 15:51 Stud289
...
...
-rw-r--r-1 tybsc289 tybsc
139 Jan 19 11:53 Student
-rw-r--r-1 tybsc289 tybsc
881 Jan 4 08:58 t1
-rw-rw-r-1 tybsc289 546
881 Dec 14 09:52 try1
-rw-rw-r-1 tybsc289 546
28 Dec 30 10:15 white
-rw-rw-r-1 tybsc289 546
36 Dec 14 09:09 yellow
(xviii) Same as above
(xix) List all the files showing the size of each file rounded up to
the nearest kilobyte.
[user43@linserver user43]$ ls -s
total 204
4 +1
4 dir3
4 login
4 pract7.17
4
ss1
4 ashwini
4 doll
4 loop
4 pract7.18
4
ss2
4 check
4 emp
4 names
4 pract7.3
4
ss3

Department of Computer Science

Kirti College
4 checkgrade
Stud289
4 commandfor
Student
4 date
white
...

T.Y.B.Sc.

Linux

4 fact

4 pink

4 pract7.3.1

4 file2

4 pract

4 pract7.4

4 gre1

4 pract7.11

0 pract7.7

(xx) List all the files according to


[user43@linserver user43]$ ls -S
prime.out
prime.c
month
bdata244
fcmp1_244
pract7.18
pract7.11
+1
loop
pract7.3
dir1
pract7.13
Student244 transdata
fod1
dir2
pract8.5
pract7.10
doll
date1
dir3
Student
line
result
result1
tryTime
pract7.9
pract8.1
yellow
fsp244
Shweta244
masterdata
emp
login
pract8.6
pract7.15
fcmp2_244
commandfor t4
pract7.6
pract7.5
fcmp2
copy1
gre1
foreg
Stud244
check
pract
ashwini

file size.

(xxi) Give the file listing displayed


[user43@linserver user43]$ ls -c
gre2
date1
pract7.18
white
gre1
commandfor
fsp244
green
+1
pract7.13
pract7.7
Stud244
Student
pract7.15 pract7.12
login
dir1
pract7.11
copy1
commandline try1
pract7.4
pract7.3.1
file2
pract7.9
pract7.5
pract7.3

fact
fcmp3_244
prime1.out pract8.14
merit289
commandline
pract7.12
pract8.12.3
10000
pract8.8
pract7.17
file2
prime1.c
checkgrade
pract7.1
pract7.7
pract7.4
ss3
pract7.3.1 date
green
pract8.7
names
pract8.11.2 empdata244
ss2
try2
gre2
product_244 pract8.3
fex1
fcmp1
ss1
in columns.
check
ashwini
pract8.3
ss2
doll
pract7.17
pract8.1
yellow
pink

ss1
emp
foreg
dir2
names
fact
pract
date
line

pract7.1
checkgrade
ss3
dir3
t1
pract7.6
pract7.10
loop

(xxii) Give the file listing in reverse order.


[user43@linserver user43]$ ls -r
yellow
ss2
pract7.5
pract7.13
names
fsp289
dir2
white
checkgrade
ss1
pract7.4
pract7.12
loop
foreg
dir1
check
try1
pract8.3
pract7.3.1 login
file2
date1
ashwini
t1
pract8.1
line
pract7.3
pract7.10
fact
date
+1
Student pract7.9
pract7.18
pract7.1
green
emp
copy1
Stud289
pract7.7
pract7.17
pract
gre2
doll
commandline ss3
pract7.6
pract7.15

Department of Computer Science

Kirti College

T.Y.B.Sc.

Linux

pink
gre1
dir3
commandfor
(xxiii) Give the file listing where all the files are in long format
showing full size details.
[user43@linserver user43]$ ls -l
total 204
-rw-r--r-1 tybsc244 tybsc
159 Dec 7 10:52 +1
-rw-r--r-1 tybsc244 tybsc
16 Jan 18 09:57 ashwini
-rw-r--r-1 tybsc244 tybsc
193 Jan 18 08:39 check
-rw-r--r-1 tybsc244 tybsc
243 Jan 18 08:38 checkgrade
-rw-r--r-1 tybsc244 tybsc
31 Jan 18 11:26 commandfor
-rw-r--r-1 tybsc244 tybsc
56 Jan 4 08:57 commandline
-rw-r--r-1 tybsc244 tybsc
212 Jan 7 11:56 copy1
-rw-r--r-1 tybsc244 tybsc
220 Jan 18 11:48 date
-rw-r--r-1 tybsc244 tybsc
0 Jan 18 11:32 date1
drwxrwxr-x
2 tybsc244 546
4096 Nov 30 09:44 dir1
drwxrwxr-x
3 tybsc244 546
4096 Dec 14 10:08 dir2
drwxrwxrwx
2 tybsc244 546
4096 Dec 7 09:07 dir3
-r-xr-xr-x
1 tybsc244 tybsc
47 Jan 4 08:24 doll
. . .
-rw-r--r-1 tybsc244tybsc
41 Jan 4 10:18 pract7.1
-rw-r--r-1 tybsc244 tybsc
260 Jan 18 09:10 pract7.10
-rw-r--r-1 tybsc244 tybsc
299 Jan 18 09:24 pract7.11
-rw-r--r-1 tybsc244 tybsc
286 Jan 18 09:33 pract7.12
-rw-r--r-1 tybsc244 tybsc
419 Jan 18 09:56 pract7.13
-rw-r--r-1 tybsc244 tybsc
220 Jan 18 11:03 pract7.15
-rw-r--r-1 tybsc244 tybsc
115 Jan 18 10:49 pract7.17
-rw-r--r-1 tybsc244 tybsc
3 Jan 18 10:26 pract7.18
-rw-r--r-1 tybsc244 tybsc
1 Jan 4 10:32 pract7.3
-rw-r--r-1 tybsc244 tybsc
360 Jan 4 10:49 pract7.3.1
-rw-r--r-1 tybsc244 tybsc
138 Jan 18 08:10 pract7.4
-rw-r--r-1 tybsc244 tybsc
103 Jan 18 08:02 pract7.5
-rw-r--r-1 tybsc244 tybsc
128 Jan 18 08:12 pract7.6
-rw-r--r-1 tybsc244 tybsc
0 Jan 18 08:33 pract7.7
-rw-r--r-1 tybsc244 tybsc
221 Jan 18 08:43 pract7.9
-rw-r--r-1 tybsc244 tybsc
79 Jan 19 11:18 pract8.1
-rw-rw-r-1 tybsc244 546
28 Dec 30 10:15 white
-rw-rw-r-1 tybsc244 546
36 Dec 14 09:09 yellow

Department of Computer Science

Kirti College

T.Y.B.Sc.

Linux

Practical No 4
Study of commands:find, tr, head, tail, wc, file, sort, split
1) Display using find command all the filenames under:
(i) /usr/sbin one screen at a time.
[user43@linserver user43]$ find /usr/sbin | more
/usr/sbin
/usr/sbin/iconvconfig
/usr/sbin/rpcinfo
/usr/sbin/build-locale-archive
/usr/sbin/zdump
/usr/sbin/zic
/usr/sbin/pwunconv
/usr/sbin/pwck
/usr/sbin/glibc_post_upgrade
/usr/sbin/alternatives
/usr/sbin/update-alternatives
/usr/sbin/mklost+found
/usr/sbin/arping
/usr/sbin/clockdiff
/usr/sbin/ping6
/usr/sbin/rdisc
/usr/sbin/tracepath
/usr/sbin/tracepath6
/usr/sbin/traceroute6
/usr/sbin/adduser
/usr/sbin/chpasswd
/usr/sbin/groupadd
/usr/sbin/groupdel
--More
(ii) /usr/sbin beginning with a lowercase c.
[user43@linserver user43]$ find /usr/sbin/c*
/usr/sbin/camel-index-control
/usr/sbin/camel-lock-helper
/usr/sbin/capiinit
/usr/sbin/chat
/usr/sbin/chkfontpath
/usr/sbin/chpasswd
/usr/sbin/chroot
/usr/sbin/ciped-cb
/usr/sbin/clockdiff
/usr/sbin/crond
/usr/sbin/cupsaddsmb
/usr/sbin/cupsd

Department of Computer Science

10

Kirti College

T.Y.B.Sc.

Linux

(iii) /usr/sbin in capital letters beginning with a lowercase c.


(Use single-quote for tr command)
[user43@linserver user43]$ find /usr/sbin/c* | tr '[a-z]'
'[A-Z]'
/USR/SBIN/CAMEL-INDEX-CONTROL
/USR/SBIN/CAMEL-LOCK-HELPER
/USR/SBIN/CAPIINIT
/USR/SBIN/CHAT
/USR/SBIN/CHKFONTPATH
/USR/SBIN/CHPASSWD
/USR/SBIN/CHROOT
/USR/SBIN/CIPED-CB
/USR/SBIN/CLOCKDIFF
/USR/SBIN/CROND
/USR/SBIN/CUPSADDSMB
/USR/SBIN/CUPSD
(iv) /usr/sbin which are over 5k in size in uppercase.
[user43@linserver user43]$ find /usr/sbin -size 5k | tr '[a-z]'
'[A-Z]'
/USR/SBIN/MODELINE2FB
/USR/SBIN/RPC.NFSD
/USR/SBIN/EXECCAP
/USR/SBIN/SETPCAPS
2) Display parts of files using head or tail command:
(i) Display and count all the lines in the file/etc/mime.types
[user43@linserver user43]$ wc -l /etc/mime.types
480 /etc/mime.types
(ii) Display the first 10 lines of the file/etc/mime.types
[user43@linserver user43]$ head -10 /etc/mime.types
# This is a comment. I love comments.
# This file controls what Internet media types are sent to the
client for
# given file extension(s). Sending the correct media type to the
client
# is important so they know how to handle the content of the file.
# Extra types can either be added here or by using an AddType
directive
# in your config files. For more information about Internet media
types,
# please read RFC 2043, 2046, 2047, 2048, and 2077. The Internet
media type
# registry is at <http://www.iana.org/assignments/media-types/>.

Department of Computer Science

11

Kirti College

T.Y.B.Sc.

Linux

(iii) Display the last 10 lines of the /etc/mime.types


[user43@linserver user43]$ tail -10 /etc/mime.types
video/vnd.fvt
video/vnd.motorola.video
video/vnd.motorola.videop
video/vnd.mpegurl
mxu
video/vnd.mts
video/vnd.nokia.interleaved-multimedia
video/vnd.vivo
video/x-msvideo
avi
video/x-sgi-movie
movie
x-conference/x-cooltalk
ice
(iv) Display the first 25 lines of the /etc/mime.types
[user43@linserver user43]$ head -25 /etc/mime.types
# This is a comment. I love comments.
# This file controls what Internet media types are sent to the
client for
# given file extension(s). Sending the correct media type to the
client
# is important so they know how to handle the content of the file.
# Extra types can either be added here or by using an AddType
directive
# in your config files. For more information about Internet media
types,
# please read RFC 2043, 2046, 2047, 2048, and 2077. The Internet
media type
# registry is at <http://www.iana.org/assignments/media-types/>.
# MIME type
application/EDI-Consent
application/EDI-X12
application/EDIFACT
application/activemessage
application/andrew-inset
application/applefile
application/atomicmail
application/batch-SMTP
application/beep+xml
application/cals-1840
application/commonground
application/cybercash
application/dca-rft
application/dec-dx

Department of Computer Science

Extension

ez

12

Kirti College

T.Y.B.Sc.

Linux

3)Classify,Count and Compare Files


a) Find out what file types you have in the following directories:
(i) /etc
[user43@linserver user43]$ file /etc/*
/etc/a2ps.cfg:
ASCII English text
/etc/a2ps-site.cfg:
ASCII English text
/etc/adjtime:
ASCII text
/etc/aep:
directory
/etc/aep.conf:
ASCII text
/etc/aeplog.conf:
ASCII text
/etc/alchemist:
directory
/etc/aliases:
ASCII English text
/etc/aliases.db:
can't read `/etc/aliases.db' (Permission
denied).
/etc/alternatives:
directory
/etc/anacrontab:
ASCII text
/etc/at.deny:
can't read `/etc/at.deny' (Permission
denied).
/etc/auto.master:
ASCII English text
/etc/auto.misc:
ASCII English text
/etc/bashrc:
ASCII text
/etc/bonobo-activation:
directory
/etc/cdrecord.conf:
ASCII English text
/etc/cipe:
directory
/etc/CORBA:
directory
/etc/cron.d:
directory
/etc/cron.daily:
directory
/etc/cron.hourly:
directory
/etc/cron.monthly:
directory
/etc/crontab:
ASCII text
/etc/cron.weekly:
directory
/etc/csh.cshrc:
ASCII text
/etc/csh.login:
ASCII text
/etc/cups:
directory
/etc/default:
directory
/etc/DIR_COLORS:
ASCII English text
/etc/DIR_COLORS.xterm:
ASCII English text
..
..
(ii) /bin
[user43@linserver user43]$ file /usr/bin/*
/usr/bin/zip:
ELF 32-bit LSB
executable, Int
el 80386, version 1 (SYSV), for GNU/Linux 2.2.5, dynamically linked
(uses shared
libs), stripped
/usr/bin/zipcloak:
ELF 32-bit LSB
executable, Int

Department of Computer Science

13

Kirti College
el 80386, version 1
(uses shared
libs), stripped
/usr/bin/zipgrep:
script text execu
table
/usr/bin/zipinfo:
executable, Int
el 80386, version 1
(uses shared
libs), stripped
/usr/bin/zipnote:
executable, Int
el 80386, version 1
(uses shared
libs), stripped
/usr/bin/zipsplit:
executable, Int
el 80386, version 1
(uses shared
libs), stripped
/usr/bin/zless:
script text execu
table
/usr/bin/zmore:
script text execu
table
..

T.Y.B.Sc.

Linux

(SYSV), for GNU/Linux 2.2.5, dynamically linked


Bourne shell
ELF 32-bit LSB
(SYSV), for GNU/Linux 2.2.5, dynamically linked
ELF 32-bit LSB
(SYSV), for GNU/Linux 2.2.5, dynamically linked
ELF 32-bit LSB
(SYSV), for GNU/Linux 2.2.5, dynamically linked
Bourne shell
Bourne shell

b)Repeat the previous question, but this time:


(i) Re-direct /etc listing to new file etcfiles.txt
[user43@linserver user43]$ file /etc/* > etcfiles.txt
[user43@linserver user43]$ cat etcfiles.txt | more
/etc/a2ps.cfg:
ASCII English text
/etc/a2ps-site.cfg:
ASCII English text
/etc/adjtime:
ASCII text
/etc/aep:
directory
/etc/aep.conf:
ASCII text
/etc/aeplog.conf:
ASCII text
/etc/alchemist:
directory
/etc/aliases:
ASCII English text
/etc/aliases.db:
can't read `/etc/aliases.db' (Permission
denied).
/etc/alternatives:
directory
/etc/anacrontab:
ASCII text
/etc/at.deny:
can't read `/etc/at.deny' (Permission
denied).
/etc/auto.master:
ASCII English text
/etc/auto.misc:
ASCII English text
/etc/bashrc:
ASCII text
/etc/bonobo-activation:
directory

Department of Computer Science

14

Kirti College
/etc/cdrecord.conf:
/etc/cipe:
/etc/CORBA:
/etc/cron.d:
/etc/cron.daily:
/etc/cron.hourly:
/etc/cron.monthly:

T.Y.B.Sc.

Linux

ASCII English text


directory
directory
directory
directory
directory
directory

(ii) Append the listing for /usr/bin to etcfiles.txt


[user43@linserver user43]$ file /usr/bin/* >> etcfiles.txt
c) Construct a command to find out how many files are in the
/usr/bin directory.
[user43@linserver user43]$ file /usr/bin/* | wc l
2161
4) Sorting
a) Sort the etcfiles.txt into reverse alphabrtical order on the
first file.
[user43@linserver user43]$ sort -r etcfiles.txt | more
/usr/bin/zsoelim:
symbolic link to
soelim
/usr/bin/zsoelim:
symbolic link to
soelim
/usr/bin/znew:
Bourne shell
ELF 32-bit LSB executable, Int
el 80386, version 1 (SYSV), for GNU/Linux 2.2.5, dynamically linked
(uses shared
libs), stripped
/usr/bin/zipnote:
ELF 32-bit LSB
executable, Int
el 80386, version 1 (SYSV), for GNU/Linux 2.2.5, dynamically linked
(uses shared
libs), stripped
b) Repeat the first sorting exercise but ignoring case differences.
[user43@linserver user43]$ sort -f -r etcfiles.txt | more
/usr/bin/zsoelim:
symbolic link to
soelim
/usr/bin/zsoelim:
symbolic link to
soelim
/usr/bin/znew:
Bourne shell
script text execu
table
/usr/bin/znew:
Bourne shell
script text execu
table
/usr/bin/zmore:
Bourne shell
script text execu
table

Department of Computer Science

15

Kirti College

T.Y.B.Sc.

Linux

/usr/bin/zmore:
Bourne shell
script text execu
table
/usr/bin/zless:
Bourne shell
script text execu
table
/usr/bin/zless:
Bourne shell
script text execu
table
/usr/bin/zipsplit:
ELF 32-bit LSB
executable, Int
el 80386, version 1 (SYSV), for GNU/Linux 2.2.5, dynamically linked
(uses shared
libs), stripped
/usr/bin/zipsplit:
ELF 32-bit LSB
executable, Int
el 80386, version 1 (SYSV), for GNU/Linux 2.2.5, dynamically linked
(uses shared
libs), stripped
/usr/bin/zipnote:
ELF 32-bit LSB
executable, Int
el 80386, version 1 (SYSV), for GNU/Linux 2.2.5, dynamically linked
(uses shared
libs), stripped
c) Sort the etcfiles.txt into alphabetical order on the second field
[user43@linserver user43]$ sort +1 etcfiles.txt | more
/usr/bin/evolution-move-tasks:
a perl script text
executable
/usr/bin/evolution-move-tasks:
a perl script text
executable
/usr/bin/libglade-xgettext:
a python script
text executabl
e
/usr/bin/libglade-xgettext:
a python script
text executabl
e
/etc/rpc:
ASCII C program text
/etc/mime-magic:
ASCII C++ program text
/etc/ltrace.conf:
ASCII C program text
/usr/bin/e2pall:
ASCII English text
/usr/bin/e2pall:
ASCII English text
/usr/bin/xsubpp:
ASCII English text
/usr/bin/xsubpp:
ASCII English text
/usr/bin/epstopdf:
ASCII English text
/usr/bin/epstopdf:
ASCII English text
/usr/bin/thumbpdf:
ASCII English text
/usr/bin/thumbpdf:
ASCII English text
/etc/imrc:
ASCII English text
/etc/fdprm:
ASCII English text
/etc/hosts:
ASCII English text

Department of Computer Science

16

Kirti College

T.Y.B.Sc.

Linux

d) Find out how many English text files are listed in the
etcfiles.txt file
[user43@linserver user43]$ grep English etcfiles.txt | wc -l
67
5) Create a file using vi editor with the following contents.
[user43@linserver user43]$ cat names
Mahesh Deshpande 234
Naresh Nair 431
Allen Disuza 121
Hari Kutian 231
Ramesh Dubey 231
Akshay Das 256
(i) Sort on the first names only
[user43@linserver user43]$ sort +0 -1 names
Akshay Das 25
Allen Disuza 121
Hari Kutian 231
Mahesh Deshpande 234
Naresh Nair 431
Ramesh Dubey 231
(ii) Sort on the last names only
[user43@linserver user43]$ sort +1 -2 names
Akshay Das 256
Mahesh Deshpande 234
Allen Disuza 121
Ramesh Dubey 231
Hari Kutian 231
Naresh Nair 431
(iii) Sort on first four characters only
[user43@linserver user43]$ cut -c 1-4 names | sort
Aksh
Alle
Hari
Mahe
Nare
Rame
(iv) Sort on their numbers only
[user43@linserver user43]$ sort +2 names
Allen Disuza 121
Hari Kutian 231
Ramesh Dubey 231
Mahesh Deshpande 234
Akshay Das 256
Naresh Nair 431

Department of Computer Science

17

Kirti College

T.Y.B.Sc.

Linux

6)Construct and execute the commands to create a file with the name
Stud<roll_no> with the following fields separated by a blank space.
(i)
[user43@linserver user43]$ cat Stud244
RollNo FirstName
LastName
DateOfBirth
Marks
289
Shweta
Choudhary
7-12-1985
430
291
Mildred
D'mello
9-12-1985
500
289
Megha
Menon
14-10-1985
550
298
Vikrant
Mahkal
12-5-1985
500
300
Sankalp
Naik
11-5-1985
550
(ii) Sort the data on first names only
[user43@linserver user43]$ sort +1 -2 Stud244
RollNo FirstName
LastName
DateOfBirth
289
Megha
Menon
14-10-1985
291
Mildred
D'mello
9-12-1985
300
Sankalp
Naik
11-5-1985
289
Shweta
Choudhary
7-12-1985
298
Vikrant
Mahkal
12-5-1985

Marks
550
500
550
430
500

(iii) Sort the data on the Marks only


[user43@linserver user43]$ sort +4 Stud244
289
Shweta
Choudhary
7-12-1985
291
Mildred
D'mello
9-12-1985
298
Vikrant
Mahkal
12-5-1985
289
Megha
Menon
14-10-1985
300
Sankalp
Naik
11-5-1985
RollNo FirstName
LastName
DateOfBirth

430
500
500
550
550
Marks

7) Construct the commands and execute them to


(i) Create a file named fsp<seat_no> having of atleast 50 lines
[user43@linserver user43]$ touch fsp244
[user43@linserver user43]$ head -50 /etc/mime.types > fsp244
[user43@linserver user43]$ cat fsp244
# This is a comment. I love comments.
# This file controls what Internet media types are sent to the
client for
# given file extension(s). Sending the correct media type to the
client
# is important so they know how to handle the content of the file.
# Extra types can either be added here or by using an AddType
directive
# in your config files. For more information about Internet media
types,
# please read RFC 2043, 2046, 2047, 2048, and 2077. The Internet
media type
# registry is at <http://www.iana.org/assignments/media-types/>.
# MIME type
application/EDI-Consent

Department of Computer Science

Extension

18

Kirti College

T.Y.B.Sc.

Linux

...
application/news-message-id
application/news-transmission
application/ocsp-request
application/ocsp-response
(ii) Display first two lines of fsp<seat_no> and convert all the
characters into capital letters
[user43@linserver user43]$ head -2 fsp244 | tr '[a-z]' '[A-Z]'
# THIS IS A COMMENT. I LOVE COMMENTS.
(iii) Display the last 15 lines
[user43@linserver user43]$ tail
application/iotp
application/ipp
application/isup
application/font-tdpfr
application/mac-binhex40
application/mac-compactpro
application/macwriteii
application/marc
application/mathematica
application/mathematica-old
application/msword
application/news-message-id
application/news-transmission
application/ocsp-request
application/ocsp-response

of fsp<seat_no>
-15 fsp244

hqx
cpt

doc

(iv) Display the lines starting with a vowel


[user43@linserver user43]$ grep ^[aeiouAEIOU] fsp244
application/EDI-Consent
application/EDI-X12
application/EDIFACT
application/activemessage
application/andrew-inset
ez
application/applefile
application/atomicmail
application/batch-SMTP
application/beep+xml
application/cals-1840
application/commonground
application/cybercash
application/dca-rft
application/dec-dx
application/dvcs
application/eshop
application/http
application/hyperstudio
application/iges
application/index

Department of Computer Science

19

Kirti College
application/index.cmd
. . .
application/mathematica-old
application/msword
application/news-message-id
application/news-transmission
application/ocsp-request
application/ocsp-response

T.Y.B.Sc.

Linux

doc

(v) Split a file fsp<seat_no> into subparts each


lines and display the contents of these subparts
number of lines in them
[user43@linserver user43]$ split -20 fsp244
[user43@linserver user43]$ ls
+1
date1
fcmp3_289
line
dir1
fcut1
login
ashwini
loop
product_244 try2
bdata244
masterdata
result
tryTime
check
merit244
result1
xaa
emp
pract7.18
Shweta244
xab
empdata244
pract7.3
ss1
xac
commandline
newsed
ss2
yellow
copy1
newsedclear ss3
cutlist1
fcmp1_244
cutlist2
fcmp2
gre2
pract7.1
prime1.c
date
fcmp2_244
green
pract7.10
prime1.out
Student244

having at most 20
and count the

t4
dir2
dir3
doll
fod1
foreg
fact
fcmp1
gre1
pract7.6

10000
fcut2
fex1
file2
month
names
fsed1
fsp244
pract

pract7.7

[user43@linserver user43]$ cat xaa | wc -l


20
[user43@linserver user43]$ cat xab | wc -l
20
[tybsc289@linserver tybsc289]$ cat xac | wc -l
10

Department of Computer Science

20

Kirti College

T.Y.B.Sc.

Linux

(vi) Split the files fsp<seat_no> into three subparts named fspaa,
fspab, fspac and display the contents of these files and count the
number of lines in them
[user43@linserver user43]$ split -20 fsp244 fsp
[user43@linserver user43]$ ls
+1
date1
fcmp3_244
line
t4
10000
dir1
fcut1
login
ashwini
dir2
fcut2
loop
product_244 try2
bdata244
dir3
fex1
masterdata
result
tryTime
check
doll
file2
merit244
result1
xaa
emp
fod1
month
pract7.18
Shweta244
xab
empdata244 foreg
names
pract7.3
fspaa
xac
commandline fact
fsed1
newsed
fspab
yellow
copy1
fcmp1
fsp244
newsedclear fspac
cutlist1
fcmp1_244
gre1
pract
cutlist2
fcmp2
gre2
pract7.1
pract7.6
prime1.c
date
fcmp2_244
green
pract7.10
pract7.7
prime1.out
Student244
[user43@linserver user43]$ cat fspaa | wc -l
20
[user43@linserver user43]$ cat fspab | wc -l
20
[user43@linserver user43]$ cat fspac | wc -l
10

Department of Computer Science

21

Kirti College

T.Y.B.Sc.

Linux

Practical No.5
1) od,cmp,comm.,diff,uniq:
Create a file named fod1 with some contents
contents and display it in
(i) octal form only
[user43@linserver user43]$ od -b fod1
0000000 124 150 151 163 040 151 163 040 160
141
0000020 154 040 156 165 155 142 143 162 040
164
0000040 141 040 103 150 157 165 144 150 141
163
0000060 143 012
0000062
(ii) octal form along with its text
[user43@linserver user43]$ od -b -c
0000000 124 150 151 163 040 151 163
141
T
h
i
s
i
s
a
0000020 154 040 156 165 155 142 143
164
l
n
u
m
b
e
t
0000040 141 040 103 150 157 165 144
163
a
C
h
o
u
d
s
0000060 143 012
c \n
0000062

having the following

162 141 143 164 151 143


065 012 123 150 167 143
162 171 012 124 131 102

contents.
fod1
040 160 162 141 143 164 151 143
p

162 040 065 012 123 150 167 143


r

\n

150 141 162 171 012 124 131 102


h

\n

2) Construct the commands to


(a) Create a file fcmp1<seat_no> with six lines containing six
names.
[user43@linserver user43]$ vi fcmp1_244
[user43@linserver user43]$ cat fcmp1_244
Shweta
Megha
Mildred
Sonal
Nikita
Nisha

Department of Computer Science

22

Kirti College

T.Y.B.Sc.

Linux

(b) Add two more names and save the contents in fcmp2<seat_no>.
[user43@linserver user43]$ cp fcmp1_244 fcmp2_244 && cat >>
fcmp2_244
Minal
Ankit
[user43@linserver user43]$ cat fcmp2_244
Shweta
Megha
Mildred
Sonal
Nikita
Nisha
Minal
Ankit
(c) Sort the contents of file fcmp1<seat_no>.
[user43@linserver user43]$ sort fcmp1_244
Ankit
Megha
Mildred
Minal
Nikita
Nisha
Shweta
Sonal
(d) Display the names, which are common to fcmp1<seat_no>, and
fcmp2<seat_no>.
[user43@linserver user43]$ comm fcmp1_244 fcmp2_244
Shweta
Megha
Mildred
Sonal
Nikita
Nisha
Minal
Ankit
(e) Display the difference between fcmp1<seat_no> and
fcmp2<seat_no>.
[user43@linserver user43]$ cat >> fcmp1_244
Abhishek
[user43@linserver user43]$ diff fcmp1_244 fcmp2_244
9d8
< Abhishek

Department of Computer Science

23

Kirti College

T.Y.B.Sc.

Linux

(f) Append the contents of fcmp1<seat_no> to fcmp2<seat_no> and


store it in the file fcmp3<seat_no>.
[user43@linserver user43]$ cat fcmp1_244 fcmp2_244 >fcmp3_244
[user43@linserver user43]$ cat fcmp3_244
Shweta
Megha
Mildred
Sonal
Nikita
Nisha
Minal
Ankit
Shweta
Megha
Mildred
Sonal
Nikita
Nisha
Minal
Ankit
Abhishek
(g) Sort the contents of fcmp3<seat_no> and display the contents
without and duplicate lines.
[user43@linserver user43]$ sort -u fcmp3_244
Abhishek
Ankit
Megha
Mildred
Minal
Nikita
Nisha
Shweta
Sonal
3) Create two files named fcmp1 and fcmp2 which consists of at least
five lines with two or three similar lines. Construct the commands.
[user43@linserver user43]$ cat fcmp1
This is fcmp1
This is fcmp2
This is unix practical
Practical on od
Practical no 5
[user43@linserver user43]$ cat fcmp2
This is fcmp1
This is fcmp2
This is 3rd line
Goodbye
Practical no 5

Department of Computer Science

24

Kirti College

T.Y.B.Sc.

Linux

A)
(i) To check whether the files differ.
[user43@linserver user43]$ cmp fcmp1 fcmp2
fcmp1 fcmp2 differ: byte 37, line 3
(ii) To compare the two files byte to byte.
[user43@linserver user43]$ cmp -l fcmp1 fcmp2
37 165 63
38 156 162
39 151 144
40 170 40
41 40 154
42 160 151
43 162 156
44 141 143
43 143 12
. . .
59 141 151
60 154 143
61 40 141
62 157 154
63 156 40
64 40 156
66 144 40
67 12 65
68 120 12
cmp: EOF on fcmp2
B) Using diff
(i) To display the lines which are common to both, the lines which
are not common and to display the lines, which are common to both.
[user43@linserver user43]$ diff fcmp1 fcmp2
3,4c3,4
< This is unix practical
< Practical on od
--> This is 3rd line
> Goodbye
(ii) To display the difference in context output format.
[user43@linserver user43]$ diff -c fcmp1 fcmp2
*** fcmp1
2006-01-24 09:30:21.000000000 +0530
--- fcmp2
2006-01-24 09:35:02.000000000 +0530
***************
*** 1,5 ****
This is fcmp1
This is fcmp2
! This is unix practical
! Practical on od
Practical no 5

Department of Computer Science

25

Kirti College

T.Y.B.Sc.

Linux

--- 1,5 ---This is fcmp1


This is fcmp2
! This is 3rd line
! Goodbye
Practical no 5
(iii) To display the unified output format.
[user43@linserver user43]$ diff -u fcmp1 fcmp2
--- fcmp1
2006-01-24 09:30:21.000000000 +0530
+++ fcmp2
2006-01-24 09:35:02.000000000 +0530
@@ -1,5 +1,5 @@
This is fcmp1
This is fcmp2
-This is unix practical
-Practical on od
+This is 3rd line
+Goodbye
Practical no 5
C) Using comm
(i) To compare the files fcmp1 and fcmp2
[user43@linserver user43]$ comm fcmp1 fcmp2
This is fcmp1
This is fcmp2
This is 3rd line
Goodbye
Practical no 5
This is unix practical
Practical on od
Practical no 5
(ii) To display the lines which are unique to fcmp1 and fcmp2
[user43@linserver user43]$ comm -3 fcmp1 fcmp2
Goodbye
Practical no 5
This is unix practical
Practical on od
Practical no 5
(iii) To display the lines which are common to fcmp1 and fcmp2
[user43@linserver user43]$ comm -12 fcmp1 fcmp2
This is fcmp1
This is fcmp2
This is 3rd line
D) Using n1 to give the line numbers to lines in fcmp1.
[user43@linserver user43]$ nl fcmp1
1 This is fcmp1
2 This is fcmp2
3 This is unix practical

Department of Computer Science

26

Kirti College
4
5

T.Y.B.Sc.

Linux

Practical on od
Practical no 5

E) Using cat to append the fcmp1 to fcmp2 and sort this appended
fcmp2 and store it in a file named funiq1
[user43@linserver user43]$ cat fcmp1
Hello
Hi
GoodMorning
GoodNight
Good Bye
[user43@linserver user43]$ cat fcmp2
Hello
Hi
GoodMorning
GoodNight
Good Bye
Hello
Hi
GoodMorning
GoodNight
Good Bye
[user43@linserver user43]$ cat fcmp1 fcmp2 |sort > funiq1
[tybsc297@linserver tybsc297]$ cat funiq1
Good Bye
Good Bye
Good Bye
GoodMorning
GoodMorning
GoodMorning
GoodNight
GoodNight
GoodNight
Hello
Hello
Hello
Hi
Hi
Hi

Department of Computer Science

27

Kirti College

T.Y.B.Sc.

Linux

F) Using uniq
(i) To remove the duplicate lines in funiq1
[user43@linserver user43]$ uniq funiq1
Good Bye
GoodMorning
GoodNight
Hello
Hi
(ii) To count the duplications and prepend number to each line
[user43@linserver user43]$ uniq -c funiq1
3 Good Bye
3 GoodMorning
3 GoodNight
3 Hello
3 Hi
(iii) To display the duplicate lines only
[user43@linserver user43]$ uniq -d funiq1
Good Bye
GoodMorning
GoodNight
Hello
Hi
(iv) To display unique lines only
[user43@linserver user43]$ uniq funiq1
Good Bye
GoodMorning
GoodNight
Hello
Hi
4) Create a file name<roll_no> with fields(first name, second name,
last name, salary)separated by :. Insert at least five appropriate
records in above file
[user43@linserver user43]$ vi Shweta244
[user43@linserver user43]$ cat Shweta244
Shweta:Naresh:Choudhary:20000
Anu:Chandu:Menon:30000
Nisha:Suresh:Choudhary:2500
Mildred:Felix:D'mello:40000
Dilip:Chandu:Mehra:60000

Department of Computer Science

28

Kirti College

T.Y.B.Sc.

Linux

(i) Sort on first names only.


[user43@linserver user43]$ sort +1 -2 Shweta244
Anu:Chandu:Menon:30000
Dilip:Chandu:Mehra:60000
Mildred:Felix:D'mello:40000
Nisha:Suresh:Choudhary:2500
Shweta:Naresh:Choudhary:20000
(ii) Display only those records whose first names start with a
vowel.
[user43@linserver user43]$ grep ^[AEIOU] Shweta244
Anu:Chandu:Menon:30000
(iii) Sort on last names only.
[user43@linserver user43]$ sort -t \: +2 Shweta244
Shweta:Naresh:Choudhary:20000
Nisha:Suresh:Choudhary:2500
Mildred:Felix:D'mello:40000
Dilip:Chandu:Mehra:60000
Anu:Chandu:Menon:30000
(iv) Display the names with salary above 10000 and add two more
records and redirect the output to the file namednew<roll_no>.
[user43@linserver user43]$ grep '[1-9][0-9][0-9][0-9][0-9]'
Shweta244
Shweta:Naresh:Choudhary:20000
Anu:Chandu:Menon:30000
Mildred:Felix:D'mello:40000
Dilip:Chandu:Mehra:60000
5) Cut,paste,tr
Create two files with at least three fields(columns) each with the
names fcut1, fcut2 and do the following:
[user43@linserver user43]$ cat fcut1
Akshay Das 256
Allen Disuza 121
Hari Kutian 231
Mahesh Deshpande 234
Naresh Nair 431
[user43@linserver user43]$ cat fcut2
This is fcmp1
This is fcmp2
This is 3rd line
Goodbye
Practical no 5

Department of Computer Science

29

Kirti College

T.Y.B.Sc.

Linux

(i) Cut first two columns from fcut1 and store the contents in the
file cutlist1 and cut the second and third columns from the fcut2
and store it in cutlist2.
[user43@linserver user43]$ cut -c 1-2 fcut1 >> cutlist1
[user43@linserver user43]$ cat cutlist1
Ak
Al
Ha
Ma
Na
[user43@linserver user43]$ cut -c 2-3 fcut2 >> cutlist2
[user43@linserver user43]$ cat cutlist2
hi
hi
hi
oo
ra
(ii) Paste the contents of cutlist2 to contents of cutlist1.
[user43@linserver user43]$ paste cutlist2 >> cutlist1
[user43@linserver user43]$ cat cutlist1
Ak
Al
Ha
Ma
Na
hi
hi
hi
oo
ra
Translate the first three lines into capital letters using tr
command.
[user43@linserver user43]$ head -3 fcut1 fcut2 | tr '[a-z]' '[A-Z]'
==> FCUT1 <==
AKSHAY DAS 256
ALLEN DISUZA 121
HARI KUTIAN 231
==> FCUT2 <==
THIS IS FCMP1
THIS IS FCMP2
THIS IS 3RD LINE

Department of Computer Science

30

Kirti College

T.Y.B.Sc.

Linux

Practical No 6
Grep,egrep,fgrep:
A) Create the file with the name gre1 and the following contents:
[user43@linserver user43]$ cat gre1
This is a first line.
This is a second line.
Please type the third line.
Do you wish to continue?
Simply type the fourth line.
B) Create the file with the name gre2 and the following contents:
[user43@linserver user43]$ cat gre2
The grep is an acronym for 'globally search a regular expression and
print it'.
The command searches the specified inpyt globally for a match with
the specified patter
n and displays it.While forming the pattern to be searched we can
use shell metacharact
ers, or regular expressions as professional unix users call them.
C) Do the following:
1) Search the word line and display the lines containing it.
[user43@linserver user43]$ grep line gre1
This is a first line.
This is a second line.
Please type the third line.
Simply type the fourth line.
2) Search the word the or The in both the files gre1 and gre2 and
display the lines containing it.
[user43@linserver user43]$ grep the gre1 gre2
gre1:Please type the third line.
gre1:Simplt type the fourth line.
gre2:The command searches the specified inpyt globally for a match
with the specified p
attern and displays it.While forming the pattern to be searched we
can use shell metach
aracters, or regular expressions as professional unix users call
them.
[user43@linserver user43]$ grep The gre1 gre2
gre2:The grep is an acronym for 'globally search a regular
expression and print it'.
gre2:The command searches the specified inpyt globally for a match
with the specified p
attern and displays it.While forming the pattern to be searched we
can use shell metach
aracters, or regular expressions as professional unix users call
them.

Department of Computer Science

31

Kirti College

T.Y.B.Sc.

Linux

3) Search for 4 letters in gre1 and gre2 whose first character is


rand last character is r
[user43@linserver user43]$ grep r......r gre1 gre2
gre2:The command searches the specified inpyt globally for a match
with the spec
ified pattern and displays it.While forming the pattern to be
searched we can us
e shell metacharacters, or regular expressions as professional unix
users call t
hem.
4) Display the lines, which end with the characters from s to z from
gre1 and gre2
[user43@linserver user43]$ grep '[s-z]$' gre1 gre2
gre1:This is a first line from a to z
gre2:The grep is an acronym for globally search a regular expression
and print it
D) Create a file empdata<seatno>, which contains the following
fields.
[user43@linserver user43]$ cat empdata244
Menon~Reena~E01~Mumbai~MKT~A~1~07-12-74~10000
Rao~Leena~E02~Madras~HRD~B~2~08-09-78~1000
Nair~Anu~E03~Dadar~PUR~A~6~10-12-85~50000
Shah~Meena~E04~Mahim~HRD~B~5~16-11-85~2500
Naik~Rupa~E05~Bandra~MKT~A~1~15-05-89~30000
(i) Display all employees who are not in department MKT.Display the
output sorted on department code and grade.
[user43@linserver user43]$ grep -v MKT empdata244 | sort +5 -7
Nair~Anu~E03~Dadar~PUR~A~6~10-12-85~50000
Rao~Leena~E02~Madras~HRD~B~2~08-09-78~1000
Shah~Meena~E04~Mahim~HRD~B~5~16-11-85~2500
(ii) Display the employees whose years of service are more than 5.
[user43@linserver user43]$ grep '~[5-9*]~' empdata244
Nair~Anu~E03~Dadar~PUR~A~6~10-12-85~50000
Shah~Meena~E04~Mahim~HRD~B~5~16-11-85~2500
(iii) Display employee(s) who get higheast salary
(iv) Store employee name and date of birth in a file bdata<seatno>.
[user43@linserver user43]$ cut -f 1,2,8 -d"~" empdata244 > bdata244
[user43@linserver user43]$ cat bdata244
Menon~Reena~07-12-74
Rao~Leena~08-09-78
Nair~Anu~10-12-85
Shah~Meena~16-11-85
Naik~Rupa~15-05-89
(v) Count total number of employees whose department code is HRD.

Department of Computer Science

32

Kirti College

T.Y.B.Sc.

Linux

[user43@linserver user43]$ grep -c HRD empdata244


2
E) Create a file student<saetno> with the following fields:
[user43@linserver user43]$ cat Student244
S01:Shweta:Q11:5:500
S02:Megha:Q12:1:400
S03:Milu:Q13:3:200
S04:Anu:Q11:2:100
S05:Ankit:Q15:4:300
(i) Display the details of student in order of their name ignoring
case.
[user43@linserver user43]$ sort -i -t":" +1 -2 Student244
S05:Ankit:Q15:4:300
S04:Anu:Q11:2:100
S02:Megha:Q12:1:400
S03:Milu:Q13:3:200
S01:Shweta:Q11:5:500
(ii) Display the details of student whose number of modules is
greater than 3.
[user43@linserver user43]$ grep ':[4-5]:' Student244
S01:Shweta:Q11:5:500
S05:Ankit:Q15:4:300
(iii) Store the list of first 5 rank holders in merit<saetno> file.
[user43@linserver user43]$ sort -t":" -r +4 Student244 | head -n 5 >
merit2
89
[user43@linserver user43]$ cat merit244
S01:Shweta:Q11:5:500
S02:Megha:Q12:1:400
S05:Ankit:Q15:4:300
S03:Milu:Q13:3:200
S06:Anu:Q12:2:100
(iv) Count number of students in Batch Q13.
[user43@linserver user43]$ grep -c Q13 Student244
1
(v) Display the list of students with same names.
[user43@linserver user43]$ grep Anu Student244
S04:Anu:Q11:2:100
S06:Anu:Q12:2:100

Department of Computer Science

33

Kirti College

T.Y.B.Sc.

Linux

Practical No 7
Q1)Advanced Shell Programming I
1) To find the sum and product of integers.
[user43@linserver user43]$ cat calc
a=30
b=20
expr $a + $b
expr $a \* $b
echo
Output
[user43@linserver user43]$ bash calc
50
600
2) To read the basic salary and find the gross salary.
[user43@linserver user43]$ cat pract7.3
echo Enter your basic salary
read basic
echo da
da= expr \( $basic \* 20 / 100 \)
echo hra
hra= expr \( $basic \* 30 / 100 \)
echo Taxes
Taxes= expr \( $basic \* 20 / 100 \)
echo Your gross salary is
expr $basic + \( $basic \* 20 / 100 \) + \( $basic \* 30 / 100 \)
- \( $basic \*
20 / 100 \)
Output
[user43@linserver user43]$ bash pract7.3
Enter your basic salary
1000
da
200
hra
300
Taxes
200
Your gross salary is
1300

Department of Computer Science

34

Kirti College

T.Y.B.Sc.

Linux

3) To check whether the file with he name entered exists or not.


[user43@linserver user43]$ cat fileexists
echo enter filename
read filename
if [ -s $filename ]
then
echo file exists
else
echo does not exists
fi
Output
[user43@linserver user43]$ bash fileexits
enter filename
greenapple
file exists
4) To compare the two strings.
[user43@linserver user43]$ cat stringcompare
echo enter a string1:
read string1
echo enter another string2:
read string2
if [ $string1 = $string2 ]
then
echo strings match
else
echo strings do not match
fi
Output
[user43@linserver user43]$ bash stringcompare
enter a string1:
Unix
enter another string2:
Unix
strings match

Department of Computer Science

35

Kirti College

T.Y.B.Sc.

Linux

5) To check whether the file has a permission to write.


[user43@linserver user43]$ cat filepermission
echo enter an existing file name
read filename
if [ -w $filename ]
then
echo yes, the file has a write permission
else
echo no write permission
fi
Output
[user43@linserver user43]$ bash filepermission
enter an existing file name
greenapple
no write permission
6) To give grades using multiple ifs.
[user43@linserver user43]$ cat grades
echo enter your marks
read marks
if [ $marks -ge 75 -a $marks -lt 100 ]
then
echo Your grade is A
elif [ $marks -ge 60 -a $marks -lt 75 ]
then
echo Your grade is B
elif [ $marks -ge 50 -a $marks -lt 60 ]
then
echo Your grade is C
elif [ $marks -ge 35 -a $marks -lt 50 ]
then
echo Your grade is Pass
elif [ $marks -gt 0 -a $marks -lt 35 ]
then
echo You have failed
elif [ $marks -gt 100 ]
then
echo enter below 100
elif [ $marks -lt 0 ]
then
echo Invalid entry
fi

Department of Computer Science

36

Kirti College

T.Y.B.Sc.

Linux

Output
[user43@linserver user43]$ bash grades
enter your marks
158
enter below 100
7)To check whether the number is +ve or ve using ifelif.
[user43@linserver user43]$ cat 10nos
echo enter a number
read a
if [ $a -lt 0 ]
then
echo $a is negative
elif [ $a -gt 0 ]
then
echo $a is positive
else
echo number is neither positive nor negative
fi
Output
[user43@linserver user43]$ bash 10nos
enter a number
55
55 is positive
8) To print the day of week using case in
[user43@linserver user43]$ cat pract7.9
echo enter a value for a day
read d
case $d in
1) echo "Monday"
;;
2) echo "Tuesday"
;;
3) echo "Wednesday"
;;
4) echo "Thursday"
;;
5) echo "Friday"
;;
6) echo "Saturday"
;;
7) echo "Sunday"
;;
*) echo "Invalid Day Number"
esac

Department of Computer Science

37

Kirti College

T.Y.B.Sc.

Linux

Output
[user43@linserver user43]$ bash pract7.9
enter a value for a day
6
Saturday
9) To find the type of the character entered.
[user43@linserver user43]$ cat pract7.10
echo enter the character
read char
case $char in
[a-z] )echo "You have entered small case"
;;
[A-Z] )echo "You have entered capital case"
;;
[0-9] )echo "You have entered number"
;;
?)echo "You have entered a special character"
;;
*)echo "You have entered more the one character"
;;
esac
Output
[user43@linserver user43]$ bash pract7.10
enter the character
a
You have entered small case
10) To find the pattern of the string entered using casein.
[user43@linserver user43]$ cat pract7.11
echo enter the string
read s
case $s in
[aeiou]*)echo "The String begins with a small case vowel"
;;
[AEIOU]*)echo "The String begins with a capital case vowel"
;;
[0-9]*)echo "The String begins with a digit"
;;
*[0-9])echo "The String ends with a digit"
;;
?????)echo "You entered a five letter word"
;;
esac

Department of Computer Science

38

Kirti College

T.Y.B.Sc.

Linux

Output
[user43@linserver user43]$ bash pract7.11
enter the string
i
The String begins with a small case vowel
11) To find the type of the file.
[user43@linserver user43]$ cat pract7.12
echo enter the filename
read filename
if [ -f $filename ]
then
echo It is a file
if [ -c $filename ]
then
echo It is a charcter special file
elif [ -b $filename ]
then
echo It is a block special file
else
echo Format not reconized
fi
elif [ -d $filename ]
then
echo It is a directory
else
echo The file or directory doesnt exist
fi
Output
[user43@linserver user43]$ bash pract7.12
enter the filename
pract7.1
It is a file
Format not recognized
12) To prepare a menu driven program.
[user43@linserver user43]$ cat pract7.13
echo Press 1 to see the working directory
echo Press 2 to create new file with some contents
echo Press 3 to see Contents of the directory
echo Press 4 to sort a file
echo Press 5 to see all users logged in
echo Enter Your choice
read a
case $a in

Department of Computer Science

39

Kirti College

T.Y.B.Sc.

Linux

1) \pwd
;;
2) echo Enter the filename
read n
\cat > $n
;;
3) \ls dir1
;;
4) echo enter the filename
read f
\sort $f
;;
5) \who
;;
*) echo Entry made is not valid
;;
esac
Output
[user43@linserver user43]$ bash pract7.13
Press 1 to see the working directory
Press 2 to create new file with same contents
Press 3 to see Contents of the directory
Press 4 to sort a file
Press 5 to see all users logged in
Enter Your choice
1
/home/tybsc244
13) To print first n number and their sum using while loop to print.
[user43@linserver user43]$ cat loop
echo enter the number
read d
n=1
sum=0
while [ $n -le $d ]
do
echo $n
sum=` expr $sum + $n `
n=` expr $n + 1 `
done
echo The sum of $d nos is $sum

Department of Computer Science

40

Kirti College

T.Y.B.Sc.

Linux

Output
[user43@linserver user43]$ bash loop
enter the number
12
1
2
3
4
5
6
7
8
9
10
11
12
The sum of 12 nos is 78
14) To call another file.
[user43@linserver user43]$ cat pract7.15
echo Press 1 to Compare the String
echo Press 2 To check wheteher number is +ve and -ve
echo Press 3 To Obtain sum of and product two numbers
read f
case $f in
1) bash pract7.4
;;
2)bash check
;;
3)bash pract7.1
;;
*)echo Invalid entry
;;
esac
Output
[user43@linserver user43]$ bash pract7.15
Press 1 to Compare the String
Press 2 To check wheteher number is +ve and -ve
Press 3 To Obtain sum of and product two numbers
2
Enter any No
-5
The no. is negative

Department of Computer Science

41

Kirti College

T.Y.B.Sc.

Linux

15) To print the first n numbers and their sum using dountil
[user43@linserver user43]$ cat pract7.16
echo Enter The first n nos. using until
read n
i=1
sum=0
until [ $i -gt $n ]
do
echo $i
sum=`expr $sum + $i`
i=`expr $i + 1`
done
echo The Sum of $n nos is $sum
Output
[user43@linserver user43]$ bash pract7.16
Enter The first n nos. using until
12
1
2
3
4
5
6
7
8
9
10
11
12
The Sum of 12 nos is 78
16) To demonstrates the use of for loop.
[user43@linserver user43]$ cat pract7.17
echo The First 10 nos using For loop
n=0
sum=0
for n in 1 2 3 4 5 6 7 8 9 10
do
echo $n
sum=`expr $sum + $n `
done
echo The sum Of 10 nos is $sum
Output
The First 10 nos using For loop

Department of Computer Science

42

Kirti College

T.Y.B.Sc.

Linux

1
2
3
4
5
6
7
8
9
10
The sum Of 10 nos is 55
For loop using command line arguments
[user43@linserver user43]$ cat commandfor
for f in $*
do
echo $f
done
Output
[user43@linserver user43]$ bash commandfor 1 2 3
1
2
3
17) To display the directories and files.
[user43@linserver user43]$ cat pract7.18
ls
Output
[user43@linserver user43]$ bash pract7.18
=
fcmp2
login
pract7.1
pract8.11.c
saffron
+1
fcmp2289
loop
pract7.10
sample
10000
fcmp2397
megha
pract8.11.out
pract7.11 pract8.12.3 scheck
5
fcmp289
Megha289
pract7.12
pract8.1.a
dir3
.5
pract7.6
prime.out
us1
egg
pract08.2
pract7.7
product289
white
empdata289 greenpple
pract11.a
pract7.9
pscheck
workbpbunixbools
fcmp1
head
pract11.a.c pract8.1
result
fcmp1289
icecream
pract4.5
pract8.11 result1

Department of Computer Science

43

Kirti College

T.Y.B.Sc.

Linux

Practical no. 8
ADVANCED SHELL PROGRAMMING II
Ex.1
(A)Write a shell script to generate the following series
1,3,2,4,3,5,4,6100
Write down the shell script in the answer sheet. Execute it and
show it to the examiner.
[user43@linserver user43]$ cat pract8.1.a
i=1
j=3
while [ $i -le 11 ]
do
echo $i
echo `expr $i + 2 `
i=` expr $i + 1 `
done
Output
[user43@linserver user43]$ bash pract8.1.a
1
3
2
4
3
5
4
6
5
7
6
8
7
9
8
10

Department of Computer Science

44

Kirti College

T.Y.B.Sc.

Linux

(B)Give a command to display the following the following results:


1) The day is: (mm/dd/yy)
2) The weekday is: (name of the weekday)
(Replace the brackets with the actual values)
Write down the commands in the answer sheet. Execute them and
show it to the examiner.
[user43@linserver user43]$ cat date
set `date`
read m
case $m in
Jan)m=01
;;
Feb)m=02
;;
Mar)m=03
;;
Apr)m=04
;;
May)m=05
;;
Jun)m=06
;;
Jul)m=07
;;
Aug)m=08
;;
Sep)m=09
;;
Oct)m=10
;;
Nov)m=11
;;
Dec)m=12
;;
esac
echo Date is $m-$3-$6
echo Week day is $1
Output
[user43@linserver user43]$ bash date
Apr
Date is 04-2-2006
Week day is Thu

Department of Computer Science

45

Kirti College

T.Y.B.Sc.

Linux

Ex.2
(A)
(i) Give correct command to the following
1. Display your current working directory.
2. List all files starting with letter C.
3. Display your terminal address.
4. Create a subdirectory COMP<seatno>.Change to this
directory & create two files comp1 <seatno1> and
<seatno2>.
5. Make a copy of file comp2<seatno2> into file
comp3<seatno3>.Display their inode numbers.
Write down the commands in the answer sheet. Execute them and
show it to the examiner.
[user43@linserver user43]$ cat pract8.2.1
echo You have following options
echo 1. Display your current working directory
echo 2. List all files starting with letter c
echo 3. Display your terminal address
echo 4. Change to directory COMP303 and create files
echo 5. Copy file comp2303 into file comp3303 and display inode
number
echo Enter your choice
read c
case $c in
1)echo you are working on
pwd
;;
2)ls c*
;;
3)tty
;;
4)cd COMP303
echo Changed to directory COMP303
echo Enter two file names
read f1
read f2
touch $f1 $f2
echo Files created
;;
5)cp comp2303 comp3303
echo File copied
cat comp3303
ls -i comp2303 comp3303
;;
esac

Department of Computer Science

46

Kirti College

T.Y.B.Sc.

Linux

Output
[user43@linserver user43]$ bash pract8.2.1
You have following options
1. Display your current working directory
2. List all files starting with letter c
3. Display your terminal address
4. Change to directory COMP303 and create files
5. Copy file comp2303 into file comp3303 and display inode number
Enter your choice
5
File copied
This is file comp2303
Practical no 8
1056849 comp2303 1056848 comp3303
(B)Create a shell script to print summation of following series
1! +2! +3! + 4! + 5! + ------- + N!
Write down shell script in the answer sheet. Execute it and show
it to the examiner.
[user43@linserver user43]$ cat pract8.2
i=1
fact=1
sum=0
echo Enter values
read n
while [ $i -le $n ]
do
fact=`expr $fact \* $i`
i=`expr $i + 1`
sum=`expr $sum + $fact`
echo $fact
done
echo The sum of the factorial $n is $sum
Output
[user43@linserver user43]$ bash pract8.2
Enter values
5
1
2
6
24
120
The sum of the factorial 5 is 153

Department of Computer Science

47

Kirti College

T.Y.B.Sc.

Linux

Ex.3
(A)Write a shell script, which displays number of ordinary files,
directories and special files in the current working directory.
Write down the shell script on the answer sheet. Execute it and show
it to the examiner.
[user43@linserver user43]$ cat filetype
echo Enter a filename
read filename
if [ -f $filename ]
then
echo Entered a file name
if [ -s $filename ]
then
echo File of length greater than 0
elif [ -c $filename ]
then
echo Special character file
elif [-b $filename ]
then
echo Block special file
else
echo File not supported
fi
elif [ -d $filename ]
then
echo entered a directory
else
echo Invalid name
fi
Output
[user43@linserver user43]$ bash filetype
Enter a filename
greenapple
Entered a file name
File of length greater than 0
Ex.4
(A)Write a shell script to create a file, which stores the names of
the files and against each name, put either morning, evening or
afternoon depending upon the time when file was created. Write a
shell script in answer sheet, execute it and show it to the
examiner.
[user43@linserver user43]$ cat time1
echo Enter File name
read f
hrs=`ls -l |grep $f|cut -d" " -f 19|cut -c1,2`
echo $hrs

Department of Computer Science

48

Kirti College

T.Y.B.Sc.

if [ $hrs -lt 12 -a $hrs -ge 0


then
echo `ls -l | grep $f`
elif [ $hrs -lt 16 -a $hrs -ge
then
echo `ls -l | grep $f`

Linux

]
Morning
12 ]
Afternoon

elif [ $hrs -lt 19 -a $hrs -ge 16 ]


then
echo `ls -l | grep $f ` Evening
else
echo `ls -l | grep $f ` Night
fi
Output
[user43@linserver user43]$ bash time1
Enter File name
time1
11
-rw-r--r-- 1 tybsc244 tybsc 372 Jan 25 11:15 time1 Morning
Ex.5
Create a file named student<seatno> with fields (Rollno, Student
name, Marks). Fields are separated with a colon (:).
Write a shell script to perform the following options
1. Print the content of he file.
2. Delete the given file
3. Rename the file
4. If above three are not there then print error message.
Write down the commands in the answer sheet, execute it and
show it to the examiner.
[user43@linserver user43]$ cat pract8.5
echo Please enter ur choice
echo Press 1 to print the contents of the file
echo Press 2 to delete the given file
echo Press 3 to rename the file
read n
echo Please enter the existing filename
read fname
case $n in
1)cat $fname
;;
2)rm $fname
echo $fname has been deleted
;;
3)echo enter the new filename
read newname
mv $fname $newname

Department of Computer Science

49

Kirti College

T.Y.B.Sc.

Linux

echo $fname has been replaced with $newname


;;
*)echo this is invalid
esac
Output
[user43@linserver user43]$ bash pract8.5
Press 1 Print the contents of the file
Press 2 Delete the given file
Press3 Rename the file
Enter Your Choice?
1
enter the filename
white
This File Contains:
helooo this a white file
a good white file
Ex.6
(A)
Write a shell script, which allows the user to add unique
records in the file product<seatno> (fields are product code,
product name, price and quantity on hand).Insert at least five
records in the product<seatno>file.
Write down shell script in the answer sheet. Execute it and show
it to the examiner.
[user43@linserver user43]$ cat pract8.6
echo enter the product id:
read id
echo enter the product name
read name
echo enter the price
read price
echo enter the product qty
read qty
line=`echo $id:$name:$price:$qty`
n=`grep $id product289 | wc -l`
if [ $n -gt 0 ]
then
echo Cant enter duplicate record
else
echo $line >> product244
echo Record entered
fi
cat product289

Department of Computer Science

50

Kirti College

T.Y.B.Sc.

Linux

Output
[user43@linserver
enter the product
04
enter the product
lmn
enter the price
90
enter the product
8
Record entered
01:abc:10:2
01:abc:12:09
02:pqr:20:1
03:bcd:20:20
04:lmn:90:8

user43]$ bash pract8.6


id:
name

qty

Ex.7
(A)Write a shell script that asks the user to input a number N and
displays the squares of al
numbers from 1 to N as follows.
1 square = _______
2 square = _______
.
.
N square = _______
Write down the shell script in the answer sheet, execute it and
show it to the examiner.
[user43@linserver user43]$ cat pract8.7
echo enter the number N
read N
i=1
sqr=1
while [ $i -le $N ]
do
sqr=` expr $i \* $i `
echo $i square = $sqr
i=` expr $i + 1`
done
Output
[user43@linserver user43]$ bash pract8.7
enter the number N
5
1 square = 1
2 square = 4
3 square = 9
4 square = 16
5 square = 25

Department of Computer Science

51

Kirti College

T.Y.B.Sc.

Linux

Ex.8
(A)Generate a shell script, which accepts a user name. If user is
not present then appropriate message should be displayed
otherwise tell the user to logout out. Wait for 5 seconds and
then kill the given users jobs.
[NOTE: User will be provided by the examiner.]
Write down the shell script in the answer sheet, execute it and
show it to the examiner.
Create a file named file <seatno> of your choice and try that
grep command to search for the following patterns along with the
following options.
1. To print only a count of line that contains a
pattern.
2. Ignore uppercase/lowercase distinction during
comparison.
3. Print all lines except those contain a pattern.
Write down the commands in the answer sheet, execute them and
show it to the examiner.
[user43@linserver user43]$ cat pract8.8
echo enter the username
read us
who >> us1
n=`grep $us us1 | wc -l `
if [ $n -gt 0 ]
then
echo You are valid user
else
echo Invalid user
fi
Output
[user43@linserver user43]$ bash pract8.8
enter the username
tybsc244
You are valid user
[user43@linserver user43]$ bash pract8.8
enter the username
tybsc200
Invalid user

Department of Computer Science

52

Kirti College

T.Y.B.Sc.

Linux

Ex.9
(A)Generate a shell script that will check for every minute, how
many
users logged in.
Write down shell script in the answer sheet, execute it and show
it to the examiner.
[user43@linserver user43]$ cat pract8.9
#To Check Whether how many users have logged in every minute
echo Enter the time
read time
t=`who | cut -c 30,31,32,33,34 | grep -c $time`
echo The Number of users who logged in at $time :$t
Output
[user43@linserver user43]$ bash pract8.9
Enter the time
15:46
The Number of users who logged in at 15:46 :3
Ex.10
(A)Write a shell script to generate prime series up to N.
Write down shell script in the answer sheet, execute it and show
it
to the examiner.
[user43@linserver user43]$ cat pract8.11.c
#include<stdio.h>
#include<math.h>
main(void)
{int no,i,j,prime=1,m;
printf("enter a number: ");
scanf("%d",&no);
if (no==1)
printf("1 is a prime");
if (no==2)
printf("2 is a prime number");
if (no%2==0)
prime=0;

Department of Computer Science

53

Kirti College

T.Y.B.Sc.

Linux

else
{
for(i=3;i<=no;i=i+2)
{ prime=1;
for(m=3;m<=(no/2);m=m+2)
{
if(no%i==0)
{
prime=0;
break;
}
}
if (prime==1)
printf("\n%d is a prime number",i);
}
}
}
Output
[user43@linserver user43]$ gcc -o primeo.out pract8.11.c
[tybsc289@linserver tybsc289]$ ./primeo.out
Enter the no 11
3
5
7

Department of Computer Science

54

Kirti College

T.Y.B.Sc.

Linux

Ex.11
(A) Write a shell script to find GCD& LCD of two positive numbers.
Write down shell script in the answer sheet, execute it and show
it to the examiner.
echo enter two numbers
read a b
prod=`expr $a \* $b | bc -l `
if [ $a -lt $b ]
then
temp=$a
a=$b
b=$temp
fi
r=` expr $a % $b `
while [ $r -gt 0 ]
do
a=$b
b=$r
r=` expr $a % $b `
done
echo GCD = $b
LCM=`expr $prod / $b | bc -l `
echo LCM = $LCM
Output
[user43@linserver user43]$ bash gcd
enter two numbers
30 58
GCD = 2
LCM = 870
(B) Write a shell script which takes two arguments. The first
argument is a pattern & second is a filename .Check whether the
pattern supplied is found in the file or not. If found store it
in other file named output<seatno> and display the content of
output file with line numbers.
Write down shell script in the answer sheet, execute it and show
it to the examiner.
[user43@linserver user43]$ cat pract8.12.3
if [ $# -gt 2 -o $# -lt 2 ]
then
echo Wrong arguments
elif [ -f $2 ]
then
echo `grep -n $1 $2` >> result
cat result
else
echo File does not exist
fi

Department of Computer Science

55

Kirti College

T.Y.B.Sc.

Linux

Output
[user43@linserver user43]$ bash pattern1 apple greenapple
1223343777
1223343777
asdakjkjdhkwe
2:asdakjkjdhkwe
2:asdakjkjdhkwe
1:i am apple 2:then i am greenapple
Ex 12
Write a shell script,which acceptes a month number , and print
corresponding month name.
[user43@linserver user43]$ cat month1
echo enter month number
read n
case $n in
1)echo January
;;
2)echo February
;;
3)echo March
;;
4)echo April
;;
5)echo May
;;
6)echo June
;;
7)echo July
;;
8)echo August
;;
9)echo September
;;
10)echo October
;;
11)echo November
;;
12)echo December
;;
*)
echo Invalid Month
;;
Esac
Output
[user43@linserver user43]$ bash month1
enter month number
1
January

Department of Computer Science

56

Kirti College

T.Y.B.Sc.

Linux

Ex.13 (*********************************)
(A)Create the following files using given fields. Fields are
separated by a colon (:)
Masterdata<seatno> (Product number,
product name, rate, description, supplier code,
Supplier name, supplier address)
Transdata<seatno> (Product number, customer code, customer
name, unit sold, rate of
sale, date of sale.)
Insert at least three records in each file and write a shell
script to display following menu: 1: Product Query 2: Customer
Query 0; Exit
Enter choice (0-2):
If the choice is 1, get the customer number from the user and
display all records with this product number from the file
masterdata<seatno>, and then ask the user if he/she wants to
query on another product. If the answer is no, the user should be
returned to main menu.
If the choice is 2, get the customer number from the user and
display all transactions pertaining to this customer from the
file transdata<seatno>.
If the choice is 0, the user shall return to shell prompt.
If choice is other than (0-2) then an error message should be
displayed and the choice is re-accepted.
Write down shell script in the answer sheet, execute it and
show it to the examiner.
[user43@linserver user43]$ cat Pract14
echo Press 1 product query
echo Press 2 customer query
echo Press 0 for exit
read n
case $n in
1)while [ $result = y ]
do
echo enter product no
read pno
echo `cut -f 1 Masterdata | grep $pno`
echo 'Do u want to check any other product?(y/n)'
read result
done
;;
2)while [ $result = y ]
do
echo enter customer no
read cno
echo `cut -f 2 Transdata | grep $cno`
#************************************************
r=`echo grep $cno Transdata | wc -l`

echo $r

Department of Computer Science

57

Kirti College

T.Y.B.Sc.

Linux

Output
Masterdata
[user43@linserver user43]$ cat Masterdata
01:abc:10:xyz:c001:def:bbbb
02:uuu:10:ppp:c002:lll:oooo
03:kkk:11:hhh:c003:tttt:iiii
Transdata
[user43@linserver user43]$ cat Transdata
01:Cust01:shweta:03:20:22Jan2005
02:Cust02:Milu:03:25:21Oct2006
03:Cust03:Anu:04:24:11Apr2004
General Output
Press 1 product query
Press 2 customer query
Press 0 for exit
1
enter product no
03
03:kkk:11:hhh:c003:tttt:iiii
Do u want to check any other product?(y/n)
n
Press 1 product query
Press 2 customer query
Press 0 for exit
2
enter customer no
Cust01
01:Cust01:shweta:03:20:22Jan2005
1
Do u want to check any other customer?(y/n)
n
Press 1 product query
Press 2 customer query
Press 0 for exit

Department of Computer Science

58

Kirti College

T.Y.B.Sc.

Linux

Practical No 9
Editors In Linux
Part I
vi editor
Create a file with the name fvi1 with atleast 10 lines.
[user43@linserver user43]$ vi fvil
1) Perform the vi commands to the following cursor movements:
- 3 words to the right
3w
- 4 words to the left
4b
- Bottom of screen
L
- Beginning of current line
O
- Beginning of the file
H
- End of current line
$
- End of the line
G
2) Perform the vi commands to do the following deletions:
- character at the cursor
x
- one word from the current position
dw
- current line
dd
- next 4 lines
4dd
- line to the left of cursor
d0
- line to the right of cursor
d$

Department of Computer Science

59

Kirti College

T.Y.B.Sc.

Linux

Part II
Ex line editor
Create a file with the name fex1 having the contents as follows:
Do the following:
1) Invoke this file from ex mode
[user43@linserver user43]$ ex fex1
2) Give line numbers to the lines of included in the file
set nu
3) Delete the last line
dd
4) Copy the first line after the last line
y$ then 1p
5) Copy lines 2 to 5 after the last line
e4yy ep
6) Delete the line 1 to 5
e5dd
7) Append the lines 6 to 10 to this line
e3yy ep
8) Try out various options to bring the file in the original order
of line numbers
set nu
9) Replace all occurrences of the word is with IS from the first
line to the end of file
sed 1,$ s/is/IS/g fex1
10) Replace all occurrences of the word for with FOR from the
following lines of the paragraph you have already typed
sed 1,10 s/for/FOR/g fex1
Part III
sed editor
Create a file with five records with the name fsed1 and do the
following using sed command:
[user43@linserver user43]$ cat fsed1
This is file fsed1
this is cat command.
hello.
hi.
tybsc.
1) Display first three lines
[user43@linserver user43]$ sed -n '1,3p' fsed1
This is file fsed1

Department of Computer Science

60

Kirti College

T.Y.B.Sc.

Linux

this is cat command.


hello.
2) Display the last line
[user43@linserver user43]$ sed -n '$p' fsed1
tybsc.
3) Display the third and fourth line
[user43@linserver user43]$ sed -n '3,4p' fsed1
hello.
hi.
4) Insert two more records and save the new file as newsed
[user43@linserver user43]$ sed '2i\This is the 6th line\' fsed1 >
newsed
[user43@linserver user43]$ cat newsed
This is file fsed1
This is the 6th line
this is cat command.
hello.
hi.
tybsc.
[user43@linserver user43]$ sed '3i\This is the 3th line\' fsed1 >
newsed
[user43@linserver user43]$ cat newsed
This is file fsed1
this is cat command.
This is the 3th line
hello.
hi.
tybsc.
5) Delete the last two records from the file newsed
[user43@linserver user43]$ cat newsed
1
2
3
4
5
6
7
[user43@linserver user43]$ sed '$d' newsed | sed '$d'
1
2
3
4
5

Department of Computer Science

61

Kirti College

T.Y.B.Sc.

Linux

Practical No 10
1) Link to the file :ln
A) Create two files with some contents with name ln1.
[user43@linserver user43]$ vi ln1
[user43@linserver user43]$ cat ln1
This is file ln1
Practical no 10
B) Copy ln1 to ln2.
[user43@linserver user43]$ cp ln1 ln2
[user43@linserver user43]$ cat ln2
This is file ln1
Practical no 10
C) Create a hard link to as hardln1 and a soft link as softln1
to ln1
[user43@linserver user43]$ ln ln1 Hardln1
[user43@linserver user43]$ ln -s ln1 Softln1
[user43@linserver user43]$ ls
+1
emp
fspab
newsedclear pract8.1
Softln1
10000
empdata244 fspac
pract
pract8.11.2 ss1
ashwini
fact
gre1
pract7.1
pract8.12.3 ss2
bdata244
fcmp1
gre2
pract7.10
pract8.14
ss3
check
fcmp1_244 green
pract7.11
pract8.3
Stud244
checkgrade
fcmp2
Hardln1
pract7.12
pract8.5
Student
date1
fod1
merit289
pract7.4
prime.out
xab
dir1
foreg
month
pract7.5
product_244 xac
dir2
fsed1
names
pract7.6
result
yellow
dir3
fsp244
newBC
pract7.7
result1
doll
fspaa
newsed
pract7.9
Shweta244
D) Construct the command to find the file permissions and inode
numbers of the above three files.
[user43@linserver user43]$ ls -i Hardln1 Softln1
1137795 Hardln1 1137795 Softln1
E) Remove file ln1.
[user43@linserver user43]$ rm ln1
F)Type the contents of hardln1 and softln1.
[user43@linserver user43]$ cat Hardln1
This is file ln1
Practical no 10
[user43@linserver user43]$ cat Softln1
This is file ln1

Department of Computer Science

62

Kirti College

T.Y.B.Sc.

Linux

G) What is your conclusion?


Hard link Hardln1 shows the content as deleting the file ln1.
But Soft link Softln1 does not shows the content as original file
is deleted.
2) File Permissions: chmod
A) Construct the command to check the permissions of all the files
and directories on your current working directory.
[user43@linserver user43]$ ls -l Hardln1 Softln1
-rw-r--r-2 tybsc289 tybsc
33 Feb 3 11:43 Hardln1
lrwxrwxrwx
1 tybsc289 tybsc
3 Feb 3 11:49 Softln1 ->
ln1
Change the permissions of the files or directories using
(i)weight notations 4 2 1
[user43@linserver user43]$ chmod 421 ln1
[user43@linserver user43]$ ls -l ln1
-r---w---x
2 tybsc244 tybsc
33 Feb 3 11:43 ln1
(ii)symbol notations r w x
for different groups u-owner, g-group users, o-others
3) Construct the commands
A) To see the process running on your terminal.
[user43@linserver user43]$ ps T
PID TTY
STAT
TIME COMMAND
2461 pts/0
S
0:00 -bash
5397 pts/0
R
0:00 ps T
B) To see the detailed listing of processes running on your
terminal.
[user43@linserver user43]$ ps -f
UID
PID PPID C STIME TTY
TIME CMD
tybsc289 2461 2439 0 08:35 pts/0
00:00:00 -bash
tybsc289 5411 2461 0 12:16 pts/0
00:00:00 ps f
C) To see the process run by a particular user.
[user43@linserver user43]$ ps -u tybsc303
PID TTY
TIME CMD
2973 pts/1
00:00:00 bash
D) To see the process run by all the users connected in the network.
[user43@linserver user43]$ ps -f -a | more
UID
PID PPID C STIME TTY
TIME CMD
ashwini
5810 5669 0 12:41 pts/5
00:00:00 man ls
ashwini
5814 5813 0 12:41 pts/5
00:00:00 sh -c (cd
/usr/share/man && (ec
ashwini
5821 5814 0 12:41 pts/5
00:00:00 /usr/bin/less -isr
tybsc289 5826 2461 0 12:41 pts/0
00:00:00 ps -f -a
tybsc289 5827 2461 0 12:41 pts/0
00:00:00 more

Department of Computer Science

63

Kirti College

T.Y.B.Sc.

E) To see all the system process.


[user43@linserver user43]$ ps -f -u root
UID
PID PPID C STIME TTY
TIME
root
1
0 0 08:33 ?
00:00:04
root
2
1 0 08:33 ?
00:00:00
..
..
root
5443 2198 0 12:20 ?
00:00:00
192.168.100.32
root
5446 5443 0 12:20 ?
00:00:00
root
5482 2198 0 12:21 ?
00:00:00
192.168.100.32
root
5487 5482 0 12:21 ?
00:00:00

Linux

CMD
init
[keventd]
in.telnetd:
[login]
in.telnetd:
[login]

4)Construct the command


A)To mount the floppy and CDROM.
[root@linserver mnt]# mount -v floppy
/dev/fd0: Input/output error
jmount: you didn't specify a filesystem type for /dev/fd0
I will try all types mentioned in /etc/filesystems or
/proc/filesystems
Trying vfat
/dev/fd0 on /mnt/floppy type vfat (rw,nosuid,nodev)
[root@linserver mnt]# umount floppy
B) To change the status to super user.
[user43@linserver user43]$ su
Password:
[root@linserver root]#
C) Change the ownership of
[user43@linserver user43]$
this is a file whose owner
[user43@linserver user43]$

files on your home directory.


cat > tryowner
is tybsc
chmod 741 tryowner

[user43@linserver user43]$ ls -l tryowner


-rwxr----x
1 tybsc244 tybsc
36 Feb

3 13:07 tryowner

[root@linserver root]# chown tybsc291 /home/tybsc303/tryowner


[user43@linserver user43]$ ls -l tryowner
-rwxr----x
1 tybsc291 tybsc
36 Feb

3 13:07 tryowner

D)Can you create the user as a super user.


No, we cannot create the user as a super user.

Department of Computer Science

64

Kirti College

T.Y.B.Sc.

Linux

5)System Administration PartI


Construct the commands to
A)Login as a root user.
[user43@linserver user43]$ su
Password:
[root@linserver root]#
B)Create two users user1 and user2.
[root@linserver root]# useradd u 100 g tybsc d /home/user1 s
/bin/bash m user1
[root@linserver root]# useradd u 101 g tybsc d /home/user2 s
/bin/bash m user2
C)Change the ownerships of files or directories between user1 and
user2.
D)Construct the command to find the group of all the
root
directory.
[root@linserver user43]# ls /home/
administrator tybsc2244 tybsc303 user07
user109
user44
ashwini
tybsc290 tybsc304 user08
user11
user43
comp1
tybsc291 tybsc305 user09
user110
user46
comp15
tybsc292 tybsc306 user1
user111
user47
comp2
tybsc293 tybsc307 user10
user12
user48
manisha1
tybsc295 tybsc308 user101 user13
user49
siddhesh
tybsc296 tybsc309 user102 user14
user50
sup
tybsc297 user01
user103 user15
user51
supurohit
tybsc298 user02
user104 user16
vijay
ty100
tybsc299 user03
user105 user17
yogesh
tybsc286
tybsc300 user04
user106 user18
tybsc287
tybsc301 user05
user107 user19
tybsc288
tybsc302 user06
user108 user2

Department of Computer Science

users on the

user20

user32

user21

user33

user22

user34

user23

user35

user24

user36

user25

user37

user26

user38

user27

user39

user28

user4

user29

user40

user3
user30
user31

user41
user42
user43

65

Kirti College

T.Y.B.Sc.

Linux

E)Change the group of file and directories of different users.


[user43@linserver user43]$ ls -l student
-rw-r--r-1 tybsc291 tybsc
109 Jan 19 11:58 student
[root@linserver user43]# chgrp ashwini /home/tybsc291/student
[user43@linserver user43]$ ls -l student
-rw-r--r-1 tybsc291 ashwini
109 Jan 19 11:58 student
F)Construct the command to change form one particular user to
other user.
[root@linserver user43]# su tybsc291
[user43@linserver user43]$ pwd
/home/tybsc289
[user43@linserver user43]$ cd /home/tybsc291
[user43@linserver user43]$
G)Change the status of the normal user to the super user.
[user43@linserver user43]$ su
Password:
[root@linserver root]#
H)Change the password of the normal user.
[user43@linserver user43]# passwd tybsc300
Changing password for user tybsc300.
New password:
Retype new password:
passwd: all authentication tokens updated successfully.

Department of Computer Science

66

You might also like