0% found this document useful (0 votes)
8 views40 pages

Linux Pracs

very important question of linux

Uploaded by

ASHRAF SAYYED
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views40 pages

Linux Pracs

very important question of linux

Uploaded by

ASHRAF SAYYED
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 40

Basic Linux commands such as file and directory

manipulation, redirection and piping


1. Create three empty files with the name a1,b2,c3.
[students@localhost students]$ touch a1 b2 c3

3.Copy contents of f6 to a1,g7 to b2,h8 to c3.


[students@localhost students]$ cp f6 a1
[students@localhost students]$ cat a1
This is first practical
This is my first lecture of linux
This is cat command
This is TYCS class
The linux lecture is going on
[students@localhost students]$ cp g7 b2
[students@localhost students]$ cat b2
Sachin is a greatest batsman
Bhajji is a greatest bowler
Dhoni is a wicketkeeper of india
Yusuf is allroundar of india
The earth moves round the sun
[students@localhost students]$ cp h8 c3
[students@localhost students]$ cat c3
Good Morning
Good Afternoon
Good Evening
Good Night
Have a good day

4.Create two directories with the name dd1,dd2.


[students@localhost students]$ mkdir dd1
[students@localhost students]$ mkdir dd2

5.Copy the files a1 and b2 to directory dd1.Copy the files f6,g7 to the directory
dd2.
[students@localhost students]$ cp a1 b2 dd1
[students@localhost students]$ ls dd1
a1 b2
[students@localhost students]$ cp f6 g7 dd2
[students@localhost students]$ ls dd2
f6 g7

6.Remove the directory dd2 along with its contents.


[students@localhost students]$ rm -r dd2

7.Rename the files a1 and b2 by newa1 and newb2.


[students@localhost students]$ mv a1 newa1
[students@localhost students]$ mv b2 newb2

8.Display the login name of all users currently logged in the system.
[students@localhost students]$ users
students students
9. List all filenames with one screen at a time.
[students@localhost bin]$ ls | more
arch
ash
ash.static
aumix-minimal
awk
......
......
......
--More--
Filter Commands Such as head, tail, cat, more, sort, cut,
grep

Head:

Qus.Display the first 10 lines of /etc/mime.types


[students@localhost students]$ 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 2045, 2046, 2047, 2048, and 2077. The Internet media type
# registry is at <http://www.iana.org/assignments/media-types/>.

Tail :
Qus.Display the last 5 lines of /etc/mime.types.
[students@localhost students]$ tail -5 /etc/mime.types
video/vnd.nokia.interleaved-multimedia
video/vnd.vivo
video/x-msvideo avi
video/x-sgi-movie movie
x-conference/x-cooltalk ice

Cat:
Qus.Create three files with the name f6,g7,h8 using cat command with the some
meaningful contents with atleast five lines each.
[students@localhost students]$ cat >f6
This is first practical
This is my first lecture of linux
This is cat command
This is TYCS class
The linux lecture is going on
[students@localhost students]$ cat >g7
Sachin is a greatest batsman
Bhajji is a greatest bowler
Dhoni is a wicketkeeper of india
Yusuf is allroundar of india
The earth moves round the sun
[students@localhost students]$ cat >h8
Good Morning
Good Afternoon
Good Evening
Good Night
Have a good day

More:
Qus.List all filenames with one screen at a time.
[students@localhost bin]$ ls | more
arch
ash
ash.static
aumix-minimal
awk
......
......
......
--More—

Sort:
Qus.Sort the data on first names only.
[students@localhost students]$ sort +1 Stud89609
89609 Ajinkya Gadhave 27-11-1990 575
89602 Arafat Ansari 31-10-1990 570
89615 Mahesh Kasturi 30-1-1990 573
89611 Prasad Gone 20-1-1990 565
89601 Santosh Ankam 12-4-1989 566

Qus.Sort the data on the marks only.


[students@localhost students]$ sort +4 Stud89609
89611 Prasad Gone 20-1-1990 565
89601 Santosh Ankam 12-4-1989 566
89602 Arafat Ansari 31-10-1990 570
89615 Mahesh Kasturi 30-1-1990 573
89609 Ajinkya Gadhave 27-11-1990 575

Grep :
Qus.Display the lines starting with a vowel.
[students@localhost students]$ grep ^[aeiouAEIOU] fsp89609
application/EDI-Consent
application/EDI-X12
application/EDIFACT
application/activemessage
.....
.....
File operation such as tar,find,zip,ln,chmod

Split Command :

Qus.Create a file named fsp<seat_no>having the listing of atleast 5 lines(eg.


listing of /usr/sbin or /usr/bin or /etc or can create own).
[students@localhost students]$ touch fsp89609
[students@localhost students]$ head -5 /etc/mime.types > fsp89609
[students@localhost students]$ cat fsp89609
# 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.
1.Split the file fsp<seat_no> into subparts each having at most 20 lines and
display the contents of these subparts and count the number of lines in them.
[students@localhost students]$ split -20 fsp89609
[students@localhost students]$ ls
c3 f6 g7 linux linuxpractical~ newb2 xab
dd1 fsp89609 h8 linuxpractical newa1 xaa xac

[students@localhost students]$ cat xaa | wc -l


20

[students@localhost students]$ cat xab | wc -l


