143
Sr. No Date Topic Sign 1. Demonstration of Linux installment 2. Understand the working of UNIX, UNIX file system 3. Basic UNIX commands o File System Commands o ls and ls with options 4 Study of Commands: o tr, head, tail, wc, sort o find ,file, ,split 5 Comparing files: od, cmp, comm, diff, uniq 6 Filter Commands Grep, egrep, fgrep 7 Advanced Shell Programming I 8 Advanced Shell Programming II 9 Editors in Linux o vi editor o Ex line editor o sed editor 10 Link to the file File Permissions Super User System Administration INDEX

Linux p Arctic Al Notes

Embed Size (px)

Citation preview

Page 1: Linux p Arctic Al Notes

Sr. No

Date Topic Sign

1. Demonstration of Linux installment

2. Understand the working of UNIX,UNIX file system

3.Basic UNIX commands

o File System Commandso ls and ls with options

4

Study of Commands:o tr, head, tail, wc,

sorto find ,file, ,split

5 Comparing files:od, cmp, comm, diff, uniq

6 Filter CommandsGrep, egrep, fgrep

7 Advanced Shell Programming I

8 Advanced Shell Programming II

9

Editors in Linuxo vi editoro Ex line editoro sed editor

10

Link to the fileFile PermissionsSuper UserSystem Administration

INDEX

Page 2: Linux p Arctic Al Notes

Practical No. 1

Demonstration practical:

Perform the following:

1. Create a Linux partition.(Use either MS-DOS fdisk command or LINUX fdisk or diskdruid option).

2. Create boot disks for LINUX.

3. Install Red Hat Linux.

4. Understand the procedure of login, logout and shutting down the server.

5. Understanding the procedure for connecting to the Linux server and creating users either graphically or using Linuxconf utility.

Page 3: Linux p Arctic Al Notes

Practical No 1Installation Of Linux Red Hat 7.1

We have 4.3 GB Hard disk drive installed in the system. 2GB is partitioned for Windows 98 as FAT 32. Rest disk space we will be using for Linux installation.

1. Enter into your system’s CMOS setup, select Advanced BIOS setup (in most of the computers) and set the first boot drive as CDROM.

2. Insert the 1st CD into CDROM drive and start the computer system.3. System will boot with the Linux Installation CD and will return

to boot prompt.Boot :

4. To install Linux in Graphical Mode. Press <Enter>Boot : <Enter>5.

Default is “ English “

Press <Enter>

6.

Default is “US” Press <Enter>7. What type of system would you like to install?

Available options are Workstation

Server SystemLaptopCustom SystemUpgrade Existing Installation.

Select Custom System as this allows you to select the partitions and keeps the existing partitions as they are all other types erase the existing partitions and we can loose the Windows 98 partition.8.

Available options are Continue (Automatic) Manually Partition

Select “Manually Partition”

9.

Language Selection

Keyboard Selection

Partitions

Tool to partition the Hard Disk

Page 4: Linux p Arctic Al Notes

Available options are Disk Druid FdiskSelect “ Disk Druid “

10.Now it will display the existing partition of windows 98 FAT32. The options are

Add / Edit / Delete / OK / BackSelect “Add” it will display the following Box

Mount Point : Type : Linux Swap Linux Native DOS 16 bit < 32M DOS 16 bit > 32M

Size in (Megs)Use Remaining Space? [ ]Allowable Drives [ * ] had

Write Mount Point : /boot

Size in (Megs) : 20Select Type : Linux NativeAgain select “Add” to create second partition

Mount Point :Size in (Megs) : 64 (if you have 32MB RAM)

Select Type : Linux SwapThis will automatically write in Mount Point as “Swap Partition”Again select “Add” to create third partitionMount point : / - This “ / “ will be root point.Size in (Megs) : 1000 (Considering only 2GB is available from 4GB HDD for Linux)Use remaining space [ * ] (select “ * “ by pressing space bar)Type : Linux Native.Now the main partition menu shows “ 0 “ (Zero) M free space since we have selected use remaining space in root partition.

Click “ OK “ Save changes to your partition table “Yes” It will display following message box

11.

Select “ Yes ”

12.Choose partition to FormatBy pressing Space Bar select all the partitions we have created. i.e. Boot and Root

Select “ OK “

We need to turn ON swap space immediately. To do this we will have to write your new partition table to the disk immediately. Is that Okay?

Page 5: Linux p Arctic Al Notes

13.

LILO Configuration

A few systems will need to pass special options to the Kernel at boot time for the system to function properly. If you need to pass boot options to the Kernel, enter them Now. If you don’t need any or aren’t sure , leave this blank.

Leave this Blank and Press “ OK “14.

LILO Configuration

/dev/had Master Boot Record (MBR) /dev/hda2 First sector of boot partition.

Select “ Master Boot Record “ and press “ OK “15.

LILO Configuration

The boot manager Red Hat uses can boot other operating systems as well. You need to tell me what partitions you would like to be able to boot and what level you want to use for each of them.

Device Partition Type Default Boot Label/dev/hda1/dev/hda5

DOS / WindowsLinux Native

DOS * Linux

OK Edit Back

You can change the Boot Label if you want. Select “ Edit “ and change the label to windows instead of DOS.“ * “ is the first option you get while booting. (i.e. Linux will be the default operating system to load). You can change it to DOS / Windows by selecting that option and pressing Space Bar.

Finally Press “ OK “

16. Hostname Configuration The hostname is the name of your computer. If your computer is attached to a network, this may be assigned by your network administrator.

Type the Host Name you want and select “ OK “

Page 6: Linux p Arctic Al Notes

17.Fire Wall Configuration

A firewall protects against unauthorized network instructions. High security blocks all incoming accesses. Medium blocks access to system services (such as telnet or printing), but allows other connections. No firewall allows all connections and is not recommended.

Security Level : ( ) High ( * ) Medium) ( ) No firewall

OK Customize Back

Select Medium and “ OK “

18.Mouse Selection : If you have serial mouse 3 button then,

Select Generic – 3 Button Mouse (Serial) from the list

[ ] Emulate 3 Buttons? - Leave this blank as it is.

Press “ OK “

19. Device

What device is your mouse located on ?

/dev/ttys0 (Com1 under DOS)

/dev/ttys1 (Com2 under DOS)

/dev/ttys2 (Com3 under DOS)

/dev/ttys3 (Com4 under DOS)

Select the highlighted one if you are not sure on which serial port your mouse is connected. Press “ OK “

20.Language Support : select English (USA) by pressing space bar on that.

[ * ] English (USA) Press “ OK “

21.Time Zone Selection [ ] Hardware clock set to GMT ?

Select from list “ Asia / Calcutta “ and Press “ OK “

22.Root Password : Enter Password and press “ OK “

Page 7: Linux p Arctic Al Notes

23.

Add User

You should use a normal user account for most activities on your system. By not using the root account casually, you will reduce the chance of disrupting your system’s configuration.

User Id : ________________

Password : ___________

Password confirm : ___________

Full Name : ____________

Enter the above mentioned information and press “ OK “

24.

Authentication Configuration

[ * ] Use Shadow password[ * ] Enable MD5 passwords[ ] Enable NIS NIS Domain:_________ NIS server : [ ] Request server via broad cast Or use :__________[ ] Enable LDAP LDAP Server:__________ LDAP base DN: [ ] use TLS connections:[ ] Enable Kerberos Realm :________ KDC :________ Admin Server:__________

Select the default values and press “ OK “

25.

Package Group Selection

[ ]Printer support[ ]X windows system[ ]G Nome[ ]KDE[ ]Mail / www / News Tools[ ]DOS / windows connectivity[ ]Graphics Manipulation[ ]Games[ ]Multimedia support

Page 8: Linux p Arctic Al Notes

[ ]Laptop support[ ]Networked workstation[ ]Dialup workstation[ ]News server[ ]NFS server[ ]SMB (Samba) server[ ]IPX / Netware ™ connectivity[ ]Anonymous FTP server[ ]SQL server[ ]Web server[ ]DNS Name server[ ]Network management workstation[ ]Authoring / Publishing[ ]Emacs[ ]Development[ ]Kernel development[ ]Utilities [ ]Everything

Select the options by pressing space bar and finally press “OK“.

26.

Video Card Selection

Select your video card from the list displayed in this box and then press “ OK “

27.

Installation to Begin

A complete log of your installation will be in /tmp/install.log after rebooting your system. You may want to keep this file for later reference.

28.Formatting will start now followed by copying files. After completing this job. The installation procedure will ask to create boot disk.

29. Insert the Blank floppy into floppy drive and press “ OK “

30. Monitor Probe : Monitor probing found.Do you want to use these settings? - Press “ Yes “

31.Video Memory : Select 1MB

Page 9: Linux p Arctic Al Notes

32.Clockchip Configuration : Select No clockchip settings (Recommended)

33.Probe for clocks : Select “ Probe “

34. Select Video Modes

8 Bit 16 Bit 24 Bit

[ ]1152 x 864

[ ]1024 x 768

[ ]800 x 600

[ ]640 x 480

[ ]800 x 600

[* ]640 x 480

[ ]640 x 480

Select the appropriate resolution by pressing space bar and then press “ OK “.

35.

Starting X

X configuration will now start X to test your configuraton.

Page 10: Linux p Arctic Al Notes

Practical No. 2

1. Understand the working of LINUX (rules), LINUX file system.

2. Create your own user account from the root login. Enter login name, group, home directory and the password.

3. Understand the procedure of logging in and out of Linux.

4. Study the following user commands:

.Passwd .talk

.id .pine

.su .write

.users .df

.who, who am i .du

.clear

.login

5. Study of general purpose utilities: man, help, cal, banner, date,cal, history, tty, stty, echo, bc.

6. Study of directory commands: pwd, dir, cd, mkdir, rmdir

Page 11: Linux p Arctic Al Notes

Practical No 2

Answer 1

Rules for naming and using files:1. LINUX IS CASE-SENSITIVE. Your user login name and password arealso case sensitive. (This goes with the tradition of UNIX and the "c" programming language being case sensitive.)

2. Filenames can be up to 256 characters long and can contain letters, numbers, "." (Dots), "_" (underscores), "-" (dashes), plus some other non-recommended characters.

3. Files with names starting with "." are normally not shown by the ls (list) or dir command. Think of these "dot" files as "hidden". We use ls -a  (list with the option "all") to see these files.

4. "/" is an equivalent to DOS "\" (root directory, meaning the parent of all other directories, or a separator between a directory name anda subdirectory or filename).

5. Under Linux, all directories appear under a single directory tree (there are no DOS-style drive letters).  This means directories and files from all physical devices are merged into this single file system.

6. Linux is a multi-user system. Your personal settings are in your home directory which is /home/your_user_login_name.

7. Under Linux, as in any multi-user operating system, directories and files have an owner and set of permissions. You will typically be allowed to write only to your home directory which is /home/your_user_login_name. Learn to use the file permissions else you will be constantly annoyed with Linux.

Linux file system

Linux treats everything it knows and understands, as a file. All utilities, applications, data in Linux is stored as a file. Even a directory is treated as a file, which contains several other files. The file system, begins with a directory called root, and resembles an upside down tree. The root directory is denoted as (/). Different directories on the root directory are

Page 12: Linux p Arctic Al Notes

/root|

| | | | | | | | | | | | bin boot dev etc usr home lib mnt proc sbin tmp var |

| | | | | user1 user2 .. ..doc .. bin …lib….

1. /bin – contains executable files for essential LINIX commands such as cp, mv, rm, ln etc. It also holds basic user programs, such as login, shells(bash, tcsh, zsh. . . etc.)

2. /boot – contains files used during booting and possibly the kernel itself are stored here.

3. /dev - contains all files related to various devices connected to the system such as terminals, printer, disk drives etc.

4. /etc – contains the files required for system administration purpose such as networking, mail.. etc. It also contains the configuration files for the system, network and server. It contains subdirectories and files such as fstab (mounts file system when you start your system), passwd(contains user password and login configuration),…..etc.shadow(contains user’s encrypted password)5. /usr – contains user related programs and files. In usr directory

there are several directories, each associated with a particular user. The system administrator creates these directories when he creates accounts for different users. Each user is allowed to work with his directory, often called as home directory. It can create subdirectories and files under his home directory.

/usr holds subdirectories such as /bin(holds programs for user’s tasks), /sbin holds user-related system operation, such as user add to add users, /lib holds all the libraries used by the system.

6. /home - home directories of individual users. For each user its home directory is created with his user name when a user account is set up./home also holds subdirectories such as /ftpd(for ftp files), /httpd(for Apche web server web site files)

7. /lib – contains all standard shared system library files/functions.

