Linux Pracs
Linux Pracs
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
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:
# 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
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 :
# 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]$ ls
c3 f6 fspaa fspac h8 linuxpractical newa1 xaa xac
dd1 fsp89609 fspab g7 linux linuxpractical~ newb2 xab
5.1- Program to calculate the sum and product of 3 user defined values
Output
Output
Output
Output
Output
Output
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
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
Output
Output
Output
Output
Output
Output
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
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:~$ 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:~$ 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
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:~$ 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:~$ rm test61
Commands/Output:
asdf@ubuntu:~$ cat >fcut
field1 field2 field3
f1 f2 f3
fld1 fld2 fld3
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
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
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
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)