20

[students@localhost students]$ cat xac | wc -l


10
2.Split the file fsp<seat_no> into 3 subparts named fspaa,fspab,fspac and display
the contents of these files and count these files and count the number of lines in
them.
[students@localhost students]$ split -20 fsp89609 fsp

[students@localhost students]$ ls
c3 f6 fspaa fspac h8 linuxpractical newa1 xaa xac
dd1 fsp89609 fspab g7 linux linuxpractical~ newb2 xab

[students@localhost students]$ cat fspaa | wc -l


20

[students@localhost students]$ cat fspab | wc -l


20

[students@localhost students]$ cat fspac | wc -l


10
Basic Shell scripting – define variables,shell
features,read user input,conditions,loop,string and
arthimetic operations

5.1- Program to calculate the sum and product of 3 user defined values

[students@localhost students]$ cat >prog1


echo "Enter the values of a, b, c"
read n1 n2 n3
echo "You have entered "
echo $n1 $n2 $n3
echo Their sum and product is
x=`echo $n1 + $n2 + $n3 | bc -l`
y=`echo $n1 \* $n2 \* $n3 | bc -l`
echo Sum: $x
echo Product: $y

Output

[students@localhost students]$chmod +x prog1

[students@localhost students]$bash prog1


Enter the values of a, b, c
234
You have entered
234
Their sum and product is
Sum: 9
Product: 24

5.2-Program that demonstrates the use of if..else with test


[students@localhost students]$cat >prog2
echo Enter any number
read n
if test $n -gt 0
then
echo $n is positive
else
echo $n is negative
fi
Output

[students@localhost students]$chmod +x prog2

[students@localhost students]$bash prog2


Enter any number
4
4 is positive
xxx@TushaR:~/New_Folder$ ./prog2.sh
Enter any number
-5
-5 is negative

5.3-Program for IF..ELSE without test

[students@localhost students]$cat > prog3


echo Enter the basic salary
read b
if [ $b -lt 1500 ]
then
hra=`echo $b \* 10 / 100 | bc -l`
da=`echo $b \* 90 / 100 | bc -l`
else
hra=500
da=`echo $b \* 98 / 100 | bc -l`
fi
g=`echo $b+$hra+$da | bc -l`
echo Gross salary = $g

Output

[students@localhost students]$chmod +x prog3

[students@localhost students]$bash prog3


Enter the basic salary
1000
Gross salary = 2000.00000000000000000000
xxx@TushaR:~/New_Folder$
xxx@TushaR:~/New_Folder$ ./prog4.sh
Enter the basic salary
2000
Gross salary = 4460.00000000000000000000

5.6- Program to check existing file


[students@localhost students]$cat > prog4
echo Enter the File Name
read fname
if [ -f $fname ]
then
echo The File exists
else
echo File with the name $fname does not exists
fi

Output

[students@localhost students]$chmod +x prog4

[students@localhost students]$bash prog4


Enter the File Name
prog5.sh
The File exists

5.7 - Program for string comparison

[students@localhost students]$cat > prog5


echo Enter the two Strings
read str1 str2
if [ $str1 = $str2 ]
then
echo The strings are identical
else
echo The strings are not identical
fi

Output

[students@localhost students]$chomd +x prog5

[students@localhost students]$bash prog5


Enter the two Strings
RAM RAM
The strings are identical
xxx@TushaR:~/New_Folder$ ./prog6.sh
Enter the two Strings
RAM SITA
The strings are not identical

5.8 - Program to check whether file has a permission to write


[students@localhost students]$cat > prog7
echo Enter the file name
read fname
if [ -w $fname ]
then
echo Type text to append. To stop press Ctrl D.
cat >> $fname
else
echo The file has no write permission
fi

Output

[students@localhost students]$chmod +x rpog7


[students@localhost students]$bash prog7
Enter the file name
test.sh
Type text to append. To stop press Ctrl D.
echo Job completed
xxx@TushaR:~/New_Folder$ ./prog7.sh
Enter the file name
t.sh
The file has no write permission

5.9 - Program to give grades using expr command

[students@localhost students]$cat > prog8


echo Enter the marks in the five subjects
read m1 m2 m3 m4 m5
per=`expr $m1 + $m2 + $m3 + $m4 + $m5`
per=`expr $per / 5`
echo $per
if [ $per -lt 35 ]
then
echo Grade = Fail
fi
if [ $per -ge 35 -a $per -lt 45 ]
then
echo Grade = Third
fi
if [ $per -ge 45 -a $per -lt 60 ]
then
echo Grade = Second
fi
if [ $per -ge 60 -a $per -lt 75 ]
then
echo Grade = First
fi
if [ $per -ge 75 ]
then
echo Grade = First D
fi

Output

[students@localhost students]$chmod +x prog8

[students@localhost students]$bash prog8


35 65 75 90 65
66
Grade = First

5.10 - Program to check whether the number is +ve or –ve usin elif
[students@localhost students]$cat > prog9
echo Enter the number
read a
if [ $a -lt 0 ]
then
echo $a is negative
elif [ $a -gt 0 ]
then
echo $a is positive
else
echo $a is positive or negative
fi

Output

[students@localhost students]$chmod +x prog9