8. /mnt - typical mount point for many user-mountable devices such as floppy drives, cd-rom readers, etc. Each device is mounted on a subdirectory of /mnt.

9. /proc - virtual file system that provides a number of system statistics.

10./root - home directory for root.11./sbin - location of binaries used for system administration,

configuration, and monitoring.12./tmp - directory specifically designed for programs and users

to store temporary files.13./var - administrative files such as log files, used by various

utilities.

Page 13: Linux p Arctic Al Notes

To connect through Telnet server

Connect to another machine using the TELNET protocol. Use a remote machine name or IP address. You will be prompted for your login name and password--you must have an account on the remote machine to login. Telnet will connect you to another machine and let you operate on it as if you were sitting at its keyboard (almost). Telnet is not very secure--everything you type moves through the networks in open text, even your password!

Managing usersLinuxconf: is a tool used for administrative tasks, including user and file management, as well as network services. It has three interfaces: text, GUI and HTML. The text interface provides cursor-based screens that can be run from shell command line. You use TAB key to move between boxes, lists and buttons. You use the arrow keys to select entries in a list. For GUI interface go to X window System interface that runs on any window manager or desktop. HTML interface is a web page interface that lists options as inks to other web pages. You can download the current version from the Linuxconf Web site at www.soulcorp.qc.ca/linuxconf.

You can easily add, remove, or change users with linuxconf: 1. Login as root user.2. Use command linuxconf to go to the text interface.3. Select user accounts in the normal list under the User accounts

heading under Config. This displays a panel listing all your user accounts, including those used for special purposes.

4. To add a new user, click add on this panel. This displays a User information panel.

5. Enter the login name, group, and user’s home directory. The root user assigns the permission at this stage, If not assigned, the minimum possible permissions will be assigned.

6. Click Accept. To give the user initial password, click Passwd. A changing Password panel is displayed where you can enter the new password.

7. Retype new UNIX password, click Accept. (It is possible to change the password). You now see the new user displayed in the user accounts panel. If you need to change or delete a user, double- click it entry in this panel to display the User information panel. To remove the user, click Del. To edit the other entries make changes at the appropriate entries and click Accept.8. Quit enter 3 times. (It is not possible to add user if you log

in as normal user. The user’s entry is made in /home as well as in a file called passwd under /etc directory.)

Answer 2 and 3:

As given in Practical 1

Answer 4:

Page 14: Linux p Arctic Al Notes

User Commands for root user

Command Description and syntax

Finger Display information about a user# finger

Passwd Change a user's password# passwd username

Su Change to another user id# su username

Useradd Add a new user to the system

Userdel Delete a user from the system

ShutdownTo shut down or reboot the system# shutdown# shutdown –r now

Halt halt the system

Initset runlevel, or define processes that are begun on a specific runlevel# init 0

Lilo install the LILO boot loader

poweroff power the system off'

Reboot reboot the system

Runlevel show the current system runlevel

User Commands for normal as well as root user

Command Description and syntax

Passwd

Change a user's password# passwd usename$ passwd usenamepassword:

IdDisplay information about a user# id$ id

SuChange to root user id$ su rootuser/usernamepassword:

users Display a list of current users# users

whowho am i

Display a list of current users as follows:Login name terminal number date &time Or the serial port

Page 15: Linux p Arctic Al Notes

Line by which your Terminal is connected To the host machinee.g. sup1 tty1 Dec 30 ……..# who or $ who# who am I or $ who am i

clear Clears the screen$ clear

login Initiate user login$ login

talk Communicate between the two users: to talk

pine Communicate between the two users: to send the message

write Communicate between the two users: to send the message

df Display information about file system

du Display information about disk usage

General Purpose Utilities

Command Purpose Syntax

man To get the on-line help. Offers help on almost all the topics related to linux.

$man exprhere expr denotes the command name for which you need the help

help To get the on-line help, but restricted to very few commands.

$help exprhere expr denotes the command name for which you need the help

banner To create a poster by blowing up its arguments on the keyboard. On each line it can display atleast ten characters.

$banner exprOR$/usr/games/banner exprexpr denotes the expression which you display as the banner.$banner –w n exprdisplays the banner of size n, where n is some positive integer.

date To get the current date and time to the nearest second For specific format each

format is proceeded by a +

symbol and the % operator

$date

$date +%d $date +%m or +%h

Page 16: Linux p Arctic Al Notes

To get the dayTo get the month in the number or name format.

To get the year

To get the date in the format mm/dd/yyTo get the time in the format hr:min:secTo get the hoursTo get the minutesTo get the seconds

To couple more than one formats at a time enclose them in single or double quotes and write + symbol before

$date +%y

$date +%D

$date +%T

$date +%D$date +%M$date +%S

e.g $date +”%H:%M:%S”or $date +’%H:%M:%S’displays the time in the format hr:min:secdate +"%D%n%T”displays the date and and time on two different lines when %n is used between them.

cal To print the calendar of the current month of the current yearTo print the calendar for a particular yearTo print the calendar for a particular month of a year.

$cal

$cal year

$cal month yearwhere month and year denote the number of the month and year denotes the year.

tty To tell the name of the terminal(Linux treats even the terminal as file)

$ttygives /dev/tty1which means the terminal name is tty1 in the /dev.(tty means the teletype command.)

stty Used to set the terminal characteristics according to user’s choice.

$stty –athe output consists of the baud rate, the parameters , a series of keywords or options with a – preceded to some of them. When an option doesn’t have the -, it means the option is turned on. It is possible to use stty to set or unset these options. Also it can be used to set the functions for some of the keys e.g

Page 17: Linux p Arctic Al Notes

to change the interrupt key, to change the eof, eol character.

history Every command has an event number associated with it. By default, Korn shell stores all previous commands. To see all the commands entered from the very beginning

$history

$history nprints last n number of commands typed.

echo To echo/display the expression required

$echo exprdisplays the expr as it is.

bc Linux provides two calculators : bc and xcalcTo do the mathematicalcalculations with truncation.To do the calculations with decimals.

$bc or $bc –qto get the clear bc$bc –qscale = 2 to set the scale to get the truncation upto 2 decimal places while doing the calculations.

Directory commands

pwd When you are logged in, you are placed in a specific directory of the file system. Rhis directory is called the current directory. To know the current working/home directory along with its pathname and parent directory.

$pwdor$echo $HOME

dir To see the contents of the directory

$dirwhich displays all the files and directories$dir –adisplays all the files and directories including the hidden files beginning with . and .. The symbols . and .. are used universally to represent the current and parent directory respectively.

cd To change to the parent $cd

Page 18: Linux p Arctic Al Notes

directory

To change to some other directory say dir1

$cd dir1

mkdir To create a new directory with name dir_nameTo create the chain of directories

$mkdir dir_name

$mkdir –p dir1/dir2/dir3creates dir3 inside dir2 inside dir1 on current working directory at once.

rmdir To remove already created directory with name dir_nameTo remove the chain of directories

$rmdir dir_name

$mkdir –p dir1/dir2/dir3removes dir3, dir2 and dir1 at once.

Page 19: Linux p Arctic Al Notes

Practical No.3

Basic Linux Commands

Part(I): File System Commands: touch, cat, cp, rm, mv, mkdir, cd, rmdir.

Do the following and enter the related commands in your journal.

1. Create five files with the name a1, b2, c3, d4, e5 by using touch

command.

2. Create five files with the name f6, g7, h8 using cat command with

some meaningful contents with at least five lines each.

3. Concatenate the contents of f6 and g7 to a file called new1.

(Without creating new1)

4. Append the contents of f6 and h8 to new1.

5. Copy contents of f6 to a1, g7 to b2, h8 to c3 by using cp command.

6. Create two directions with the name dd1, dd2.

7. Copy the files a1 and b2 to the directory dd1 by using cp

command. Copy the files f6, g7 to the director dd2.

8. Remove the files a1, b2 from dd1.

9. Remove the directory dd2 along with its contents.

10. Rename the files f6, g7, h8, by newf6, newg7, newh8, using mv

command.

11.Move the files newf6, newg7 the directory dd1.

Part II : ls and ls with options

Change the directory to /bin and do the following and enter the

commands in your journal.

(i) list all filenames.

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

(iii) list all filenames with 2 characters, 3 characters.

(iv) List all filenames with 2 and 4 characters at the same time.

(v) List all filenames starting with vowel.

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

d.

(vii) List all filenames with exactly three characters in which the

second character is a vowel.

Page 20: Linux p Arctic Al Notes

(viii) List all filenames starting with character ‘a’.

(ix) List all 4 character filenames whose first character is ‘a’

and the third character is ‘b’.

(x) List all filenames whose first character is ‘m’ or ‘r’ or is

in the range c to f or v to z.

(xi) List all filenames whose first character is any thing other

than an alphabet in the range d to m.

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

exactly three characters in their filename.

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

exactly two or three or four characters in their filename.

Change the directory to your home directory and do the

following

(xiv) List the contents of the directory.

(xv) List the contents of the directory along with all hidden

files.

(xvi) List the contents of the directory along with all hidden

files except . and .. files.

(xvii) List all files with their attributes and file permissions.

(xviii) List all files identifying direct6ories and executable files.

(xix) List all the files showing the size of each file rounded up

to the nearest kilobyte.

(xx) List all the files according to file size.

(xxi) Give the file listing displayed in columns.

(xxii) Give the file listing in reverse order.

(xxiii) Give the file listing where all the files are in long format

showing full file details.

Page 21: Linux p Arctic Al Notes

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.[tybsc308@linserver tybsc308]$ 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[tybsc308@linserver tybsc308]$ cat> f6This is file f6this is cat command.hello.hi.tybsc.

[tybsc308@linserver tybsc308]$ cat> g7thid is file g7hello.hi.tybsc.this is cat cmd.

[tybsc308@linserver tybsc308]$ cat> h8this is file h8hello.hi.tybsc.this is cat cmd.

Q:3) Concatenate the contents of f6 and g7 to a file called new1.(Without creating new1)[tybsc308@linserver tybsc308]$ cat f6 g7 >>new1[tybsc308@linserver tybsc308]$ cat new1This is file f6this is cat command.hello.hi.tybsc.thid is file g7hello.hi.tybsc.this is cat cmd.

Q:4) Append the contents of f6 and h8 to new1. [tybsc308@linserver tybsc308]$ cat f6 h8 >> new1[tybsc308@linserver tybsc308]$ cat new1This is file f6this is cat command.

Page 22: Linux p Arctic Al Notes

hello.hi.tybsc.thid is file g7hello.hi.tybsc.this is cat cmd.This is file f6this is cat command.hello.hi.tybsc.this is file h8hello.hi.tybsc.this is cat cmd.

Q:5) Copy contents of f6 to a1, g7 to b2, h8 to c3 by using cp command.[tybsc308@linserver tybsc308]$ cp f6 a1[tybsc308@linserver tybsc308]$ cat a1This is file f6this is cat command.hello.hi.tybsc.

[tybsc308@linserver tybsc308]$ cp g7 b2[tybsc308@linserver tybsc308]$ cat b2thid is file g7hello.hi.tybsc.this is cat cmd.

[tybsc308@linserver tybsc308]$ cp h8 c3[tybsc308@linserver tybsc308]$ cat c3this is file h8hello.hi.tybsc.this is cat cmd.

Q:6) Create two directions with the name dd1 and dd2.[tybsc308@linserver tybsc308]$ mkdir dd1[tybsc308@linserver tybsc308]$ mkdir dd2

Page 23: Linux p Arctic Al Notes

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.[tybsc308@linserver tybsc308]$ cp a1 b2 dd1[tybsc308@linserver tybsc308]$ ls dd1a1 b2

[tybsc308@linserver tybsc308]$ cp a1 b2 dd1[tybsc308@linserver tybsc308]$ ls dd1a1 b2

Q:8) Remove the files a1, b2 from dd1.[tybsc308@linserver tybsc308]$ rm dd1/a1[tybsc308@linserver tybsc308]$ ls dd1b2

[tybsc308@linserver tybsc308]$ rm dd1/b2[tybsc308@linserver tybsc308]$ ls dd1

Q:9) Remove the directory dd2 along with its contents. [tybsc308@linserver tybsc308]$ rm -r dd2

Q:10) Rename the files f6, g7, h8, by newf6, newg7, newh8, using mv command.[tybsc308@linserver tybsc308]$ mv f6 newf6[tybsc308@linserver tybsc308]$ mv g7 newg7[tybsc308@linserver tybsc308]$ mv h8 newh8

Q:11) Move the files newf6, newg7 to the directory dd1.[tybsc308@linserver tybsc308]$ mv newf6 newg7 dd1[tybsc308@linserver tybsc308]$ ls dd1newf6 newg7