[students@localhost students]$bash prog9
-5
-5 is negative
xxx@TushaR:~/New_Folder$ ./prog9.sh
Enter the number
0
0 is positive or negative

5.11 - Program to print the day of the week using case.. in


[students@localhost students]$cat > prog10
echo Enter the day number
read num
case $num in
1) echo Sunday;;
2) echo Monday;;
3) echo Tuesday;;
4) echo Wednesday;;
5) echo Thursday;;
6) echo Friday;;
7) echo Saturday;;
*) echo Enter the number bet 1 to 7;;
esac
Output

[students@localhost students]$chmod +x prog10


[students@localhost students]$bash prog10
Enter the day number
f
Enter the number bet 1 to 7
xxx@TushaR:~/New_Folder$ ./prog10.sh
Enter the day number
5
Thursday

5.12 - Program to find the type of the character entered using case
[students@localhost students]$cat > prog11
echo Enter any Character
read c
case $c in
[[:lower:]]) echo Small Case Letter;;
[[:upper:]]) echo Capital Letter;;
[0-9]) echo Digit;;
?) echo Special Symbol;;
*) echo More than one character, re-enter;;
esac

# [a-z]) May work or not


# [A-Z]) May work or not
# In some OS [a-z] is equals to [aAbB...yYz]
# In some OS [A-Z] is equals to [AbB...yYzZ]

Output

[students@localhost students]$chmod +x prog11


[students@localhost students]$ bash prog11
Enter any Character
T
Capital Letter
5.13 - Program to find the pattern using case
[students@localhost students]$cat > prog12
echo Enter the word
read str
case $str in
[aeiou]*)echo The word begins with a vowel;;
[0-9]*)echo The word begins with a digit;;
*[0-9])echo The word ends with a digit;;
????) echo The word entered is 4 lettered word;;
*)echo The word entered is either starts with a Constants or incorrect input;;
Esac
Output

[students@localhost students]$chmod +x prog12


[students@localhost students]$bash prog12
Enter the word
apple
The word begins with a vowel
xxx@TushaR:~/New_Folder$ ./prog12.sh
Enter the word
cock
The word entered is 4 lettered word

5.14 - Program to find the type of the file


[students@localhost students]$cat > prog 13
echo Enter the name of the file
read fname
case $fname in
*.c) echo "It's a c prog file";;
*.out)echo It\'s an output file;;
*.sh) echo "It's a shell file";;
*) echo Not sure;;
esac
Output

[students@localhost students]$chmod +x prog13

[students@localhost students]$bash prog13


Enter the name of the file
prog13.sh
It's a shell file
xxx@TushaR:~/New_Folder$ ./prog13.sh
Enter the name of the file
test
Not sure
5.15 - Menu Driven Program
[students@localhost students]$cat > prog14
echo Enter
echo 1 To see the contents of /etc/passwd
echo 2 To see list of uers
echo 3 To see present working directory
echo 4 exit
echo enter your choice
read n
case $n in
1) cat /etc/passwd;;
2) ls /home;;
3) pwd;;
4) exit;;
*) echo Enter the choice as 1, 2, 3 or 4;;
esac
Output

[students@localhost students]$chmod +x prog14

[students@localhost students]$bash prog14


Enter
1 To see the contents of /etc/passwd
2 To see list of uers
3 To see present working directory
4 exit
enter your choice
1
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/bin/sh
bin:x:2:2:bin:/bin:/bin/sh
sys:x:3:3:sys:/dev:/bin/sh
sync:x:4:65534:sync:/bin:/bin/sync
.
.
.
And so on
Another OutPut
Enter
1 To see the contents of /etc/passwd
2 To see list of uers
3 To see present working directory
4 exit
enter your choice
3
/home/xxx/New_Folder
5.16 - Program to print first n number and their sum: while loop

[students@localhost students]$cat > prog15


i=1
sum=0
while [ $i -le 10 ]
do
echo $i
sum=`expr $sum + $i`
i=`expr $i + 1`
done
echo The sum is: $sum
Output

[students@localhost students]$chmod +x prog15

[students@localhost students]$bash prog15


1
2
3
4
5
6
7
8
9
10
The sum is: 55

5.17 - Program to call another file


[students@localhost students]$cat > prog16
echo hello!!
echo date is `date`
echo call prog15
bash prog15.sh
echo End of the prog16

Output

[students@localhost students]$chmod +x prog16

[students@localhost students]$bash prog16


hello!!
date is Wed Dec 29 14:50:19 IST 2010
call prog15
1
2
3
4
5
6
7
8
9
10
The sum is: 55
End of Prog15
End of the prog16

5.18 - Program to print first n numbers and their sum: do..until


[students@localhost students]$cat > prog17
i=1
sum=0
until [ $i -gt 10 ]
do
echo $i
sum=`expr $sum + $i`
i=`expr $i + 1`
done
echo The Sum: $sum

Output

[students@localhost students]$chmod +x prog17

[students@localhost students]$bash prog17


1
2
3
4
5
6
7
8
9
10
The Sum: 55

5.19 - Program to illustrate the use of For Loop


[students@localhost students]$cat > prog18
sum=0
for i in 1 2 3 4 5 6 7 8 9 10
do
sum=`expr $sum + $i`
echo $i
done
echo The sum is: $sum

Output

[students@localhost students]$chmod +x prog18