Page 24: Linux p Arctic Al Notes

Part II: ls and ls with options

Change the directory to /bin and do the following and enter the commands in your journal.(i) List all filenames.[tybsc308@linserver tybsc308]$ cd /bin[tybsc308@linserver bin]$ lsarch cut gawk ls red tcshash date gettext mail rm touchash.static dd grep mkdir rmdir trueaumix-minimal df gtar mknod rpm umountawk dmesg gunzip mktemp rvi unamebasename dnsdomainname gzip more rview unicode_startbash doexec hostname mount sed unicode_stopbash2 domainname igawk mt setfont unlinkbsh dumpkeys ipcalc mv setserial usleepcat echo jpeg-6b netstat sh vichgrp ed kbd_mode nice sleep viewchmod egrep kill nisdomainname sort ypdomainnamechown env link pgawk stty zcatcp ex ln ping sucpio false loadkeys ps synccsh fgrep login pwd tar

(ii) List all filenames with one screen at a time.[tybsc308@linserver bin]$ ls |morearchashash.staticaumix-minimalawkbasenamebashbash2bshcatchgrpchmod

Page 25: Linux p Arctic Al Notes

chowncpcpiocshcutdatedddfdmesgdnsdomainnamedoexec--More—

(iii) List all filenames with 2 characters, 3 characters.[tybsc308@linserver bin]$ ls ??cp dd df ed ex ln ls mt mv ps rm sh su vi

[tybsc308@linserver bin]$ ls ???ash awk bsh cat csh cut env pwd red rpm rvi sed tar

(iv) List all filenames with 2 characters and 4 characters at the same time.[tybsc308@linserver bin]$ ls ?? ????arch cpio df ex gtar link mail mv ps sort sync vibash date echo gawk gzip ln more nice rm stty tcsh viewcp dd ed grep kill ls mt ping sh su true zcat

(v) List all filenames starting with vowel [tybsc308@linserver bin]$ ls [aeiou]*arch ash.static awk ed env igawk umount unicode_start unlinkash aumix-minimal echo egrep ex ipcalc uname unicode_stop usleep

(vi) List all filenames with the last character as a or b or c or d.[tybsc308@linserver tybsc308]$ ls *[abcd]fspaa fspac newsed prime.c xaa xacfspab masterdata prime1.c transdata xab

(vii) List all filenames with exactly three characters in which the second character is a vowel.[tybsc308@linserver bin]$ ls ?[aeiou]?cat cut red sed tar