[students@localhost students]$bash prog18


1
2
3
4
5
6
7
8
9
10
The sum is: 55

shell operations – enviormental variables

6.1 - Write a program to demonstration of command line arguments


[students@localhost students]$cat > prog43
#(Positional parameters)
#
# $1, $2 The positional parameters
# $* Complete set of positional parameters as a single string
# $# Number of arguments specified in command line
# $0 Name of executed command
# $? Exit status of last command
# $! PID of last background job
# $@ Same as $* except when enclosed in double quote

Output

[students@localhost students]$chmod +x prog43


[students@localhost students]$bash prog43
#(Positional parameters)
#
# $1, $2 The positional parameters
# $* Complete set of positional parameters as a single string
# $# Number of arguments specified in command line
# $0 Name of executed command
# $? Exit status of last command
# $! PID of last background job
# $@ Same as $* except when enclosed in double quote

6.2 - Program that searches pattern


[students@localhost students]$cat > prog44
echo "Enter the pattern to search: "
read pat
echo "Enter the filename: "
read fname
echo OUTPUT
echo -------------------
grep "$pat" $fname && echo "Pattern found in file"
echo "********************************************"
grep "$pat" $fname || echo "Pattern not found"
exit

Output

[students@localhost students]$chmod +x prog44


[students@localhost students]$bash prog44
Enter the pattern to search:
the
Enter the filename:
prog45.sh
OUTPUT
-------------------
echo "Enter the pattern to search: "
echo "Enter the filename: "
Pattern found in file
********************************************
echo "Enter the pattern to search: "
echo "Enter the filename: "
xxx@TushaR:~/New_Folder$ ./prog45.sh
Enter the pattern to search:
do
Enter the filename:
prog44.sh
OUTPUT
-------------------
********************************************
Pattern not found

6.3 - Write a shell script to create a file which stores the name of files and
against each name put either "Morning", "Evening" or "Afternoon" depending
upon time when file is created.
[students@localhost students]$cat > prog45
rm mae
touch mae
#cat mae
ls -l > lnglst
cut -c37-38 lnglst > timelst
for i in `cat timelst`
do
if (test $i -gt 0 -a $i -lt 12)
then
echo Morning $i o\'clock >> mae
elif (test $i -ge 12 -a $i -lt 18)
then
echo Afternoon $i o\'clock >> mae
else
echo Evening $i o\'clock >> mae
fi
done
ls > fnamelst
paste fnamelst mae > mae_out
cat mae_out

Output

[students@localhost students]$chmod +x prog45

[students@localhost students]$bash prog45


fnamelst Evening 23 o'clock
lnglst Evening 23 o'clock
mae Evening 23 o'clock
mae_out Evening 23 o'clock
mae_out~ Evening 23 o'clock
prog40.sh Afternoon 12 o'clock
prog41.sh Morning 11 o'clock
prog42.sh Evening 20 o'clock
prog47.sh Evening 23 o'clock
prog47.sh~ Evening 23 o'clock
timelst Evening 23 o'clock
timelst~ Evening 23 o'clock

6.4 - Write and execute the commands for the following:


1. Create 3 empty files with the name a1,b2,c3.
2. Create 3 files with the name f6,g7,h8 using cat command with some
meaningful contents with atleast five lines each.
3. Copy the contents of f6 to a1,g7 to b2 and h8 to c3.
4. Create two directories with the name dd1,dd2.
5. Copy the files a1 and b2 to directory dd1. Copy the files f6 and g7 to
directory dd2.
6. Remove the directory dd2 along with its contents.
7. Rename the files a1 and b2 to newa1 and newb2 respectively.
8. Display the login names of all users currently logged in the system.
9. Count the number of lines,words and characters in h8.

Commands/output:
asdf@ubuntu:~$ cat > b2
asdf@ubuntu:~$ cat > c3
asdf@ubuntu:~$ cat > f6
f6Line1
f6Line2
f6Line3
f6Line4
f6Line5
asdf@ubuntu:~$ cat > g7
g7Line1
g7Line2
g7Line3
g7Line4
g7Line5
asdf@ubuntu:~$ cat > h8
h8Line1
h8Line2
h8Line3
h8Line4
h8Line5
asdf@ubuntu:~$ cp f6 a1
asdf@ubuntu:~$ cp g7 b2
asdf@ubuntu:~$ cp h8 c3
asdf@ubuntu:~$ mkdir dd1 dd2
asdf@ubuntu:~$ cp a1 b2 dd1
asdf@ubuntu:~$ cp f6 g7 dd2
asdf@ubuntu:~$ rm -r dd1
asdf@ubuntu:~$ rm -r dd2
asdf@ubuntu:~$ mv a1 newa1
asdf@ubuntu:~$ mv b2 newb2
asdf@ubuntu:~$ who
asdf tty7 2012-01-29 23:49 (:0)
asdfpts/0 2012-01-30 00:00 (:0.0)

asdf@ubuntu:~$ wc h8
5 9 78 h8

asdf@ubuntu:~$
6.5 - Write and execute the commands to change the directory to/ bin and do the
following:
1. List all filenames with one screen at a time.
2. List all filenames with 1 character and 2 characters.
3. List all filenames starting with vowels.
4. List all filenames with the last character as a or b or c or d.
5. List all filenames with exactly three characters in which the second
character is a vowel.

Commands/output:
asdf@ubuntu:/bin$ cd /bin

asdf@ubuntu:/bin$ ls |more
bash
bunzip2
busybox
bzcat
bzcmp
bzdiff
bzegrep
bzexe
bzfgrep
bzgrep
bzip2
bzip2recover
bzless
bzmore
cat
chgrp
chmod
chown
chvt
cp
cpio
dash
date

asdf@ubuntu:/bin$ ls ??
cpdddfediplnlsmt mv ncpsrmshsu

asdf@ubuntu:/bin$ ls ???
cat dirpwdsed tar
asdf@ubuntu:/bin$ ls [aeiouAEIOU]*
echoinit-checkconf open umountunicode_start
ed initctl2dot openvtuname
egrepipulockmgr_serveruncompress

asdf@ubuntu:/bin$ ls *[abcd]
chmodddedlsmodmknodncnc.openbsdpwdsedsh.distrib sync

asdf@ubuntu:/bin$ ls ?[aeiouAEIOU]?
cat dirsed tar

6.5 - Write and execute the commands to change the directory to/ etc and do the
following:
1. List the contents of the directory.
2. List the contents of the directory along with all hidden files.
3. List all files with their attributes and file permissions.
4. List all files identifying directories and executable files.
5. Give the file listing displayed in columns.
6. Give the file listing in reverse order.

Commands/output:
asdf@ubuntu:/bin$ cd /etc

asdf@ubuntu:/etc$ ls
acpi group- pcmcia
adduser.confgrub.dperl
adjtimegshadow pm
alternativesgshadow- pnm2ppa.conf
anacrontab gtk-2.0 polkit-1
apmhdparm.conf popularity-contest.conf
apparmorhost.confppp
apparmor.d hostname profile
apport hosts profile.d
apthosts.allow protocols
at.denyhosts.deny pulse
avahihp python
bash.bashrcifplugd python2.7
bash_completioninit rc0.d
bash_completion.dinit.d rc1.d
bindresvport.blacklistinitramfs-tools rc2.d
blkid.confinputrc rc3.d
blkid.tabinsserv rc4.d
bluetoothinsserv.conf rc5.d
bogofilter.cf insserv.conf.d rc6.d
bonobo-activation iproute2 rc.local
brlapi.key issue rcS.d
brltty issue.net resolvconf
ca-certificateskbdresolv.conf
ca-certificates.conf kernel rmt
calendar kernel-img.confrpc
chatscriptskerneloops.confrsyslog.conf
checkbox.dldaprsyslog.d
compizconfigld.so.cache samba
computer-janitor.dld.so.confsane.d
ConsoleKitld.so.conf.dsecuretty
console-setup legal security
cron.dlftp.conf sensors3.conf
cron.dailylibpaper.dsensors.d
cron.hourlylibreoffice services
cron.monthlylintianrcsgml
crontablocale.alias shadow
cron.weeklylocaltime shadow-
crypttablogcheck shells
cupslogin.defsskel
cupshelperslogrotate.confsnmp
dbus-1logrotate.d sound
debconf.conflsb-base speech-dispatcher
debian_version lsb-base-logging.sh ssh
defaultlsb-release ssl
defomaltrace.confsudoers
deluser.conf magic sudoers.d
depmod.dmagic.mimesysctl.conf
dhcpmailcapsysctl.d
dhcp3 mailcap.orderterminfo
dictionaries-commonmanpath.configtimezone
doc-basemime.typestpvmlp.conf

asdf@ubuntu:/etc$ ls -a
. grouppcmcia
.. group-perl
acpigrub.d pm
adduser.confgshadow pnm2ppa.conf
adjtimegshadow- polkit-1
alternatives gtk-2.0 popularity-contest.conf
anacrontabhdparm.confppp
apmhost.conf profile
apparmor hostname profile.d
apparmor.d hosts protocols
apporthosts.allow pulse
apthosts.deny .pwd.lock
at.denyhp python
avahiifplugd python2.7
bash.bashrcinit rc0.d
bash_completioninit.d rc1.d
bash_completion.dinitramfs-tools rc2.d
bindresvport.blacklistinputrc rc3.d
blkid.confinsserv rc4.d
blkid.tabinsserv.conf rc5.d
bluetoothinsserv.conf.d rc6.d
bogofilter.cf iproute2 rc.local
bonobo-activation issue rcS.d
brlapi.key issue.net resolvconf
brlttykbdresolv.conf
ca-certificates kernel rmt
ca-certificates.conf kernel-img.confrpc
calendarkerneloops.confrsyslog.conf