(viii) List all filenames starting with character ‘a’.[tybsc308@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’.[tybsc308@linserver bin]$ ls [a]?[b]?ls: [a]?[b]?: No such file or directory

Page 26: Linux p Arctic Al Notes

(x) List all filenames whose first character is ‘m’ or ‘r’ or is in the range c to f or v to z.[tybsc308@linserver bin]$ ls [mrc-fv-z]*cat cpio df dumpkeys ex mknod mv rvi zcatchgrp csh dmesg echo false mktemp red rviewchmod cut dnsdomainname ed fgrep more rm vichown date doexec egrep mail mount rmdir viewcp 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.[tybsc308@linserver bin]$ ls [!d-m]*arch cat nice rpm stty unicode_startash chgrp nisdomainname rvi su unicode_stopash.static chmod pgawk rview sync unlinkaumix-minimal chown ping sed tar usleepawk cp ps setfont tcsh vibasename cpio pwd setserial touch viewbash csh red sh true ypdomainnamebash2 cut rm sleep umount zcatbsh netstat rmdir sort uname

(xii) Construct a command to display the total number of files with exactly three characters in their filename.[tybsc308@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.[tybsc308@linserver bin]$ ls ?? ??? ????|wc -l 49

Change the directory to the home directory and do the following(xiv) List the contents of the directory.[tybsc308@linserver tybsc308]$ ls dir1f1 file1

(xv) List the contents of the directory along with all hidden files.[tybsc308@linserver tybsc308]$ ls -a dir1. .. f1 file1

Page 27: Linux p Arctic Al Notes

(xvi) List the contents of the directory along with all hidden files except . and .. files.[tybsc308@linserver tybsc308]$ ls -A+1 copy1 fcmp1_308 green pract7.10 pract7.7 prime1.out Student date fcmp2 .gtkrc pract7.11 pract7.9 prime.c Student308 ashwini date1 fcmp2_308 .kde pract7.12 pract8.1 .prime.c.swo t4 .bash_history dir1 fcmp3_308 line pract7.13 pract8.11.2 prime.out transdata .bash_logout dir2 fex1 login pract7.15 pract8.12.3 product_308 try2.bash_profile dir3 .file loop pract7.17 pract8.14 result tryTime .bashrc doll file2 masterdata pract7.18 pract8.3 result1 .viminfo bdata308 .emacs fod1 merit308 pract7.3 pract8.5 Shweta308 yellowcheck emp foreg month pract7.3.1 pract8.6 ss1 checkgrade empdata308 fsp308 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.[tybsc308@linserver tybsc308]$ ls -ltotal 204-rw-r--r-- 1 tybsc308 tybsc 159 Dec 7 10:52 +1-rw-r--r-- 1 tybsc308 tybsc 16 Jan 18 09:57 ashwini-rw-r--r-- 1 tybsc308 tybsc 193 Jan 18 08:39 check-rw-r--r-- 1 tybsc308 tybsc 307 Jan 10 15:51 Stud308

...

...-rw-r--r-- 1 tybsc308 tybsc 139 Jan 19 11:53 Student-rw-r--r-- 1 tybsc308 tybsc 881 Jan 4 08:58 t1-rw-rw-r-- 1 tybsc308 546 881 Dec 14 09:52 try1-rw-rw-r-- 1 tybsc308 546 28 Dec 30 10:15 white-rw-rw-r-- 1 tybsc308 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.[tybsc308@linserver tybsc308]$ ls -stotal 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 4 checkgrade 4 fact 4 pink 4 pract7.3.1 4 Stud308

Page 28: Linux p Arctic Al Notes

4 commandfor 4 file2 4 pract 4 pract7.4 4 Student 4 date 4 gre1 4 pract7.11 0 pract7.7 4 white

...

(xx) List all the files according to file size.[tybsc308@linserver tybsc308]$ ls -Sprime.out prime.c month fact fcmp3_308 bdata308 fcmp1_308 pract7.18 prime1.out pract8.14 pract7.11 +1 loop merit308 commandline pract7.3 dir1 pract7.13 pract7.12 pract8.12.3 Student308 transdata fod1 10000dir2 pract8.5 pract7.10 pract8.8 pract7.17 file2 doll date1 dir3 prime1.c checkgrade Student line result pract7.1 pract7.7 result1 tryTime pract7.9 pract7.4 ss3 pract8.1 yellow fsp308 pract7.3.1 date Shweta308 masterdata emp greenlogin pract8.6 pract7.15 pract8.7 names fcmp2_308 commandfor t4 pract8.11.2 empdata308 pract7.6 pract7.5 fcmp2 ss2 try2 gre2 copy1 gre1 foreg product_308 pract8.3 fex1 Stud308 check pract fcmp1 ss1 ashwini

(xxi) Give the file listing displayed in columns.[tybsc308@linserver tybsc308]$ ls -cgre2 date1 pract7.18 check ss1 pract7.1 white gre1 commandfor ashwini emp checkgrade fsp308 green +1 pract8.3 foreg ss3 pract7.13 pract7.7 Stud308 ss2 dir2 dir3Student pract7.15 pract7.12 doll names t1 login dir1 pract7.11 pract7.17 fact pract7.6 copy1 commandline try1 pract8.1 pract pract7.10 pract7.4 pract7.3.1 file2 yellow date loop pract7.9 pract7.5 pract7.3 pink line

(xxii) Give the file listing in reverse order.[tybsc308@linserver tybsc308]$ ls -ryellow ss2 pract7.5 pract7.13 names fsp308 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 Stud308 pract7.7 pract7.17 pract gre2 doll commandline ss3 pract7.6 pract7.15 pink gre1 dir3 commandfor

Page 29: Linux p Arctic Al Notes

(xxiii) Give the file listing where all the files are in long format showing full size details.[tybsc308@linserver tybsc308]$ ls -ltotal 204-rw-r--r-- 1 tybsc308 tybsc 159 Dec 7 10:52 +1-rw-r--r-- 1 tybsc308 tybsc 16 Jan 18 09:57 ashwini-rw-r--r-- 1 tybsc308 tybsc 193 Jan 18 08:39 check-rw-r--r-- 1 tybsc308 tybsc 243 Jan 18 08:38 checkgrade-rw-r--r-- 1 tybsc308 tybsc 31 Jan 18 11:26 commandfor-rw-r--r-- 1 tybsc308 tybsc 56 Jan 4 08:57 commandline-rw-r--r-- 1 tybsc308 tybsc 212 Jan 7 11:56 copy1-rw-r--r-- 1 tybsc308 tybsc 220 Jan 18 11:48 date-rw-r--r-- 1 tybsc308 tybsc 0 Jan 18 11:32 date1drwxrwxr-x 2 tybsc308 546 4096 Nov 30 09:44 dir1drwxrwxr-x 3 tybsc308 546 4096 Dec 14 10:08 dir2drwxrwxrwx 2 tybsc308 546 4096 Dec 7 09:07 dir3-r-xr-xr-x 1 tybsc308 tybsc 47 Jan 4 08:24 doll

. . .-rw-r--r-- 1 tybsc308 tybsc 41 Jan 4 10:18 pract7.1-rw-r--r-- 1 tybsc308 tybsc 260 Jan 18 09:10 pract7.10-rw-r--r-- 1 tybsc308 tybsc 299 Jan 18 09:24 pract7.11-rw-r--r-- 1 tybsc308 tybsc 286 Jan 18 09:33 pract7.12-rw-r--r-- 1 tybsc308 tybsc 419 Jan 18 09:56 pract7.13-rw-r--r-- 1 tybsc308 tybsc 220 Jan 18 11:03 pract7.15-rw-r--r-- 1 tybsc308 tybsc 115 Jan 18 10:49 pract7.17-rw-r--r-- 1 tybsc308 tybsc 3 Jan 18 10:26 pract7.18-rw-r--r-- 1 tybsc308 tybsc 1 Jan 4 10:32 pract7.3-rw-r--r-- 1 tybsc308 tybsc 360 Jan 4 10:49 pract7.3.1-rw-r--r-- 1 tybsc308 tybsc 138 Jan 18 08:10 pract7.4-rw-r--r-- 1 tybsc308 tybsc 103 Jan 18 08:02 pract7.5-rw-r--r-- 1 tybsc308 tybsc 128 Jan 18 08:12 pract7.6-rw-r--r-- 1 tybsc308 tybsc 0 Jan 18 08:33 pract7.7-rw-r--r-- 1 tybsc308 tybsc 221 Jan 18 08:43 pract7.9-rw-r--r-- 1 tybsc308 tybsc 79 Jan 19 11:18 pract8.1-rw-rw-r-- 1 tybsc308 546 28 Dec 30 10:15 white-rw-rw-r-- 1 tybsc308 546 36 Dec 14 09:09 yellow

Page 30: Linux p Arctic Al Notes

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 (ii) /usr/sbin beginning with a lowercase ‘c’.(iii) /usr/sbin in capital letters beginning with a lowercase

‘c ’ (Use single-quote for tr command)(iv) /usr sbin which are over 5k in size in uppercase.

2. Display Parts of Files using head or tail command:

(i) Display and count all the lines in the file /etc/mime.types

(ii) Display the first 10 lines of the file/etc/mime.types (iii) Display the last 10 lines of /etc/mime.types(iv) Display the first 25 lines of /etc/mime.types

3. Classify, Count and Compare Files

a. Find out what file types you have in the following directories:i. /etc

ii. /binb. Repeat the previous question, but this time:

i. Re-direct /etc listing to new file etcfiles.txtii. Append the listing for /usr/bin to etcfiles.txt

c. Construct a command to find out how many files are in the /usr/bin directory.

4. Sorting:

a. Sort the etcfiles.txt file into reverse alphabetical order on the first filed. You may notice that capital and lowercase letters are sorted

independently, e.g. ‘A’ comes before ‘a’.b. Repeat the first sorting exercise but ignoring case differences c. Sort the etcfiles.txt files into alphabetical order on the

second field (the file type).d. Find out how many English text files are listed in the

etcfiles.txt file.

5. Create a file using vi editor with the following contentsMahesh Deshpande 234Naresh Nair 431Allen Disuza 121Hari Kutian 231

Page 31: Linux p Arctic Al Notes

Ramesh Dubey 231Akshay Das 256

(i) Sort on the first names only(ii) Sort on last names only(iii) Sort on first 4 characters only(iv) Sort on their numbers only.

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 having the below mentioned values:

Field RollNo First Name Last Name Date of Birth Marks Values Numeric Character Character dd-mm-yy Numeric out of 600(i) Insert at least five appropriate records and do

the following:(ii) Sort the data on first names only.(iii) Sort the data on the Marks only(iv) 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.

Write down the commands and attach the printout of the commands and their the corresponding output in your answer sheet.

7. Construct the commands and execute them to(i) 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).

(ii) Display first 2 lines of fsp<seat_no> and convert all the characters into capital letters.(iii) Display the last 15 lines of fsp<seat_no>.(iv) Display the lines starting with a vowel.(v) 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.

(vi) 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.

Page 32: Linux p Arctic Al Notes

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.[tybsc308@linserver tybsc308]$ 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’.[tybsc308@linserver tybsc308]$ 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

Page 33: Linux p Arctic Al Notes

(iii) /usr/sbin in capital letters beginning with a lowercase ‘c’.(Use single-quote for tr command)[tybsc308@linserver tybsc308]$ 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. [tybsc308@linserver tybsc308]$ 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[tybsc308@linserver tybsc308]$ wc -l /etc/mime.types 480 /etc/mime.types

(ii) Display the first 10 lines of the file/etc/mime.types[tybsc308@linserver tybsc308]$ 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/>.

Page 34: Linux p Arctic Al Notes

(iii) Display the last 10 lines of the /etc/mime.types[tybsc308@linserver tybsc308]$ tail -10 /etc/mime.typesvideo/vnd.fvtvideo/vnd.motorola.videovideo/vnd.motorola.videopvideo/vnd.mpegurl mxuvideo/vnd.mtsvideo/vnd.nokia.interleaved-multimediavideo/vnd.vivovideo/x-msvideo avivideo/x-sgi-movie moviex-conference/x-cooltalk ice

(iv) Display the first 25 lines of the /etc/mime.types[tybsc308@linserver tybsc308]$ 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 2045, 2046, 2047, 2048, and 2077. The Internet media type# registry is at <http://www.iana.org/assignments/media-types/>.

# MIME type Extensionapplication/EDI-Consentapplication/EDI-X12application/EDIFACTapplication/activemessageapplication/andrew-inset ezapplication/applefileapplication/atomicmailapplication/batch-SMTPapplication/beep+xmlapplication/cals-1840application/commongroundapplication/cybercashapplication/dca-rftapplication/dec-dx

Page 35: Linux p Arctic Al Notes

3)Classify,Count and Compare Filesa) Find out what file types you have in the following directories:(i) /etc[tybsc308@linserver tybsc308]$ 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[tybsc308@linserver tybsc308]$ file /usr/bin/*/usr/bin/zip: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), for GNU/Linux 2.2.5, dynamically linked (uses shared libs), stripped/usr/bin/zipcloak: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), for GNU/Linux 2.2.5, dynamically linked (uses shared

Page 36: Linux p Arctic Al Notes

libs), stripped/usr/bin/zipgrep: Bourne shell script text executable/usr/bin/zipinfo: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), for GNU/Linux 2.2.5, dynamically linked (uses shared libs), stripped/usr/bin/zipnote: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), for GNU/Linux 2.2.5, dynamically linked (uses shared libs), stripped/usr/bin/zipsplit: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), for GNU/Linux 2.2.5, dynamically linked (uses shared libs), stripped/usr/bin/zless: Bourne shell script text executable/usr/bin/zmore: Bourne shell script text executable…..

b)Repeat the previous question, but this time:(i) Re-direct /etc listing to new file etcfiles.txt[tybsc308@linserver tybsc308]$ file /etc/* > etcfiles.txt[tybsc308@linserver tybsc308]$ 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/etc/cdrecord.conf: ASCII English text/etc/cipe: directory/etc/CORBA: directory

Page 37: Linux p Arctic Al Notes

/etc/cron.d: directory/etc/cron.daily: directory/etc/cron.hourly: directory/etc/cron.monthly: directory

(ii) Append the listing for /usr/bin to etcfiles.txt[tybsc308@linserver tybsc308]$ file /usr/bin/* >> etcfiles.txt

c) Construct a command to find out how many files are in the /usr/bin directory.[tybsc308@linserver tybsc308]$ file /usr/bin/* | wc –l 2161

4) Sortinga) Sort the etcfiles.txt into reverse alphabetical order on the first file.[tybsc308@linserver tybsc308]$ 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, Intel 80386, version 1 (SYSV), for GNU/Linux 2.2.5, dynamically linked (uses shared libs), stripped/usr/bin/zipnote: ELF 32-bit LSB executable, Intel 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.[tybsc308@linserver tybsc308]$ 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 executable/usr/bin/znew: Bourne shell script text executable/usr/bin/zmore: Bourne shell script text executable/usr/bin/zmore: Bourne shell script text executable/usr/bin/zless: Bourne shell script text execu

Page 38: Linux p Arctic Al Notes

table/usr/bin/zless: Bourne shell script text executable/usr/bin/zipsplit: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), for GNU/Linux 2.2.5, dynamically linked (uses shared libs), stripped/usr/bin/zipsplit: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), for GNU/Linux 2.2.5, dynamically linked (uses shared libs), stripped/usr/bin/zipnote: ELF 32-bit LSB executable, Intel 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[tybsc308@linserver tybsc308]$ 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 executable/usr/bin/libglade-xgettext: a python script text executable/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

d) Find out how many English text files are listed in the etcfiles.txt file[tybsc308@linserver tybsc308]$ grep English etcfiles.txt | wc -l 67

Page 39: Linux p Arctic Al Notes

5) Create a file using vi editor with the following contents.[tybsc308@linserver tybsc308]$ cat namesMahesh Deshpande 234Naresh Nair 431Allen Disuza 121Hari Kutian 231Ramesh Dubey 231Akshay Das 256

(i) Sort on the first names only[tybsc308@linserver tybsc308]$ sort +0 -1 namesAkshay Das 256Allen Disuza 121Hari Kutian 231Mahesh Deshpande 234Naresh Nair 431Ramesh Dubey 231

(ii) Sort on the last names only[tybsc308@linserver tybsc308]$ sort +1 -2 namesAkshay Das 256Mahesh Deshpande 234Allen Disuza 121Ramesh Dubey 231Hari Kutian 231Naresh Nair 431

(iii) Sort on first four characters only[tybsc308@linserver tybsc308]$ cut -c 1-4 names | sortAkshAlleHariMaheNareRame

(iv) Sort on their numbers only[tybsc308@linserver tybsc308]$ sort +2 -3 namesAllen Disuza 121Hari Kutian 231Ramesh Dubey 231Mahesh Deshpande 234Akshay Das 256Naresh Nair 431

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)[tybsc308@linserver tybsc308]$ cat Stud308RollNo FirstName LastName DateOfBirth Marks308 Shweta Choudhary 7-12-1985 450308 Mildred D'mello 9-12-1985 500

Page 40: Linux p Arctic Al Notes

308 Megha Menon 14-10-1985 550308 Vikrant Mahkal 12-5-1985 500308 Sankalp Naik 11-5-1985 550

(ii) Sort the data on first names only[tybsc308@linserver tybsc308]$ sort +1 -2 Stud308RollNo FirstName LastName DateOfBirth Marks308 Megha Menon 14-10-1985 550308 Mildred D'mello 9-12-1985 500308 Sankalp Naik 11-5-1985 550308 Shweta Choudhary 7-12-1985 450308 Vikrant Mahkal 12-5-1985 500

(iii) Sort the data on the Marks only[tybsc308@linserver tybsc308]$ sort +4 Stud308308 Shweta Choudhary 7-12-1985 450308 Mildred D'mello 9-12-1985 500308 Vikrant Mahkal 12-5-1985 500308 Megha Menon 14-10-1985 550308 Sankalp Naik 11-5-1985 550RollNo FirstName LastName DateOfBirth Marks

7) Construct the commands and execute them to(i) Create a file named fsp<seat_no> having of atleast 50 lines[tybsc308@linserver tybsc308]$ touch fsp308[tybsc308@linserver tybsc308]$ head -50 /etc/mime.types > fsp308[tybsc308@linserver tybsc308]$ cat fsp308# 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/>.

# MIME type Extensionapplication/EDI-Consent

...application/news-message-idapplication/news-transmissionapplication/ocsp-requestapplication/ocsp-response

(ii) Display first two lines of fsp<seat_no> and convert all the characters into capital letters

Page 41: Linux p Arctic Al Notes

[tybsc308@linserver tybsc308]$ head -2 fsp308 | tr '[a-z]' '[A-Z]'# THIS IS A COMMENT. I LOVE COMMENTS.

(iii) Display the last 15 lines of fsp<seat_no>[tybsc308@linserver tybsc308]$ tail -15 fsp308application/iotpapplication/ippapplication/isupapplication/font-tdpfrapplication/mac-binhex40 hqxapplication/mac-compactpro cptapplication/macwriteiiapplication/marcapplication/mathematicaapplication/mathematica-oldapplication/msword docapplication/news-message-idapplication/news-transmissionapplication/ocsp-requestapplication/ocsp-response

(iv) Display the lines starting with a vowel[tybsc308@linserver tybsc308]$ grep ^[aeiouAEIOU] fsp308application/EDI-Consentapplication/EDI-X12application/EDIFACTapplication/activemessageapplication/andrew-inset ezapplication/applefileapplication/atomicmailapplication/batch-SMTPapplication/beep+xmlapplication/cals-1840application/commongroundapplication/cybercashapplication/dca-rftapplication/dec-dxapplication/dvcsapplication/eshopapplication/httpapplication/hyperstudioapplication/igesapplication/indexapplication/index.cmd

. . .application/mathematica-oldapplication/msword docapplication/news-message-idapplication/news-transmissionapplication/ocsp-requestapplication/ocsp-response

Page 42: Linux p Arctic Al Notes

(v) Split a 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[tybsc308@linserver tybsc308]$ split -20 fsp308[tybsc308@linserver tybsc308]$ ls+1 date1 fcmp3_308 line t4 10000 dir1 fcut1 login ashwini dir2 fcut2 loop product_308 try2 bdata308 dir3 fex1 masterdata result tryTime check doll file2 merit308 result1 xaa emp fod1 month pract7.18 Shweta308 xab empdata308 foreg names pract7.3 ss1 xac commandline fact fsed1 newsed ss2 yellow copy1 fcmp1 fsp308 newsedclear ss3 cutlist1 fcmp1_308 gre1 pract cutlist2 fcmp2 gre2 pract7.1 pract7.6 prime1.c date fcmp2_308 green pract7.10 pract7.7 prime1.out Student308

[tybsc308@linserver tybsc308]$ cat xaa | wc -l 20[tybsc308@linserver tybsc308]$ cat xab | wc -l 20[tybsc308@linserver tybsc308]$ cat xac | wc -l 10

Page 43: Linux p Arctic Al Notes

(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[tybsc308@linserver tybsc308]$ split -20 fsp308 fsp[tybsc308@linserver tybsc308]$ ls+1 date1 fcmp3_308 line t4 10000 dir1 fcut1 login ashwini dir2 fcut2 loop product_308 try2 bdata308 dir3 fex1 masterdata result tryTime check doll file2 merit308 result1 xaa emp fod1 month pract7.18 Shweta308 xab empdata308 foreg names pract7.3 fspaa xac commandline fact fsed1 newsed fspab yellow copy1 fcmp1 fsp308 newsedclear fspac cutlist1 fcmp1_308 gre1 pract cutlist2 fcmp2 gre2 pract7.1 pract7.6 prime1.c date fcmp2_308 green pract7.10 pract7.7 prime1.out Student308

[tybsc308@linserver tybsc308]$ cat fspaa | wc -l 20[tybsc308@linserver tybsc308]$ cat fspab | wc -l 20[tybsc308@linserver tybsc308]$ cat fspac | wc -l 10

Page 44: Linux p Arctic Al Notes

Practical No. 5

1. od, cmp, comm, diff, uniq:

Create a file named fod1 with some contentshaving the following contents and display it in (i) octal form only and (ii) octal form along with its text contents.

2. Construct the commands to

a. Create a file fcmp1<seat_no> with six lines containing six names. b. Add two more names and save the contents in fcmp2<seat_no>. c. Sort the contents of file fcmp1<seat_no> . d. Display the names, which are common to fcmp1 <seat_no>, and

fcmp2<seat_no>.e. Display the difference between fcmp1 <seat_no> and fcmp2<seat_no>.f. Append the contents of fcmp1 <seat_no> to fcmp2<seat_no> and store it in

the file fcmp3<seat_no>. g. Sort the contents of fcmp3<seat_no> and display the contents without any

duplicate lines.

3. Create two files named fcmp1 and fcmp2 which consists of at least five lines with two or three similar lines. Construct the commands A. using cmp (i) to check whether the files differ (ii) to compare the two files byte by byte.

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 (ii) to display the difference in context output format (iii) to display the unified output format.

C. Using comm (i) To compare the files fcmp1 and fcmp2

(ii) To display the lines which are unique to fcmp1 and fcmp2(iii) To display the lines which are common to the fcmp1 and fcmp2.

D. Using nl to give the line numbers to lines in fcmp1.

E. Using cp to append the fcmp1 to fcmp2 and sort this appended fcmp2 and store it in a file named funiq1.

Page 45: Linux p Arctic Al Notes

F. Using uniq (i) to remove the duplicate lines in funiq1. (ii) to count the duplications and prepend number to

each line (iii) to display the duplicate lines only (iv) to display unique lines only.

Write down the commands and attach the printout of thecommands and their the corresponding output in your answer sheet.

4. Create a file named name<roll_no> with fields (first name, second name, last name, salary) separated by “ : ”.Insert at least five appropriate records in above file.

Perform following sort operations:

(i) Sort on first names only.(ii) Display only those records whose first names start

with a vowel.(iii) Sort on last names only.(iv) Display the names with salary above 10000 and add two more records and redirect the output to the file named namenew<roll_no>.

Write the commands in the answer sheet, execute and attach the printout of the commands with their output.

5. Cut, paste, tr

Create two files with at least three fields(columns) each with the names fcut1, fcut2 and do the following:

1. Cut first two columns from fcut1 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.

Translate the first three lines into capital letters using tr command.

Page 46: Linux p Arctic Al Notes

Practical No.5

1) od,cmp,comm.,diff,uniq:Create a file named fod1 with some contents having the following contents and display it in(i) octal form only[tybsc308@linserver tybsc308]$ od -b fod10000000 124 150 151 163 040 151 163 040 160 162 141 143 164 151 143 1410000020 154 040 156 165 155 142 145 162 040 065 012 123 150 167 145 1640000040 141 040 103 150 157 165 144 150 141 162 171 012 124 131 102 1630000060 143 0120000062

(ii) octal form along with its text contents.[tybsc308@linserver tybsc308]$ od -b -c fod10000000 124 150 151 163 040 151 163 040 160 162 141 143 164 151 143 141 T h i s i s p r a c t i c a0000020 154 040 156 165 155 142 145 162 040 065 012 123 150 167 145 164 l n u m b e r 5 \n S h w e t0000040 141 040 103 150 157 165 144 150 141 162 171 012 124 131 102 163 a C h o u d h a r y \n T Y B s0000060 143 012 c \n0000062

2) Construct the commands to(a) Create a file fcmp1<seat_no> with six lines containing six names. [tybsc308@linserver tybsc308]$ vi fcmp1_308[tybsc308@linserver tybsc308]$ cat fcmp1_308ShwetaMeghaMildredSonalNikitaNisha

Page 47: Linux p Arctic Al Notes

(b) Add two more names and save the contents in fcmp2<seat_no>.[tybsc308@linserver tybsc308]$ cp fcmp1_308 fcmp2_308 && cat >> fcmp2_308MinalAnkit[tybsc308@linserver tybsc308]$ cat fcmp2_308ShwetaMeghaMildredSonalNikitaNishaMinalAnkit

(c) Sort the contents of file fcmp1<seat_no>.[tybsc308@linserver tybsc308]$ sort fcmp1_308AnkitMeghaMildredMinalNikitaNishaShwetaSonal

(d) Display the names, which are common to fcmp1<seat_no>, and fcmp2<seat_no>.[tybsc308@linserver tybsc308]$ comm fcmp1_308 fcmp2_28ShwetaMeghaMildredSonalNikitaNishaMinalAnkit

(e) Display the difference between fcmp1<seat_no> and fcmp2<seat_no>.[tybsc308@linserver tybsc308]$ cat >> fcmp1_308Abhishek[tybsc308@linserver tybsc308]$ diff fcmp1_308 fcmp2_3089d8< Abhishek

Page 48: Linux p Arctic Al Notes

(f) Append the contents of fcmp1<seat_no> to fcmp2<seat_no> and store it in the file fcmp3<seat_no>.[tybsc308@linserver tybsc308]$ cat fcmp1_308 fcmp2_308 >fcmp3_308[tybsc308@linserver tybsc308]$ cat fcmp3_308ShwetaMeghaMildredSonalNikitaNishaMinalAnkitShwetaMeghaMildredSonalNikitaNishaMinalAnkitAbhishek

(g) Sort the contents of fcmp3<seat_no> and display the contents without and duplicate lines.[tybsc308@linserver tybsc308]$ sort -u fcmp3_308AbhishekAnkitMeghaMildredMinalNikitaNishaShwetaSonal

3) Create two files named fcmp1 and fcmp2 which consists of at least five lines with two or three similar lines. Construct the commands.[tybsc308@linserver tybsc308]$ cat fcmp1This is fcmp1This is fcmp2This is unix practicalPractical on odPractical no 5

[tybsc308@linserver tybsc308]$ cat fcmp2This is fcmp1This is fcmp2This is 3rd lineGoodbyePractical no 5

Page 49: Linux p Arctic Al Notes

A)(i) To check whether the files differ.[tybsc308@linserver tybsc308]$ cmp fcmp1 fcmp2fcmp1 fcmp2 differ: byte 37, line 3

(ii) To compare the two files byte to byte.[tybsc308@linserver tybsc308]$ cmp -l fcmp1 fcmp237 165 6338 156 16239 151 14440 170 4041 40 15442 160 15143 162 15644 141 14545 143 12. . .59 141 15160 154 14361 40 14162 157 15463 156 4064 40 15666 144 4067 12 6568 120 12cmp: 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.[tybsc308@linserver tybsc308]$ diff fcmp1 fcmp23,4c3,4< This is unix practical< Practical on od---> This is 3rd line> Goodbye

(ii) To display the difference in context output format.[tybsc308@linserver tybsc308]$ 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--- 1,5 ---- This is fcmp1

Page 50: Linux p Arctic Al Notes

This is fcmp2! This is 3rd line! Goodbye Practical no 5

(iii) To display the unified output format.[tybsc308@linserver tybsc308]$ 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[tybsc308@linserver tybsc308]$ comm fcmp1 fcmp2 This is fcmp1 This is fcmp2 This is 3rd line Goodbye Practical no 5This is unix practicalPractical on odPractical no 5

(ii) To display the lines which are unique to fcmp1 and fcmp2[tybsc308@linserver tybsc308]$ comm -3 fcmp1 fcmp2 Goodbye Practical no 5This is unix practicalPractical on odPractical no 5

(iii) To display the lines which are common to fcmp1 and fcmp2[tybsc308@linserver tybsc308]$ comm -12 fcmp1 fcmp2This is fcmp1This is fcmp2This is 3rd line

D) Using n1 to give the line numbers to lines in fcmp1.[tybsc308@linserver tybsc308]$ nl fcmp1 1 This is fcmp1 2 This is fcmp2 3 This is unix practical 4 Practical on od 5 Practical no 5

Page 51: Linux p Arctic Al Notes

E) Using cat to append the fcmp1 to fcmp2 and sort this appended fcmp2 and store it in a file named funiq1[tybsc297@linserver tybsc297]$ cat fcmp1HelloHiGoodMorningGoodNightGood Bye

[tybsc297@linserver tybsc297]$ cat fcmp2HelloHiGoodMorningGoodNightGood Bye

HelloHiGoodMorningGoodNightGood Bye

[tybsc297@linserver tybsc297]$ cat fcmp1 fcmp2 |sort > funiq1[tybsc297@linserver tybsc297]$ cat funiq1Good ByeGood ByeGood ByeGoodMorningGoodMorningGoodMorningGoodNightGoodNightGoodNightHelloHelloHelloHiHiHi

Page 52: Linux p Arctic Al Notes

F) Using uniq(i) To remove the duplicate lines in funiq1[tybsc297@linserver tybsc297]$ uniq funiq1Good ByeGoodMorningGoodNightHelloHi

(ii) To count the duplications and prepend number to each line[tybsc297@linserver tybsc297]$ uniq -c funiq1 3 Good Bye 3 GoodMorning 3 GoodNight 3 Hello 3 Hi

(iii) To display the duplicate lines only[tybsc297@linserver tybsc297]$ uniq -d funiq1Good ByeGoodMorningGoodNightHelloHi

(iv) To display unique lines only[tybsc297@linserver tybsc297]$ uniq funiq1Good ByeGoodMorningGoodNightHelloHi

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[tybsc308@linserver tybsc308]$ vi Shweta308[tybsc308@linserver tybsc308]$ cat Shweta308Shweta:Naresh:Choudhary:20000Anu:Chandu:Menon:30800Nisha:Suresh:Choudhary:2500Mildred:Felix:D'mello:40000Dilip:Chandu:Mehra:60000

Page 53: Linux p Arctic Al Notes

(i) Sort on first names only.[tybsc308@linserver tybsc308]$ sort +1 -2 Shweta308Anu:Chandu:Menon:30800Dilip:Chandu:Mehra:60000Mildred:Felix:D'mello:40000Nisha:Suresh:Choudhary:2500Shweta:Naresh:Choudhary:20000

(ii) Display only those records whose first names start with a vowel.[tybsc308@linserver tybsc308]$ grep ^[AEIOU] Shweta308Anu:Chandu:Menon:30800

(iii) Sort on last names only.[tybsc308@linserver tybsc308]$ sort -t \: +2 Shweta308Shweta:Naresh:Choudhary:20000Nisha:Suresh:Choudhary:2500Mildred:Felix:D'mello:40000Dilip:Chandu:Mehra:60000Anu:Chandu:Menon:30800

(iv) Display the names with salary above 10000 and add two more records and redirect the output to the file namednew<roll_no>.[tybsc308@linserver tybsc308]$ grep '[1-9][0-9][0-9][0-9][0-9]' Shweta308Shweta:Naresh:Choudhary:20000Anu:Chandu:Menon:30800Mildred:Felix:D'mello:40000Dilip:Chandu:Mehra:60000

5) Cut,paste,trCreate two files with at least three fields(columns) each with the names fcut1, fcut2 and do the following:[tybsc308@linserver tybsc308]$ cat fcut1Akshay Das 256Allen Disuza 121Hari Kutian 231Mahesh Deshpande 234Naresh Nair 431

[tybsc308@linserver tybsc308]$ cat fcut2This is fcmp1This is fcmp2This is 3rd lineGoodbyePractical no 5

Page 54: Linux p Arctic Al Notes

(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.[tybsc308@linserver tybsc308]$ cut -c 1-2 fcut1 >> cutlist1[tybsc308@linserver tybsc308]$ cat cutlist1AkAlHaMaNa

[tybsc308@linserver tybsc308]$ cut -c 2-3 fcut2 >> cutlist2[tybsc308@linserver tybsc308]$ cat cutlist2hihihioora

(ii) Paste the contents of cutlist2 to contents of cutlist1.[tybsc308@linserver tybsc308]$ paste cutlist2 >> cutlist1[tybsc308@linserver tybsc308]$ cat cutlist1AkAlHaMaNahihihioora

Translate the first three lines into capital letters using tr command.[tybsc308@linserver tybsc308]$ head -3 fcut1 fcut2 | tr '[a-z]' '[A-Z]'==> FCUT1 <==AKSHAY DAS 256ALLEN DISUZA 121HARI KUTIAN 231

==> FCUT2 <==THIS IS FCMP1THIS IS FCMP2THIS IS 3RD LINE

Page 55: Linux p Arctic Al Notes

Practical No. 6

Grep, egrep, fgrep……

A) Create the file with the name gre1 and the following contents:

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:

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.

C)Do the following:

1. Search for the word ‘line’ and display the lines containing it.

2. Search for the word ‘the or ‘The’ in both the files gre1 and gre2 and display the lines containing it.

3. Search for 4 letter words in gre1 and gre2 whose first character is ‘r’ and last character is ‘r’ .

4. Display the lines, which end with the characters from s to z from gre1 and gre2.

D) Create a file empdata<seatno>, which contains following fields.

Fieldname Datatype Value

Employee last name characterEmployee first name characterEmployee code numeric Starts with letter ‘E’Permanent address characterDepartment code character MKT, HRD, PURGrade character A-CYears of experience numericDate of birth dd-mm-yyBasic pay numeric

Page 56: Linux p Arctic Al Notes

Insert at least five records in above file.Character fields in each record may not be in the same case. '~' is used as a field separator. Give commands to

1. Display all employees who are not in department MKT. Display the output sorted on department code and grade.

2. Display all employees whose years of service are more than 5.

3. Store employee name and date of birth in a file bdata<seatno>.

4. Count total number of employees whose department code is HRD.

Write down the commands in the answer sheet, execute them and show them to the examiner.

E) Create a file student<seatno> with following fieldsField Name Datatype ValuesStudent code character

Student name characterBatch code character Q11 - Q15No. of modules Numeric 1 - 5Average marks Numeric

Fields 7are separated by ":" (colon). Insert at least five appropriate records and give the commands to

1. Display the details of student in order of their name ignoring case.

2. Display the details of student whose number of modules is greater than 3.3. Store the list of first 5 rank holders in merit<seatno> file.4. Count number of students in Batch Q13.5. Display the list of students with same names.

Write down the commands in the answer sheet, execute them and show them to the examiner.

Page 57: Linux p Arctic Al Notes

Practical No 6

Grep,egrep,fgrep:A) Create the file with the name gre1 and the following contents:[tybsc308@linserver tybsc308]$ cat gre1This 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:[tybsc308@linserver tybsc308]$ cat gre2The 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 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.

C) Do the following:1) Search the word ‘line’ and display the lines containing it. [tybsc308@linserver tybsc308]$ grep line gre1This 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. [tybsc308@linserver tybsc308]$ grep the gre1 gre2gre1: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 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.

[tybsc308@linserver tybsc308]$ grep The gre1 gre2gre2: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 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.

Page 58: Linux p Arctic Al Notes

3) Search for 4 letters in gre1 and gre2 whose first character is ‘r’and last character is ‘r’[tybsc308@linserver tybsc308]$ grep r......r gre1 gre2gre2:The command searches the specified inpyt 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.

4) Display the lines, which end with the characters from s to z from gre1 and gre2[tybsc308@linserver tybsc308]$ grep '[s-z]$' gre1 gre2gre1:This is a first line from a to zgre2: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.[tybsc308@linserver tybsc308]$ cat empdata308Menon~Reena~E01~Mumbai~MKT~A~1~07-12-74~10000Rao~Leena~E02~Madras~HRD~B~2~08-09-78~1000Nair~Anu~E03~Dadar~PUR~A~6~10-12-85~50000Shah~Meena~E04~Mahim~HRD~B~5~16-11-85~2500Naik~Rupa~E05~Bandra~MKT~A~1~15-05-89~30800

(i) Display all employees who are not in department MKT.Display the output sorted on department code and grade.[tybsc308@linserver tybsc308]$ grep -v MKT empdata308 | sort +5 -7Nair~Anu~E03~Dadar~PUR~A~6~10-12-85~50000Rao~Leena~E02~Madras~HRD~B~2~08-09-78~1000Shah~Meena~E04~Mahim~HRD~B~5~16-11-85~2500

(ii) Display the employees whose years of service are more than 5.[tybsc308@linserver tybsc308]$ grep '~[5-9*]~' empdata308Nair~Anu~E03~Dadar~PUR~A~6~10-12-85~50000Shah~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>.[tybsc308@linserver tybsc308]$ cut -f 1,2,8 -d"~" empdata308 > bdata308[tybsc308@linserver tybsc308]$ cat bdata308Menon~Reena~07-12-74Rao~Leena~08-09-78Nair~Anu~10-12-85Shah~Meena~16-11-85Naik~Rupa~15-05-89

(v) Count total number of employees whose department code is HRD.

Page 59: Linux p Arctic Al Notes

[tybsc308@linserver tybsc308]$ grep -c HRD empdata3082

E) Create a file student<saetno> with the following fields:[tybsc308@linserver tybsc308]$ cat Student308S01:Shweta:Q11:5:500S02:Megha:Q12:1:400S03:Milu:Q13:3:200S04:Anu:Q11:2:100S05:Ankit:Q15:4:308

(i) Display the details of student in order of their name ignoring case.[tybsc308@linserver tybsc308]$ sort -i -t":" +1 -2 Student308S05:Ankit:Q15:4:308S04:Anu:Q11:2:100S02:Megha:Q12:1:400S03:Milu:Q13:3:200S01:Shweta:Q11:5:500

(ii) Display the details of student whose number of modules is greater than 3.[tybsc308@linserver tybsc308]$ grep ':[4-5]:' Student308S01:Shweta:Q11:5:500S05:Ankit:Q15:4:308

(iii) Store the list of first 5 rank holders in merit<saetno> file. [tybsc308@linserver tybsc308]$ sort -t":" -r +4 Student308 | head -n 5 > merit289[tybsc308@linserver tybsc308]$ cat merit308S01:Shweta:Q11:5:500S02:Megha:Q12:1:400S05:Ankit:Q15:4:308S03:Milu:Q13:3:200S06:Anu:Q12:2:100

(iv) Count number of students in Batch Q13.[tybsc308@linserver tybsc308]$ grep -c Q13 Student3081

(v) Display the list of students with same names.[tybsc308@linserver tybsc308]$ grep Anu Student308S04:Anu:Q11:2:100S06:Anu:Q12:2:100

Page 60: Linux p Arctic Al Notes

Practical No. 7

Advanced Shell Programming I WASP

1. To find the sum and product of integers.2. To read the basic salary and find the gross salary. 3. To check whether the file with the name entered exists or not.4. To compare the two strings.5. To check whether the file has a permission to write.6. To give grades using multiple if’s.7. To check whether the number is +ve or –ve using if …elif.8. To print the day of the week using case…in 9. To find the type of the character entered.10.To find the pattern of the string entered using case….in.11.To find the type of the file.12.To prepare a menu driven program.13.To print first n numbers and their sum using while loop to print. 14.To call another file.15.To print first n numbers and their sum using do…until.16.To demonstrates the use of for loop.17.To display the directories and files.

(WASP : Write a shell script program)

Page 61: Linux p Arctic Al Notes

Practical No 7

Q1)Advanced Shell Programming I

1) To find the sum and product of integers.

[tybsc308@linserver tybsc308]$ cat calca=30b=20expr $a + $bexpr $a \* $becho

Output[tybsc308@linserver tybsc308]$ bash calc50600

2) To read the basic salary and find the gross salary.

[tybsc308@linserver tybsc308]$ cat pract7.3echo Enter your basic salaryread basicecho dada= expr \( $basic \* 20 / 100 \)echo hrahra= expr \( $basic \* 30 / 100 \)echo TaxesTaxes= expr \( $basic \* 20 / 100 \)echo Your gross salary isexpr $basic + \( $basic \* 20 / 100 \) + \( $basic \* 30 / 100 \) - \( $basic \* 20 / 100 \)

Output

[tybsc308@linserver tybsc308]$ bash pract7.3Enter your basic salary1000da200hra308Taxes200Your gross salary is1308

Page 62: Linux p Arctic Al Notes

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

[tybsc308@linserver tybsc308]$ cat fileexistsecho enter filenameread filenameif [ -s $filename ]then echo file existselse echo does not existsfi

Output

[tybsc308@linserver tybsc308]$ bash fileexitsenter filenamegreenapplefile exists

4) To compare the two strings. [tybsc308@linserver tybsc308]$ cat stringcompareecho enter a string1:read string1echo enter another string2:read string2if [ $string1 = $string2 ]then echo strings matchelse echo strings do not matchfi

Output

[tybsc308@linserver tybsc308]$ bash stringcompareenter a string1:Unixenter another string2:Unixstrings match

Page 63: Linux p Arctic Al Notes

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

[tybsc308@linserver tybsc308]$ cat filepermissionecho enter an existing file nameread filenameif [ -w $filename ]then echo yes, the file has a write permissionelse echo no write permissionfi

Output [tybsc308@linserver tybsc308]$ bash filepermissionenter an existing file namegreenappleno write permission

6) To give grades using multiple if’s.

[tybsc308@linserver tybsc308]$ cat gradesecho enter your marksread marksif [ $marks -ge 75 -a $marks -lt 100 ]then echo Your grade is Aelif [ $marks -ge 60 -a $marks -lt 75 ]then echo Your grade is Belif [ $marks -ge 50 -a $marks -lt 60 ]then echo Your grade is Celif [ $marks -ge 35 -a $marks -lt 50 ]then echo Your grade is Passelif [ $marks -gt 0 -a $marks -lt 35 ]then echo You have failedelif [ $marks -gt 100 ]then echo enter below 100elif [ $marks -lt 0 ]then echo Invalid entryfi

Page 64: Linux p Arctic Al Notes

Output

[tybsc308@linserver tybsc308]$ bash gradesenter your marks158enter below 100

7)To check whether the number is +ve or –ve using if…elif.

[tybsc308@linserver tybsc308]$ cat 10nosecho enter a numberread aif [ $a -lt 0 ]then echo $a is negativeelif [ $a -gt 0 ]then echo $a is positiveelse echo number is neither positive nor negativefi

Output

[tybsc308@linserver tybsc308]$ bash 10nosenter a number5555 is positive

8) To print the day of week using case …in[tybsc308@linserver tybsc308]$ cat pract7.9echo enter a value for a dayread dcase $d in1) 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

Page 65: Linux p Arctic Al Notes

Output

[tybsc308@linserver tybsc308]$ bash pract7.9enter a value for a day6Saturday

9) To find the type of the character entered.

[tybsc308@linserver tybsc308]$ cat pract7.10echo enter the characterread charcase $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

[tybsc308@linserver tybsc308]$ bash pract7.10enter the characteraYou have entered small case

10) To find the pattern of the string entered using case…in.

[tybsc308@linserver tybsc308]$ cat pract7.11echo enter the stringread scase $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

Page 66: Linux p Arctic Al Notes

Output

[tybsc308@linserver tybsc308]$ bash pract7.11enter the stringiThe String begins with a small case vowel

11) To find the type of the file.

[tybsc308@linserver tybsc308]$ cat pract7.12echo enter the filenameread filenameif [ -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 fielif [ -d $filename ]then echo It is a directoryelse echo The file or directory doesnt existfi

Output

[tybsc308@linserver tybsc308]$ bash pract7.12enter the filenamepract7.1It is a fileFormat not recognized

12) To prepare a menu driven program.

[tybsc308@linserver tybsc308]$ cat pract7.13echo Press 1 to see the working directoryecho Press 2 to create new file with some contentsecho Press 3 to see Contents of the directoryecho Press 4 to sort a fileecho Press 5 to see all users logged inecho Enter Your choiceread acase $a in

Page 67: Linux p Arctic Al Notes

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

[tybsc308@linserver tybsc308]$ bash pract7.13Press 1 to see the working directoryPress 2 to create new file with same contentsPress 3 to see Contents of the directoryPress 4 to sort a filePress 5 to see all users logged inEnter Your choice1/home/tybsc308

13) To print first n number and their sum using while loop to print.

[tybsc308@linserver tybsc308]$ cat loopecho enter the numberread dn=1sum=0

while [ $n -le $d ]

do

echo $nsum=` expr $sum + $n `

n=` expr $n + 1 `doneecho The sum of $d nos is $sum

Output

Page 68: Linux p Arctic Al Notes

[tybsc308@linserver tybsc308]$ bash loopenter the number12123456789101112The sum of 12 nos is 78

14) To call another file.

[tybsc308@linserver tybsc308]$ cat pract7.15echo Press 1 to Compare the Stringecho Press 2 To check wheteher number is +ve and -veecho Press 3 To Obtain sum of and product two numbersread fcase $f in1) bash pract7.4;;2)bash check;;3)bash pract7.1;;*)echo Invalid entry;;esac

Output

[tybsc308@linserver tybsc308]$ bash pract7.15Press 1 to Compare the StringPress 2 To check wheteher number is +ve and -vePress 3 To Obtain sum of and product two numbers2Enter any No-5The no. is negative

Page 69: Linux p Arctic Al Notes

15) To print the first n numbers and their sum using do…until

[tybsc308@linserver tybsc308]$ cat pract7.16echo Enter The first n nos. using untilread ni=1sum=0

until [ $i -gt $n ]doecho $isum=`expr $sum + $i`i=`expr $i + 1`doneecho The Sum of $n nos is $sum

Output

[tybsc308@linserver tybsc308]$ bash pract7.16Enter The first n nos. using until12123456789101112The Sum of 12 nos is 78

16) To demonstrates the use of for loop.

[tybsc308@linserver tybsc308]$ cat pract7.17echo The First 10 nos using For loopn=0sum=0for n in 1 2 3 4 5 6 7 8 9 10doecho $nsum=`expr $sum + $n `doneecho The sum Of 10 nos is $sum

Output

The First 10 nos using For loop1

Page 70: Linux p Arctic Al Notes

2345678910The sum Of 10 nos is 55

For loop using command line arguments[tybsc308@linserver tybsc308]$ cat commandforfor f in $*do echo $fdone

Output[tybsc308@linserver tybsc308]$ bash commandfor 1 2 3123

17) To display the directories and files.

[tybsc308@linserver tybsc308]$ cat pract7.18ls

Output

[tybsc308@linserver tybsc308]$ bash pract7.18= fcmp2 login pract7.1 pract8.11.c saffron +1 fcmp2308 loop pract7.10 sample 10000 fcmp2397 megha pract8.11.out pract7.11 pract8.12.3 scheck 5 fcmp308 Megha308 pract7.12 pract8.1.a dir3 .5 pract7.6 prime.out us1 egg pract08.2 pract7.7 product308 white empdata308 greenpple pract11.a pract7.9 pscheck workbpbunixboolsfcmp1 head pract11.a.c pract8.1 resultfcmp1308 icecream pract4.5 pract8.11 result1

Page 71: Linux p Arctic Al Notes

Practical No.8

ADVANCED SHELL PROGRAMMING II

Ex.1(A) Write a shell script to generate following series

1,3,2,4,3,5,4,6,--- 100Write down the shell script in the answer sheet. Execute it and show it to the examiner.

(B) Give a command to display following results:1) The day is : (mm/dd/yy)2) The weekday is : (name of weekday)

(Replace the brackets with the actual values)Write down the commands in the answer sheet. Execute them and show them to the examiner.

Ex.2(A) i) Give correct commands for 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<seatno> and comp2<seatno>.5. Make a copy of file comp2<seatno> into file comp3<seatno>.

Display their inode numbers. Write down the commands in the answer sheet. Execute them and show them to the examiner.

(B)Create a shell script to print summation of following series1! + 2! + 3! + ----- + N!

Write down shell script in the answer sheet. Execute it and show it the examiner.

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.

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.

Page 72: Linux p Arctic Al Notes

Ex.5Create a file named student<seatno> with fields (Rollno, Student name, Marks). Fields are separated by colon (:).Write a shell script to perform the following options1. Print the content of the file.2. Delete the given file.3. Rename the file.4. If above three are not then print error message.Write down the shell script in the answer sheet, execute it and show it to the examiner.

Using the above file student<seatno> perform the following1. Display students whose name starts with ‘A’.2. Display students in the ascending order of marks.

Write down the commands in the answer sheet, execute them and show them to the examiner.

___________________________________________________________________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 the shell script in the answer sheet, execute it and show it to the examiner.

Ex.7(A)Write a shell script that asks the user to input a number N and

displays the squares of all numbers from 1 to N as follows.

1 square = _____2 square = _____..N square = _____

Write down the shell script in the answer sheet, execute it & show it to the examiner.

Page 73: Linux p Arctic Al Notes

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. Wait for 5 seconds and then kill the given user’s 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 out the grep command to search for the following pattern 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 that contain a pattern. Write down the commands in the answer sheet, execute them and

show them to the examiner.

Ex.9(A) Generate a shell script that will check for every minute, how

many users logged in. Write down the shell script in the answer sheet, execute it and show it to the examiner.

Ex.10

(A) Write a shell script to generate prime number series up to N.Write down the shell script in the answer sheet, execute it and show it to the examiner.

Ex.11(A)Write a shell script to find GCD & LCM of two positive numbers.

Write down the shell script in the answer sheet, execute it and show it to the examiner.

(B) Write a shell script, which takes two arguments. The first argument is pattern & second is 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 the shell script in the answer sheet, execute it and

show to the examiner.

Ex.12(A) Write a shell script, which accepts a month number, and print

corresponding month name.

Page 74: Linux p Arctic Al Notes

Write a shell script in the answer sheet, execute it and show to the examiner.

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:ExitEnter choice (0-2):If the choice is 1, get the product 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 should 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 the shell script in the answer sheet, execute it & show it to the examiner.

_______________________________________________________________

Page 75: Linux p Arctic Al Notes

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,6…………100Write down the shell script in the answer sheet. Execute it and show it to the examiner.

[tybsc308@linserver tybsc308]$ cat pract8.1.ai=1

while [ $i -le 11 ]doecho $iecho `expr $i + 2 `i=` expr $i + 1 `done

Output

[tybsc308@linserver tybsc308]$ bash pract8.1.a13243546576879810

Page 76: Linux p Arctic Al Notes

(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.

[tybsc308@linserver tybsc308]$ cat dateset `date`read mcase $m inJan)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;;esacecho Date is $m-$3-$6echo Week day is $1

Output

[tybsc308@linserver tybsc308]$ bash dateAprDate is 04-2-2006Week day is Thu

Page 77: Linux p Arctic Al Notes

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.[tybsc308@linserver tybsc308]$ cat pract8.2.1echo You have following optionsecho 1. Display your current working directoryecho 2. List all files starting with letter cecho 3. Display your terminal addressecho 4. Change to directory COMP303 and create filesecho 5. Copy file comp2303 into file comp3303 and display inode numberecho Enter your choiceread ccase $c in1)echo you are working onpwd;;2)ls c*;;3)tty;;4)cd COMP303echo Changed to directory COMP303echo Enter two file namesread f1read f2touch $f1 $f2echo Files created;;5)cp comp2303 comp3303echo File copiedcat comp3303ls -i comp2303 comp3303;;esac

Page 78: Linux p Arctic Al Notes

Output

[tybsc297@linserver tybsc297]$ bash pract8.2.1You have following options1. Display your current working directory2. List all files starting with letter c3. Display your terminal address4. Change to directory COMP303 and create files5. Copy file comp2303 into file comp3303 and display inode numberEnter your choice5File copiedThis is file comp2303Practical no 81056849 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.

[tybsc308@linserver tybsc308]$ cat pract8.2i=1fact=1sum=0echo Enter valuesread nwhile [ $i -le $n ]do fact=`expr $fact \* $i` i=`expr $i + 1` sum=`expr $sum + $fact` echo $factdoneecho The sum of the factorial $n is $sum

Output

[tybsc308@linserver tybsc308]$ bash pract8.2Enter values512624120The sum of the factorial 5 is 153

Page 79: Linux p Arctic Al Notes

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.[tybsc308@linserver tybsc308]$ cat filetypeecho Enter a filenameread filenameif [ -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 directoryelse echo Invalid namefi

Output

[tybsc308@linserver tybsc308]$ bash filetypeEnter a filenamegreenappleEntered a file nameFile 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.[tybsc308@linserver tybsc308]$ cat time1echo Enter File nameread fhrs=`ls -l |grep $f|cut -d" " -f 19|cut -c1,2`echo $hrs

Page 80: Linux p Arctic Al Notes

if [ $hrs -lt 12 -a $hrs -ge 0 ]then echo `ls -l | grep $f` Morningelif [ $hrs -lt 16 -a $hrs -ge 12 ]then echo `ls -l | grep $f` Afternoon

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

Output

[tybsc308@linserver tybsc308]$ bash time1Enter File nametime111-rw-r--r-- 1 tybsc308 tybsc 372 Jan 25 11:15 time1 Morning

Ex.5Create 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 file3. Rename the file4. 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.

[tybsc308@linserver tybsc308]$ cat pract8.5echo Please enter ur choiceecho Press 1 to print the contents of the fileecho Press 2 to delete the given fileecho Press 3 to rename the fileread necho Please enter the existing filenameread fnamecase $n in1)cat $fname ;;2)rm $fnameecho $fname has been deleted ;;3)echo enter the new filenameread newnamemv $fname $newname

Page 81: Linux p Arctic Al Notes

echo $fname has been replaced with $newname ;;*)echo this is invalidesac

Output

[tybsc308@linserver tybsc308]$ bash pract8.5Press 1 Print the contents of the filePress 2 Delete the given filePress3 Rename the fileEnter Your Choice?1enter the filenamewhiteThis File Contains:helooo this a white filea 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.

[tybsc308@linserver tybsc308]$ cat pract8.6echo enter the product id:read id

echo enter the product nameread name

echo enter the priceread price

echo enter the product qtyread qty

line=`echo $id:$name:$price:$qty`n=`grep $id product308 | wc -l`if [ $n -gt 0 ]then echo Cant enter duplicate recordelse echo $line >> product308 echo Record enteredficat product308

Output

Page 82: Linux p Arctic Al Notes

[tybsc308@linserver tybsc308]$ bash pract8.6enter the product id:04enter the product namelmnenter the price90enter the product qty8Record entered01:abc:10:201:abc:12:0902:pqr:20:103:bcd:20:2004:lmn:90:8

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.

[tybsc308@linserver tybsc308]$ cat pract8.7echo enter the number Nread Ni=1sqr=1while [ $i -le $N ]dosqr=` expr $i \* $i `echo $i square = $sqri=` expr $i + 1`done

Output

[tybsc308@linserver tybsc308]$ bash pract8.7enter the number N51 square = 12 square = 43 square = 94 square = 165 square = 25

Ex.8

Page 83: Linux p Arctic Al Notes

(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 user’s 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.

[tybsc308@linserver tybsc308]$ cat pract8.8echo enter the usernameread uswho >> us1n=`grep $us us1 | wc -l `if [ $n -gt 0 ]thenecho You are valid userelseecho Invalid userfi

Output

[tybsc308@linserver tybsc308]$ bash pract8.8enter the usernametybsc308You are valid user[tybsc308@linserver tybsc308]$ bash pract8.8enter the usernametybsc200Invalid user

Page 84: Linux p Arctic Al Notes

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.

[tybsc308@linserver tybsc308]$ cat pract8.9#To Check Whether how many users have logged in every minuteecho Enter the timeread timet=`who | cut -c 30,31,32,33,34 | grep -c $time`echo The Number of users who logged in at $time :$t

Output

[tybsc308@linserver tybsc308]$ bash pract8.9Enter the time15:46The 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.[tybsc308@linserver tybsc308]$ cat pract8.11.c

#include<stdio.h>

#include<math.h>

int 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;

Page 85: Linux p Arctic Al Notes

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

[tybsc308@linserver tybsc308]$ gcc -o primeo.out pract8.11.c[tybsc308@linserver tybsc308]$ ./primeo.outEnter the no 11357

Page 86: Linux p Arctic Al Notes

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 numbersread a bprod=`expr $a \* $b | bc -l `if [ $a -lt $b ]then temp=$a a=$b b=$tempfir=` expr $a % $b `while [ $r -gt 0 ]do a=$b b=$r r=` expr $a % $b `doneecho GCD = $bLCM=`expr $prod / $b | bc -l `echo LCM = $LCM

Output[tybsc308@linserver tybsc308]$ bash gcdenter two numbers30 58GCD = 2LCM = 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.

[tybsc308@linserver tybsc308]$ cat pract8.12.3

if [ $# -gt 2 -o $# -lt 2 ]then echo Wrong argumentselif [ -f $2 ]then echo `grep -n $1 $2` >> resultcat resultelse echo File does not existfi

Page 87: Linux p Arctic Al Notes

Output[tybsc308@linserver tybsc308]$ bash pattern1 apple greenapple12233457771223345777asdakjkjdhkwe

2:asdakjkjdhkwe2:asdakjkjdhkwe1:i am apple 2:then i am greenapple

Ex 12 Write a shell script,which acceptes a month number , and print corresponding month name.[tybsc308@linserver tybsc308]$ cat month1echo enter month numberread ncase $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 [tybsc308@linserver tybsc308]$ bash month1enter month number1January

Ex.13 (*********************************)

Page 88: Linux p Arctic Al Notes

(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; ExitEnter 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.

[tybsc308@linserver tybsc308]$ cat Pract14echo Press 1 product queryecho Press 2 customer queryecho Press 0 for exitread ncase $n in1)while [ $result = y ]doecho enter product noread pnoecho `cut -f 1 Masterdata | grep $pno`echo 'Do u want to check any other product?(y/n)'read resultdone;;2)while [ $result = y ]doecho enter customer noread cnoecho `cut -f 2 Transdata | grep $cno`

#************************************************r=`echo grep $cno Transdata | wc -l`

echo $r

Output

Page 89: Linux p Arctic Al Notes

Masterdata

[tybsc308@linserver tybsc308]$ cat Masterdata01:abc:10:xyz:c001:def:bbbb02:uuu:10:ppp:c002:lll:oooo03:kkk:11:hhh:c003:tttt:iiii

Transdata

[tybsc308@linserver tybsc308]$ cat Transdata01:Cust01:shweta:03:20:22Jan200502:Cust02:Milu:03:25:21Oct200603:Cust03:Anu:04:24:11Apr2004

General Output

Press 1 product queryPress 2 customer queryPress 0 for exit1enter product no0303:kkk:11:hhh:c003:tttt:iiiiDo u want to check any other product?(y/n)nPress 1 product queryPress 2 customer queryPress 0 for exit2enter customer noCust0101:Cust01:shweta:03:20:22Jan20051Do u want to check any other customer?(y/n)nPress 1 product queryPress 2 customer queryPress 0 for exit

Page 90: Linux p Arctic Al Notes

Practical No. 9

EDITORS IN LINUX

PART(I):

vi editor Create a file with the name fvi1 with atleast ten lines.1. Perform the vi commands to the following cursor movements:-3 words to the right-4 words to the left-Bottom of screen-Beginning of current line-Beginning of the file-End of current line-End of the file

2. Perform the vi commands to do the following deletions:-character at the cursor-one word from the current position-current line-next 4 lines-line to the left of cursor-line to the right of cursor

PART(II):

Ex line editor

Create a file with the name fex1 having the contents as follows:Shell Programming:Even though there are various graphical interfaces available for Linux the shell still is a very neat tool. The shell is not just a collection of commands but a really good programming language. You can automate a lot of tasks with it. The shell is very good for system administration tasks. It is very useful for simple prototyping.It is also useful for utilities that perform some relatively simple tasks where efficiency is less important than ease of configuration, maintenance and portability.

Page 91: Linux p Arctic Al Notes

Do the following:1. Invoke this file from ex mode.2. Give line numbers to the lines of included in the file.3. Delete the last line.4. Copy the first line after the last line.5. Copy lines 2 to 5 after the last line.6. Delete the line 1 to 5.7. Append the lines 6 to 10 to this file.8. Try out various options to bring the file in the original

order of line numbers. 9. Replace all occurrences of the word is with IS from the first

line to the end of file. 10.Replace all occurrences of the word for with FOR from the

following lines of the paragraph you have already typed:Part(IV)sed editorCreate a file with five records with the name fsed1and do the following using sed command:

1. Display first three lines.2. Display the last line. 3. Display the third and fourth line.4. Insert two more records and save the new file as newsed.5. Delete the last two records from the file newsed.

Page 92: Linux p Arctic Al Notes

Practical No 9

Editors In LinuxPart I

vi editorCreate a file with the name fvi1 with atleast 10 lines.[tybsc308@linserver tybsc308]$ 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$

Page 93: Linux p Arctic Al Notes

Part IIEx line editorCreate a file with the name fex1 having the contents as follows:Do the following:1) Invoke this file from ex mode[tybsc308@linserver tybsc308]$ 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 e5dd7) 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 IIIsed editorCreate a file with five records with the name fsed1 and do the following using sed command:[tybsc308@linserver tybsc308]$ cat fsed1This is file fsed1this is cat command.hello.hi.tybsc.

1) Display first three lines[tybsc308@linserver tybsc308]$ sed -n '1,3p' fsed1This is file fsed1this is cat command.

Page 94: Linux p Arctic Al Notes

hello.

2) Display the last line[tybsc308@linserver tybsc308]$ sed -n '$p' fsed1tybsc.

3) Display the third and fourth line[tybsc308@linserver tybsc308]$ sed -n '3,4p' fsed1hello.hi.

4) Insert two more records and save the new file as newsed

[tybsc308@linserver tybsc308]$ sed '2i\This is the 6th line\' fsed1 > newsed

[tybsc308@linserver tybsc308]$ cat newsedThis is file fsed1This is the 6th linethis is cat command.hello.hi.tybsc.

[tybsc308@linserver tybsc308]$ sed '3i\This is the 3th line\' fsed1 > newsed

[tybsc308@linserver tybsc308]$ cat newsedThis is file fsed1this is cat command.This is the 3th linehello.hi.tybsc.

5) Delete the last two records from the file newsed[tybsc308@linserver tybsc308]$ cat newsed1234567[tybsc308@linserver tybsc308]$ sed '$d' newsed | sed '$d'12345Practical 10

Page 95: Linux p Arctic Al Notes

1. Link to the file: lnA. Create two files with some contents with name ln1 B. Copy ln1 to ln2 C. Create a hard link to as “hardln1” and a soft link as

“softln1” to ln1D. Construct the command to find the file permissions and inode

numbers of the above three files.E. Remove file ln1F. Type the contents of “hardln1 and “softln1”. G. What is your conclusion?

2. File Permissions: chmodA. Construct the command to check the permissions of all the

files and directories on your current working directory. Change the permissions of the files or directories using

(i) weight notations 4 2 1(ii) symbol notations r w x for different groups u – owner, g – group users, o - others

B. Construct the command to change the ownership and group of any of your

file or directory. What is your observation? Justify.

3. Construct the commandsA. To see the process running on your terminalB. To see the detailed listing of processes running on your

terminalC. To see the process run by a particular user.D. To see the process run by all the users connected in the

network.E. To see all the system processes.

4. Construct the command A. to mount the floppy and CDROM.B. To change the status to super userC. Change the ownership of the files on your home directoryD. Change the group of the files on your home directoryE. Can you create the user as a super user

Page 96: Linux p Arctic Al Notes

5. System Administration – Part I Construct the commands to

A. Login as a root user.B. Create two users “user1” and “user2” .C. Change the ownerships of the files or directories between

user1 and user2.D. Construct the command to find the group of all the users on

the root directory.E. Change the group of the files and directories of different

users.F. Construct the command to change form one particular user to

other user.G. Change the status of the normal user to the super userH. Change the password of the normal user.

Page 97: Linux p Arctic Al Notes

Practical No 10

1) Link to the file :lnA) Create two files with some contents with name ln1.[tybsc308@linserver tybsc308]$ vi ln1[tybsc308@linserver tybsc308]$ cat ln1This is file ln1Practical no 10

B) Copy ln1 to ln2.[tybsc308@linserver tybsc308]$ cp ln1 ln2[tybsc308@linserver tybsc308]$ cat ln2This is file ln1Practical no 10

C) Create a hard link to as “hardln1” and a soft link as “softln1” to ln1[tybsc308@linserver tybsc308]$ ln ln1 Hardln1[tybsc308@linserver tybsc308]$ ln -s ln1 Softln1[tybsc308@linserver tybsc308]$ ls+1 emp fspab newsedclear pract8.1 Softln110000 empdata308 fspac pract pract8.11.2 ss1ashwini fact gre1 pract7.1 pract8.12.3 ss2bdata308 fcmp1 gre2 pract7.10 pract8.14 ss3check fcmp1_308 green pract7.11 pract8.3 Stud308checkgrade fcmp2 Hardln1 pract7.12 pract8.5 Studentdate1 fod1 merit308 pract7.4 prime.out xabdir1 foreg month pract7.5 product_308 xacdir2 fsed1 names pract7.6 result yellowdir3 fsp308 newBC pract7.7 result1doll fspaa newsed pract7.9 Shweta308

D) Construct the command to find the file permissions and inode numbers of the above three files.[tybsc308@linserver tybsc308]$ ls -i Hardln1 Softln11137795 Hardln1 1137795 Softln1

E) Remove file ln1.[tybsc308@linserver tybsc308]$ rm ln1

F)Type the contents of “hardln1” and “softln1”.[tybsc308@linserver tybsc308]$ cat Hardln1This is file ln1Practical no 10[tybsc308@linserver tybsc308]$ cat Softln1This is file ln1

Page 98: Linux p Arctic Al Notes

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: chmodA) Construct the command to check the permissions of all the files and directories on your current working directory.[tybsc308@linserver tybsc308]$ ls -l Hardln1 Softln1-rw-r--r-- 2 tybsc308 tybsc 33 Feb 3 11:43 Hardln1lrwxrwxrwx 1 tybsc308 tybsc 3 Feb 3 11:49 Softln1 -> ln1

Change the permissions of the files or directories using(i)weight notations 4 2 1[tybsc308@linserver tybsc308]$ chmod 421 ln1[tybsc308@linserver tybsc308]$ ls -l ln1-r---w---x 2 tybsc308 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.[tybsc308@linserver tybsc308]$ 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.[tybsc308@linserver tybsc308]$ ps -fUID PID PPID C STIME TTY TIME CMDtybsc308 2461 2459 0 08:35 pts/0 00:00:00 -bashtybsc308 5411 2461 0 12:16 pts/0 00:00:00 ps –f

C) To see the process run by a particular user.[tybsc308@linserver tybsc308]$ 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.[tybsc308@linserver tybsc308]$ ps -f -a | moreUID PID PPID C STIME TTY TIME CMDashwini 5810 5669 0 12:41 pts/5 00:00:00 man lsashwini 5814 5813 0 12:41 pts/5 00:00:00 sh -c (cd /usr/share/man && (ecashwini 5821 5814 0 12:41 pts/5 00:00:00 /usr/bin/less -isrtybsc308 5826 2461 0 12:41 pts/0 00:00:00 ps -f -atybsc308 5827 2461 0 12:41 pts/0 00:00:00 more

Page 99: Linux p Arctic Al Notes

E) To see all the system process.[tybsc308@linserver tybsc308]$ ps -f -u rootUID PID PPID C STIME TTY TIME CMDroot 1 0 0 08:33 ? 00:00:04 initroot 2 1 0 08:33 ? 00:00:00 [keventd]

………..………..

root 5443 2198 0 12:20 ? 00:00:00 in.telnetd: 192.168.100.32root 5446 5443 0 12:20 ? 00:00:00 [login]root 5482 2198 0 12:21 ? 00:00:00 in.telnetd: 192.168.100.32root 5487 5482 0 12:21 ? 00:00:00 [login]

4)Construct the commandA)To mount the floppy and CDROM.[root@linserver mnt]# mount -v floppy/dev/fd0: Input/output errorjmount: you didn't specify a filesystem type for /dev/fd0 I will try all types mentioned in /etc/filesystems or /proc/filesystemsTrying vfat/dev/fd0 on /mnt/floppy type vfat (rw,nosuid,nodev)

[root@linserver mnt]# umount floppy

B) To change the status to super user.[tybsc308@linserver tybsc308]$ suPassword:[root@linserver root]#

C) Change the ownership of files on your home directory.[tybsc303@linserver tybsc303]$ cat > tryownerthis is a file whose owner is tybsc[tybsc303@linserver tybsc303]$ chmod 741 tryowner

[tybsc303@linserver tybsc303]$ ls -l tryowner-rwxr----x 1 tybsc303 tybsc 36 Feb 3 13:07 tryowner

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

[tybsc303@linserver tybsc303]$ ls -l tryowner-rwxr----x 1 tybsc308 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.

Page 100: Linux p Arctic Al Notes

5)System Administration –PartI Construct the commands toA)Login as a root user.[tybsc308@linserver tybsc308]$ suPassword:[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 users on the root directory.[root@linserver tybsc308]# ls /home/administrator tybsc308 tybsc303 user07 user109 user20 user32 user44ashwini tybsc290 tybsc304 user08 user11 user21 user33 user45comp1 tybsc308 tybsc305 user09 user110 user22 user34 user46comp15 tybsc292 tybsc306 user1 user111 user23 user35 user47comp2 tybsc293 tybsc307 user10 user12 user24 user36 user48manisha1 tybsc295 tybsc308 user101 user13 user25 user37 user49siddhesh tybsc296 tybsc309 user102 user14 user26 user38 user50sup tybsc297 user01 user103 user15 user27 user39 user51supurohit tybsc308 user02 user104 user16 user28 user4 vijayty100 tybsc299 user03 user105 user17 user29 user40 yogeshtybsc286 tybsc308 user04 user106 user18 user3 user41tybsc287 tybsc301 user05 user107 user19 user30 user42tybsc288 tybsc302 user06 user108 user2 user31 user43

Page 101: Linux p Arctic Al Notes

E)Change the group of file and directories of different users.[tybsc308@linserver tybsc308]$ ls -l student-rw-r--r-- 1 tybsc308 tybsc 109 Jan 19 11:58 student

[root@linserver tybsc308]# chgrp ashwini /home/tybsc308/student

[tybsc308@linserver tybsc308]$ ls -l student-rw-r--r-- 1 tybsc308 ashwini 109 Jan 19 11:58 student

F)Construct the command to change form one particular user to other user.[root@linserver tybsc308]# su tybsc308[tybsc308@linserver tybsc308]$ pwd/home/tybsc308 [tybsc308@linserver tybsc308]$ cd /home/tybsc308[tybsc308@linserver tybsc308]$

G)Change the status of the normal user to the super user.[tybsc308@linserver tybsc308]$ suPassword:[root@linserver root]#

H)Change the password of the normal user. [root@linserver tybsc308]# passwd tybsc308Changing password for user tybsc308.New password:Retype new password:passwd: all authentication tokens updated successfully.