asdf@ubuntu:/etc$ ls -l
total23
drwxr-xr-x 3 root root 4096 2011-04-26 04:33 acpi
-rw-r--r-- 1 root root 2981 2011-04-26 04:21 adduser.conf
-rw-r--r-- 1 root root 46 2012-01-29 01:00 adjtime
drwxr-xr-x 2 root root 4096 2011-04-26 04:37 alternatives
-rw-r--r-- 1 root root 395 2010-06-20 13:41 anacrontab
drwxr-xr-x 6 root root 4096 2011-04-26 04:25 apm
drwxr-xr-x 3 root root 4096 2011-04-26 04:37 apparmor
drwxr-xr-x 8 root root 4096 2011-04-26 04:36 apparmor.d
drwxr-xr-x 4 root root 4096 2011-04-26 04:36 apport
drwxr-xr-x 6 root root 4096 2011-05-17 19:35 apt
-rw-r----- 1 root daemon 144 2010-06-28 01:06 at.deny
drwxr-xr-x 3 root root 4096 2011-04-26 04:33 avahi
-rw-r--r-- 1 root root 1939 2011-04-01 00:56 bash.bashrc
-rw-r--r-- 1 root root 58739 2011-04-05 02:07 bash_completion
drwxr-xr-x 3 root root 4096 2011-04-26 04:36 bash_completion.d
-rw-r--r-- 1 root root 344 2011-04-11 15:03 bindresvport.blacklist
-rw-r--r-- 1 root root 321 2011-03-21 13:58 blkid.conf
lrwxrwxrwx 1 root root 15 2011-05-17 19:05 blkid.tab -> /dev/.blkid.tab
drwxr-xr-x 2 root root 4096 2011-04-26 04:33 bluetooth
-rw-r--r-- 1 root root 7439 2010-10-29 20:57 bogofilter.cf
drwxr-xr-x 2 root root 4096 2011-04-26 04:33 bonobo-activation
-rw-r--r-- 1 root root 33 2011-04-26 04:36 brlapi.key
drwxr-xr-x 2 root root 12288 2011-04-26 04:36 brltty

asdf@ubuntu:/etc$ ls -F
acpi/ group- pcmcia/
adduser.confgrub.d/ perl/
adjtimegshadow pm/
alternatives/ gshadow- pnm2ppa.conf
anacrontab gtk-2.0/ polkit-1/
apm/ hdparm.conf popularity-contest.conf
apparmor/ host.confppp/
apparmor.d/ hostname profile
apport/ hosts profile.d/
apt/ hosts.allow protocols
at.denyhosts.deny pulse/
avahi/ hp/ python/
bash.bashrcifplugd/ python2.7/
bash_completioninit/ rc0.d/
bash_completion.d/ init.d/ rc1.d/
bindresvport.blacklistinitramfs-tools/ rc2.d/
blkid.confinputrc rc3.d/
blkid.tab@ insserv/ rc4.d/
bluetooth/ insserv.conf rc5.d/
bogofilter.cf insserv.conf.d/ rc6.d/
bonobo-activation/ iproute2/ rc.local*
brlapi.key issue rcS.d/
brltty/ issue.net resolvconf/
ca-certificates/ kbd/ resolv.conf
ca-certificates.conf kernel/ rmt*
calendar/ kernel-img.confrpc
chatscripts/ kerneloops.confrsyslog.conf
checkbox.d/ ldap/ rsyslog.d/

asdf@ubuntu:/etc$ ls -x
acpiadduser.confadjtime
alternativesanacrontabapm
apparmorapparmor.dapport
aptat.denyavahi
bash.bashrcbash_completionbash_completion.d
bindresvport.blacklistblkid.confblkid.tab
bluetooth bogofilter.cf bonobo-activation
brlapi.keybrlttyca-certificates
ca-certificates.conf calendar chatscripts
checkbox.dcompizconfig computer-janitor.d
ConsoleKit console-setup cron.d
cron.dailycron.hourlycron.monthly
crontabcron.weeklycrypttab
cupscupshelpers dbus-1
debconf.confdebian_version default
defomadeluser.confdepmod.d
dhcp dhcp3 dictionaries-common
doc-basedpkgemacs
environmentfirefox fonts
foomaticfstabfuse.conf
gai.conf gamin gconf
gdbgdmghostscript
ginn gnome gnome-app-install
gnome-settings-daemon gnome-system-tools gnome-vfs-2.0
gnome-vfs-mime-magicgroff group
group-grub.dgshadow

asdf@ubuntu:/etc$ ls -r
zsh_command_not_foundpapersize gnome-vfs-mime-magic
xul-extpam.d gnome-vfs-2.0
xmlpam.conf gnome-system-tools
xdg opt gnome-settings-daemon
X11 openoffice gnome-app-install
wpa_supplicantobex-data-server gnome
wodim.confnsswitch.confginn
wgetrc newt ghostscript
vtrgb networks gdm
vmware-toolsNetworkManagergdb
vim network gconf
usb_modeswitch.dnetscsid.conf gamin
usb_modeswitch.confnanorcgai.conf
UPowermtools.conffuse.conf
update-notifiermtab.fuselockfstab
update-motd.dmtabfoomatic
update-managermotd fonts
updatedb.conf mono firefox
ufw modules environment
udevmodprobe.demacs
ucf.conf mke2fs.conf dpkg
tpvmlp.confmime.types doc-base
timezonemanpath.config dictionaries-common
terminfomailcap.order dhcp3
sysctl.dmailcapdhcp
sysctl.confmagic.mimedepmod.d
6.6 - Construct the commands and execute them to
1. Create a file named fsp<seat_no> having the listing of atleast 50 lines (e.g,
listing of/usr/sbin or /usr/bin or /etc or can create your own).
2. Display first 2 lines of fsp<seat_no> and convert all the characters into
capital letters.
3. Display the last 15 lines of fsp<seat_no>.
4. Display the lines starting with a vowel.
5. Split the file fsp<seat_no> into subparts each having at most 20 lines and
display the contents of these subparts and count the number of lines in
them.
6. Split the file 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.

Commands/output:
asdf@ubuntu:~$ ls /usr/sbin>fsp61

asdf@ubuntu:~$ head -n 2 fsp61


aa-audit
aa-autodep

asdf@ubuntu:~$ head -n 2 fsp61 |tr [:lower:] [:upper:]


AA-AUDIT
AA-AUTODEP

asdf@ubuntu:~$ tail -n 15 fsp61


usermod
uuidd
validlocale
vbetool
vcstime
vidmode
vigr
vipw
visudo
vmtoolsd
vmware-checkvm
vmware-rpctool
vmware-tools-upgrader
vpddecode
zic

asdf@ubuntu:~$ grep^[aeiouAEIOU] fsp61


aa-audit
aa-autodep
aa-complain
aa-decode
aa-disable
aa-enforce
aa-genprof
aa-logprof
aa-status
aa-unconfined
aa-update-browser
accept
accessdb
acpid
addgroup
add-shell
adduser
anacron
apparmor_status
aptd
arp
arpd
aspell-autobuildhash
atd
avahi-autoipd
avahi-daemon
avivotool
e2freefrag
iconvconfig
install-docs
install-info
install-sgmlcatalog
invoke-rc.d
ip6tables-apply
iptables-apply
irqbalance
ispell-autobuildhash
ownership
ufw
update-alternatives
update-apt-xapian-index
update-binfmts
update-bootsystem-insserv
update-ca-certificates
update-catalog
update-default-aspell
update-default-ispell
update-default-wordlist
update-dictcommon-aspell
update-dictcommon-hunspell
update-fonts-alias
update-fonts-dir
update-fonts-scale
update-grub
update-grub2
update-grub-gfxpayload
update-gsfontmap
update-icon-caches
update-inetd
update-info-dir
update-initramfs
update-locale
update-mime
update-openoffice-dicts
update-passwd
update-python-modules
update-rc.d
update-rc.d-insserv
update-software-center
update-usbids
update-xmlcatalog
upgrade-from-grub-legacy
usb_modeswitch
usb_modeswitch_dispatcher
usbmuxd
useradd
userdel
usermod
uuidd

asdf@ubuntu:~$ split -l 20 fsp61

asdf@ubuntu:~$ lsxa*
xaaxabxacxadxaexafxagxahxaixajxakxalxam

asdf@ubuntu:~$ wc -l xa*
20 xaa
20 xab
20 xac
20 xad
20 xae
20 xaf
20 xag
20 xah
20 xai
20 xaj
20 xak
20 xal
11 xam
251 total

asdf@ubuntu:~$ split -l 84 fsp61 fsp

asdf@ubuntu:~$ ls fspa*
fspaafspabfspac

asdf@ubuntu:~$ wc -l fspa*
84 fspaa
84 fspab
83 fspac
251 total
6.7 - Write and execute the commands to create a file with the name
Stud<roll_no> with the following fields separated by a blank space having the
below mentioned values:
Field Roll no First Name Last Name Date of Birth Marks
Values Numeric Character Characterdd-mm-yy Numeric out of 600

1. Insert at least five appropriate records and do the following:


2. Sort the data on first names only.
3. Sort the data on the Marks only.
4. Prepare a ranked merit list with student’s first and last name only and store
in the file Merit<roll_no> and display its contents.

Commands/output:
asdf@ubuntu:~$ cat >stud61
01 cnamel lname1 01-01-1991 376
02 bname1 lname1 01-02-1991 426
03 dname1 lname1 01-03-1991 276
04 aname1 lname1 01-04-1991 479
05 enamel lname1 01-05-1991 176

asdf@ubuntu:~$ sort -k 2,2 stud61


04 aname1 lname1 01-04-1991 479
02 bname1 lname1 01-02-1991 426
01 cnamel lname1 01-01-1991 376
03 dname1 lname1 01-03-1991 276
05 enamel lname1 01-05-1991 176

asdf@ubuntu:~$ sort -k 5,5 stud61


05 enamel lname1 01-05-1991 176
03 dname1 lname1 01-03-1991 276
01 cnamel lname1 01-01-1991 376
02 bname1 lname1 01-02-1991 426
04 aname1 lname1 01-04-1991 479

asdf@ubuntu:~$ grep [4-5][0-9][0-9] stud61 |cut -d " " -f 2,3 |tee merit61
bname1 lname1
aname1 lname1
6.8 - Write down the commands and execute them for the following:
1. Create a file called test<seat_no>. Create a hard link called h_test and a
symbolic links_test. Find out the inode number of the files.
2. Remove the original file “test”. Can you still get the contents of the original
file?
3. Display the contents of the two lines h_test and s_test. Justify the output.

Commands/output:
asdf@ubuntu:~$ cat test61

asdf@ubuntu:~$ ln test61 h_test

asdf@ubuntu:~$ ln -s test61 links_test

asdf@ubuntu:~$ ls -i test61 *test


919127 h_test 919184links_test 919127 test61

asdf@ubuntu:~$ rm test61

asdf@ubuntu:~$ cat test61


cat: test61: No such file or directory

asdf@ubuntu:~$ cat h_test

asdf@ubuntu:~$ cat links_test


cat: links_test: No such file or directory
6.11 - Create two files with at least three fields (columns) each with the names
fcut, fcut2 and do the following:
1. Cut first two columns from fcut and store the contents in the file cutlist1
and cut the second and the third column from the fcut2 and store it in
cutlist2.
2. Paste the contents of cutlist2 to contents of cutlist1.
3. Translate the first three lines into capital letters using tr command.

Commands/Output:
asdf@ubuntu:~$ cat >fcut
field1 field2 field3
f1 f2 f3
fld1 fld2 fld3

asdf@ubuntu:~$ cat > fcut2


ffield1 ffield2 ffield3
ff1 ff2 ff3
ffld1 ffld2 ffld3

asdf@ubuntu:~$ cut -d " " -f 1,2fcut> cutlist1

asdf@ubuntu:~$ cut -d " " -f 2,3 fcut2 > cutlist2

asdf@ubuntu:~$ paste -d " " cutlist1 cutlist2


field1 field2 ffield2 ffield3
f1 f2 ff2 ff3
fld1 fld2 ffld2 ffld3

asdf@ubuntu:~$ head -n 3 cutlist1 | tr [:lower:] [:upper:]


FIELD1 FIELD2
F1 F2
FLD1 FLD2
6.12 - Create the file with the name gre2 and the following contents: The grep is
an acronym for ‘globally search a regular expression and print it’. The command
searches the specified input globally for a match with the specified pattern and
displays it. While forming the pattern to be searched we can use shell
metacharacters, or regular expressions as professional unix users call them.
Write and execute the commands to
1. Search the word ‘unix’ and display the word containing it.
2. Search for the word ‘the or ‘The’ in the file gre2 and display the lines
containing it.
3. Search for 4 letter words in gre2 whose first character is ‘w’ and last
character is ‘h’.

Commands/Output:
asdf@ubuntu:~$ cat >gre2
The grep is an acronym for 'globally search a regular expression and print it'
The command searches the specified input globally for a match with a specified patern
and displays it
while forming the pattern to be searched we can use shell metacharacters,or regular
expressions asprofessionalunix users call them

asdf@ubuntu:~$ grep "unix" gre2


while forming the pattern to be searched we can use shell metacharacters,or regular
expressions asprofessionalunix users call them
asdf@ubuntu:~$ grep "[tT]he" gre2
The grep is an acronym for 'globally search a regular expression and print it'
The command searches the specified input globally for a match with a specified patern
and displays it
while forming the pattern to be searched we can use shell metacharacters,or regular
expressions asprofessionalunix users call them

asdf@ubuntu:~$ grep "w..h" gre2


The command searches the specified input globally for a match with a specified patern
and displays it
asdf@ubuntu:~$
6.13 - Create a file with the name fvi1 with atleast ten lines. Perform the vi
commands to the following cursor movements:

Commands/Output:
asdf@ubuntu:~$ cat >fvi1
This is line1
This is line2
This is line3
This is line4
This is line5
This is line6
This is line7
This is line8
This is line9
This is line10
asdf@ubuntu:~$ vi fvi1
3w(3 words to right)

This is line1
[T]his is line2
This is line3
This is line4
This is line5
This is line6
This is line7
This is line8
This is line9
This is line10

4b(4 words to left)


[T]his is line1
This is line2
This is line3
This is line4
This is line5
This is line6
This is line7
This is line8
This is line9
This is line10

G(bottom of screen)
This is line1
This is line2
This is line3
This is line4
This is line5
This is line6
This is line7
This is line8
This is line9
[T]his is line10

0(begining of current line)


This is line1
This is line2
This is line3
This is line4
This is line5
This is line6
This is line7
This is line8
This is line9
[T]his is line10

1G(begining of the file)


[T]his is line1
This is line2
This is line3
This is line4
This is line5
This is line6
This is line7
This is line8
This is line9
This is line10

$(end of current line)


This is line[1]
This is line2
This is line3
This is line4
This is line5
This is line6
This is line7
This is line8
This is line9
This is line10

shift + :wq
6.14 Create a file course<seat_no> with following fields and the values of
corresponding type:
Course code Course name Batch code Duration (in days) Fees
Character CharacterCharacter Numeric Numeric
Fields are separated by “:” (colon). Open a file course using vi editor. Write and
execute vi commands for the following:
1. Move three lines down at a time.-3j
2. Copy first line of file course and paste it so that it becomes the last line –
1G,yy,G,p
3. Search for a string “Msc”-1G,/MSc
4. Go to line number 4-4G
5. Move to first word of line-0
6. Search for the pattern “BCom” and replace it with “MCom”
-:1,s/BCom/MCom/g
7. Delete previous character from current cursor position-x
8. Delete the whole line –dd

Commands/Output:
Move three lines down at a time (3j)
copy first line of file course and paste it so that it becomes thelast line(1G,yy,G,p)
search for a string ("Msc"-1G,/Msc)
go to line number 4 (4G)
move to first word of line(0)
search for the pattern "Bcom" and replace it with "Mcom" (:1,$s/BCom/MCom/g)
delete previous character from current cursor position (x)
delete the whole line (dd)

You might also like