0% found this document useful (0 votes)
7 views58 pages

ULP Lab File

The document provides a comprehensive guide to Unix/Linux commands and the vi editor, detailing various commands for logging in, managing files, and executing shell scripts. It includes practical exercises for developing simple shell programs using Bash, along with examples of scripts for various tasks such as displaying user information and performing arithmetic operations. Additionally, it covers the vi editor's modes and commands for editing text files.

Uploaded by

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

ULP Lab File

The document provides a comprehensive guide to Unix/Linux commands and the vi editor, detailing various commands for logging in, managing files, and executing shell scripts. It includes practical exercises for developing simple shell programs using Bash, along with examples of scripts for various tasks such as displaying user information and performing arithmetic operations. Additionally, it covers the vi editor's modes and commands for editing text files.

Uploaded by

freefire01ashu
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Practical No.

-1

Problem Statement:

Familiarize with Unix/Linux Log In/log Out and various other commands& vi editor.

Software and Hardware Requirements:

2 GHz dual core processor

2 GB RAM (system memory)

25 GB of hard-drive space (or USB stick, memory card or external drive but see LiveCD for an
alternative approach)

VGA capable of 1024x768 screen resolution

Either a CD/DVD drive or a USB port for the installer media

Internet access is helpful

Commands:

1. $login:
Login: piet
Password: 123

2. $ logout or Exit: To logout from the command line.

3. $passwd:
Changing password for current user
(Current) Linux passwd-123
New passwd:456

4. $date: Display current system date and [Link] Sep 21 17:57:15 EDT 2016

5. $cal: To display calendar.

6. $cal 5 2016: Display calendar of May 2016.

7. $ who: The information of users who is currently logged into the [Link] :0 2015-08-
25 17:46

8. $ who am i:
piet pts/2 2015-08-25 17:46

9. $ who -Hu:
Name Line Time IDLE PID COMMENT piet pts/1 2016-08-
25 17:46 3456(.00)

1 |P a g e
10. $ date:
$ date “+ my birthday is %d %b %Y”
My birthday is 23 sep 16
Year-
Y- full year 2016 y-
Abb year 16 Month-
B-full month m-
numeric month day
of month-d
TIME- Hours-H
Minute-M Second-s

11. $ echo:
-Display the message on the terminal screen
-it does not recognize the blank space
-without an argument pull a blank line

$ echo how are you


how are you
$

12. $ tty:
Linux treats everything as files, even the terminal connected are represented as files.
$tty
/dev/pts/1
$
Here terminal filename is 1

13. $ tput:
Command is used to control the cursor on the screen.
$ tput clear-clear the screen
$ tput cup 10 5 –cup is an argument along with 10 th row and 5th
column $ tput smso-start bold sequence $ tput rmso –start normal
sequence

14. $ uname:
(i) $ uname
Linux
$
(ii) $ uname – r
2.4.18
This reption gives the version number of machine.
(iii) $ uname –m i
686
This gives the detects of machine (iv) $
uname -a
Gives all detail of the system

2 |P a g e
15. $ bc:
-It is for calculator and small language for writing numerical programs. -After
using this command use CTRL+D for exit.
$ bc
60+6
66
[Ctrl+D]
$

16. $script:
This command is used to record an interactive session.
$script
Script started, file is typescript
$ exit
Script done, file is typescript 17.
$ more typescript:
With this command we can see the recorded session 18.
$ man:
It gives format and manual page of command. It includes NAME, DESCRIPTION and
SYNOPSIS.
$ man command_name 19.
$stty:
Display the setting of terminal
$ stty –a - print all current setting in human re adable form
20. $ ls- List the directories and files$ ls [option]….[file]…..
$ ls –a – shows all the files
$ ls –A- show almost all file
$ ls –m- comma seperates list of enteries
$ ls –r- reverse order while sorting
$ ls –s- show file by size
$ ls –t- sort by modification time
21. $ cd -used for change directory$ cd dirname
For eg: $pwd
/home/cloudera $ cd
Desktop $pwd
/home/cloudera/Deskto
p

22. $ pwd
-It gives presnt working directory For
eg:
$pwd
/home/cloudera

23. $ mkdir
-New directories can be created by mkdir command For
eg:
$ mkdir abhi: create directory abhi
$ mkdir a a/b: make a directory with a name and then sub directory b under the parent a 24.
$ rmdir

3 |P a g e
-it removes directory.
-directory must be empty
$ rm filename
25. $ mv
-Moving or renaming filename and directory
$ mv old file name new file name
26. $ cat
-It is used to create a new file, see content of a file and contents to the existing file.
$ cat filename For eg.
$ cat helloworld.c (i)
$ cat > filename
………. type…character
…….

……………….

- to create a filename

29. $ cp
Copy the content of old file to new file if new file does not exist then it first create destination
new file first and then add content of old file to new file.
$cp old_file new_file
30. To view long files
-pg, more and less are basic command to see files having more contents.
-head and tail to see beginning and ending contents of a file
(i)$ pg filename-display the content of file name
$pg -10-display maximum 10 lines for each window
$pg+10-start to display lines from line no.10
(ii)$ more filename
–show in %
-press spacde bar next window
-Ctrl+D to move forward one screen
-Ctrl+B to move backword on screen
(iii)$ head
-display first few line of one and more files
$head file1 file2 file3
= =>file1<<= =
abcd
= =>file2<<= =
abc
= =>file3<<= =
Abcd
$head sec- it display first 10 lines of sec filename
$head -15 sec- display 15 files
(iv)$ tail
$tail filename1..2
-Display last few lines of one or more files
$tail sec
-it display last 10 lines of sec filename
31. $wc:- $wc filename
32. $cmp

4 |P a g e
To compare files we use cmp command. If the contents of two or more files are same and we
want to delete files that are identical then use cmp command.
If files are differ then it will give msg like file1 file 2 differ: byte1, line1
Syntax: $ cmp file1 file2
33. $comm
$comm file1 file2 -common data in two or more files
34. $diff-
$diff file1 file2–difference betw een files and it also tells the changes you have to make to make
two files identical.

Familiarize with vi Editor.

The vi editor is the most popular editor in Linux. The current version is really "vim", but to invoke
it simply type "vi". The text format that vi uses is the plainest elementary text format that exists,
the ASCII format. But files created by vi can be run through other programs that convert the files
to other formats, if desired. In Linux and other Unix clone operating systems, the operating system
and the editor do not care whether or not file names have suffixes or extensions.

Open a terminal window in your Linux system and click on it to make sure it is active. You will
do all your work in the terminal window. If you are not already familiar with how to open a
terminal window, find what directory you are in, list the files in a directory, create a directory,
move files, copy files, rename files, delete files, execute programs, add missing commands and
Linux command line operations in a terminal window you should first read my article how to use
Linux terminal.

To edit an existing file named "xyz", or create a new file named "xyz", type "vi xyz". When in vi,
there are two major modes, the input mode and the edit mode. In the input mode, anything you
type goes into the file. In the edit mode you can move around the file, copy, delete etc. Hit the
"Esc" key to get from the input mode to the edit mode. When in the edit mode the mouse is not
used to move the cursor, keyboard commands are used. The arrow keys move the cursor up,
down, right, left. The spacebar moves the cursor to the right. "$" moves the cursor to the end of
the present line. The "enter" key moves the cursor to the beginning of the next line. "237" then
the "enter" key will move ahead 237 lines in the file, unless there are less than 237 lines in the
file, in which case the cursor will not move. Type "1G" to jump to the top of the file, "G" to move
to the end of the file.

The syntax to launch Vi is as follows:

vi name_of_the_file

Once the file is open, you can move around by using cursors or the keys h, j, k and l (in case the
keyboard does not have any arrow cursors).
When the cursor is moved to a particular letter in a word in the file, press "a" to enter the insert
mode after that letter, press "i" to enter the insert mode just before that letter. Type what you want
in the insert mode then hit the "Esc" key to leave the insert mode and get back to the edit mode.

5 |P a g e
Modes:-

1. Command mode
2. Insert mode

$ Vi [Link]

Getting out of vi (Command mode)

First press Esc key

quite : q!
write : w!

reload : e!

write and quite Shift ZZ

Moving the cursor Key Cursor movement

h left one space

j down one line

k up one line

l right one space

6 |P a g e
Editing the text

i insert before character

a insert after character

x deletes one character

dd delete current line

7 |P a g e
Practical No.-2

Problem Statement:

Develop simple shell programs using Bash or any other shell in Linux.

Software and Hardware Requirements:

2 GHz dual core processor

2 GB RAM (system memory)

25 GB of hard-drive space (or USB stick, memory card or external drive but see LiveCD for an
alternative approach)

VGA capable of 1024x768 screen resolution

Either a CD/DVD drive or a USB port for the installer media

Internet access is helpful

Commands:

How to write and execute shell scripts :-

You can write a shell scripts by text editor like vi or by using cat command

1) Write a program in shell to display output of who, cal and pwd command
$ cat > myprog #File name is myprog

who
cal
pwd
ctrl+d
$

Execute file:

We can execute shell programs in two different ways.


1. Using bash shell
$ bash [Link]

2. By making the file executable

8 |P a g e
In this method, we have to first make the file executable by giving it execution permission
using chmod command. Because initially owner of the file does not have the executable
permission.
$ chmod u+x [Link]
To run the program we use the following command. $
./myprog

You will get an error message “myprog” execution permission denied”. This is because when
a new file is created , it is created with read and write permission of owner not execute
permission . To solve this problem use chmod command to make it executable.

$ chmod 744 myprog

then

$ ./myprog

1) cat > Explicit_list.sh

for a in 1 2 3 4 5 do

echo “ welcomes $a times “

done

ctrl+d

Output :-

welcome 1 times

welcome 2 times

welcome 3 times

* First make Explicit_list.sh as executable file as $ chmod


744 explicit_list

then run by this command

$ ./explicit_list.sh

9 |P a g e
2) You can make program in vi editor also.

$ vi [Link]

for a in

{1…5} do

echo “welcome $a times “ done

Press Esc and then shift ZZ

$ chmod 744 [Link]

$ ./[Link]

Output :-

welcome 1 times

welcome 2 times

welcome 3 times

welcome 4 times

welcome 5 times

3) $ vi setting_step.sh

for a in (1..10..3) do

echo “welcome $a times “


done

$ chmod 744 setting_step


$ ./setting_step.sh

Output:-

welcome 1 times

welcome 4 times

welcome 7 times

10 |P a g e
welcome 10 times

4)Write a shell script to print first 10 natural numbers with a


step of 2.

#shell script to print numbers 1 to 100


i=1
while [ $i -le 100 ] do

echo $i
i=$(($i+1))
done

11 |P a g e
5)Write a shell script to print first 10 natural numbers with a
step of 2.

#shell script to print numbers 1 to 100


i=1
while [ $i -le 100 ] do

echo $i
i=$(($i+1))
done

6)Add two variables using command line argument in


shell script

Code:
#!/bin/bash
#shell script to add two numbers #using
command line arguments
#$1 refers to the first argument
#$2 refers the second argument and so on
sum=$(($1 + $2)) echo "Sum = $sum"
Output:

7)Write a Shell Script to create a directory Code:


#!/bin/bash mkdirnewdir&> /dev/null if [ $? -eq 0 ] #$? contain Zero if
previous command execute successfully
#otherwise it contains non-zero value
then echo "Directory created successfully" else echo
"Directory already exist" fi
Output:

12 |P a g e
8)Positive or Negative - Shell Script

Code:
#!/bin/bash echo
"Enter a Number" read
num
if [ $num -lt0 ]
then echo
"Negative" elif [
$num -gt0 ] then
echo "Positive" else echo "Neither
Positive Nor Negative" fi

Output:

8) Shell script for finding greatest of three numbers

Code:

#!/bin/bash echo
"Enter Num1" read
num1 echo "Enter
Num2" read num2
echo "Enter Num3"
read num3
if [ $num1 -gt $num2 ]&& [ $num1 -gt $num3 ]
then echo $num1
elif [ $num2 -gt $num1 ]&& [ $num2 -gt $num3 ]
then echo $num2 else echo $num3
fi
Output:

13 |P a g e
9) Shell Script to print numbers 1 to 10 using
(i) while loop

Code: Output:

#!/bin/bash i=1 while [ $i -le 10 ] do echo $i


i=$(($i+1)) done

(ii) for loop

Code: Output:

#!/bin/bash for((i=1;i<=100;i++)) do echo $i


done

10) Shell script to print sum of all digit

Code:

#!/bin/bash echo
"Enter a number" read
num sum=0
while [ $num -gt0 ] do mod=`expr $num % 10` #It
will split each digits sum=`expr $sum + $mod`

14 |P a g e
#Add each digit to sum num=`expr $num / 10`
#divide num by 10.
done echo

$sum

Output:

11) Shell script to find factorial of a number

Code:

#!/bin/bash echo
"Enter a number" read
num fact=1 while [
$num -gt1 ]
do fact=$((fact *num)) #fact = fact *
num num=$((num-1)) #num = num
- 1 done echo $fact Output:

15 |P a g e
12) Shell script to add and access array elements

Code:

#!/bin/bash arr=(10
20 30 40 50) sum=0
foriin ${arr[@]} do
sum=`expr $sum + $i`
done echo $sum

Output:

13) Shell program to add two numbers using


functions

Code:

#!/bin/bash
functionadd() {
sum=$(($1 + $2))
echo "Sum = $sum"
} a=10
b=20
#call the add function and pass the values
add $a $b Output:

14) Shell script to compare two strings

Code:

#!/bin/bash read -p "Enter two


strings: " str1 str2 if [ $str1 == $str2
] then echo "Equal" else echo
"Un Equal"

16 |P a g e
fi

Output:

17 |P a g e
Practical No.-3

Problem Statement:

Develop advanced shell programs using grep, fgrep & egrep.

Software and Hardware Requirements:

2 GHz dual core processor

2 GB RAM (system memory)

25 GB of hard-drive space (or USB stick, memory card or external drive but see LiveCD for an
alternative approach)

VGA capable of 1024x768 screen resolution

Either a CD/DVD drive or a USB port for the installer media

Internet access is helpful

Commands:

GREP:-GREP stands for global regular expressions print. It is a filter which is capable to
receiving its input from standard input and sending its output to standard output.

1. GREP
2. EGERP- Extended GREP
3. FGREP- Fixed GREP

1. GREP:- This command is used to search or extract lines or records from an input [Link]
means entire file is searched for specified patterns

$ grep [options] pattern filename/filenames

2. Searching single word:-

$ grep for largest.C

OUTPUT:

Press 1 for addition

Press 2 for addition

3. Searching multi word:-

$ gep “RK bakshi” teacher

18 |P a g e
OUTPUT:

RK bakshi

[Link] options:-

4.1 Inverse option(-v) :- It will print those lines that do not contain specified patterns.

$ grep –v “dp” teachers

OUTPUT:

Rajender

Pawan

Mukesh

Anil

4.2 C ignore option(-i) :- GREP distinguish between uppercase and lowercase. With i optionit
will print all patterns without considering the case.

$ grep –I “DP” teachers

OUTPUT:

Dp gupta

4.3 Filename option(-l) :- Print the filename in which specified pattern is present.

$ grep –l “dp” student teachers for.c

OUTPUT:

teachers

4.4 count occurances(-c) :- count the occurances of records.

$ grep -c ”dp” teachers

OUTPUT:

01

4.5 Line number option(-n) :- This option will print out the line number with the record
thatcontain the specified pattern.

19 |P a g e
$ grep –n “for” largest.C for

OUTPUT:

largest.C :11: Press 1 for addition

largest.C :12: Press 2 for subtraction

for :1: for a in 1 2 3 4 5

4.6 Multiple pattern search option(-e) :- This pattern option allow us to search multiplepatterns
within a single command.

$ grep –e “dp” –e “rk” teachers

OUTPUT:

dp gupta

rk bakshi

2. EGERP:-

Extended GREP because it include two additional quantifier “?” and “+”. This is most powerful
command of grep family. It also include search for grep family. It also includes
search for multiple patterns. For this we use pipe character “|”.

2.1 Multiple patterns search:-

$ egerp “rk|dp” teachers

OUTPUT:

rk bakshi

dp gupta

2.2 uses of additional quantifiers “+” and “?”:- + quantifier matches one or moreoccurrences
of previous character. For example ‘at’ matches a, aa, aaa and so on.

? Quantifier matches zero or one occurrence of previous character.

3. FGREP:-

Fixed Grep. It is used for fixed character patterns searching and does not make use of any
quantifiers. It is the fastest among entire pattern searching techniques.

$ fgrep “rk

20 |P a g e
> dp

> rajender

“teachers Or

First make a file e.g [Link]


$ cat>[Link]

rk
dp

rajender ctrl+d

$
Now we can search these words or patterns with –f o ption
$ egrep –f mp teachers

where mp is file1 and teachers is file2

21 |P a g e
Practical No.-4

Problem Statement:

Compile and debug various C language based programs using ‘makefile’ & ‘debug’ utility.

Software and Hardware Requirements:

2 GHz dual core processor

2 GB RAM (system memory)

25 GB of hard-drive space (or USB stick, memory card or external drive but see LiveCD for an
alternative approach)

VGA capable of 1024x768 screen resolution

Either a CD/DVD drive or a USB port for the installer media

Internet access is helpful

Commands:

Make a C Program:

We can make a C Program by 3 options:

1. Right click on Desktop→ Create Document→ Empty File. Open the file and write C
program and save it with .C extension.
2. Write a program using Cat command as,
$cat>largest.C
3. Write a program using vi Editor as,
$ vi largest.C

Run a C Program:

Using gcc Compiler.

gcc means GNU Compiler Collection.

gnu means ‘GNU’ Not Unix/Linux OS.

Program 1: hello.C

#include <stdio.h>
void main() {
printf(“Welcome”);
}

22 |P a g e
$gcc hello.C:- This command compile the hello.C program. If we want to run this program than
we have to run [Link] as,

$./[Link]

Output: Welcome

*/Note: Every C Program has the same output file as [Link] with rwx rwx r-x means 775 file
permission. Means user and group can read, write and execute every C program. But other
can read and execute program not write. /*

If we want to give every C Program different output file name then use this option for Compile

$gcc –o lar largest.c or $gcc largest.c – o lar

o is for ‘output’.

lar is Output File.

largest.c is source program.

Program 2: largest.C

#include<stdio.h> void
main()
{
int a,b; printf(“Enter the values of a
and b”);
scanf(“%d %d”,&a,&b); if(a>b)
{
printf(“a is greater”); else
printf(“b is greater”); }

Run this program as,

$./lar

Output: Enter the values of a and b

20 13
a is greater

23 |P a g e
Makefile Utility: -

Makefile is a program building tool which runs on Unix/Linux. It aids in simplifying building
program executables that may need various modules. To determine how the modules, need to
be compiled or recompiled together, make takes the help of user-defined makefile. This tutorial
should enhance your knowledge about the structure and utility of [Link] guides the
make utility while compiling and linking program modules.

24 |P a g e
Process of Makefile:
1. Suppose, we have 3 files main.c (main source file), function1.c, function2.c and function3.c
(source files that contains function definition), functions.h (that contain function
declaration).
2. Here we will execute a program named Square root of number.
3. Makefile to compile these files. all:
gcc – o square main.c function1.c function2.c function3.c
4. Compile program/project using makefile
$ make

Debug utility:-
1. Compile your C program with -g option. This allows the compiler to collect the
debugging information.
a. gcc –g factorial.c
2. Launch the C debugger (gdb) as shown below.
a. gdb [Link]
3. Set up a break point inside C program
a. Syntax: break line_number
4. gdb command shortcuts

25 |P a g e
Practical No.-5

Problem Statement:

Learning of installation of dual operating system with Linux having previously installed
other window based operating system. Both OSs should be in working operating mode.

Software and Hardware Requirements:

2 GHz dual core processor

2 GB RAM (system memory)

25 GB of hard-drive space (or USB stick, memory card or external drive but see LiveCD for an
alternative approach)

VGA capable of 1024x768 screen resolution

Either a CD/DVD drive or a USB port for the installer media

Internet access is helpful

Theory:

1. Overview: The Ubuntu desktop is easy to use, easy to install and includes everything youneed
to run your organisation, school, home or enterprise. It's also open source, secure, accessible and
free to download. 2. Requirements

You'll need to consider the following before starting the installation:

• Connect your laptop to a power source.

• Ensure you have at least 25GB of free storage space, or 5GB for a minimal
installation.

•Have access to either a DVD or a USB flash drive containing the version of Ubuntu
you want to install.
· Make sure you have a recent backup of your data. While it's unlikely that anything will
go wrong, you can never be too prepared.

There are two ways of setting up your dual boot -

Install Ubuntu after Windows 7


From my experience, this is the best approach to dual-boot Windows 7 and Ubuntu.

1. Make sure you have Windows 7 installed and running on your PC.
2. [OPTIONAL] You can prepare your disk by making available a free partition for
yourUbuntu. You may use the Disk management tool on Windows to do this or employ a

26 |P a g e
third party app such as Paragon Partition Manager or EaseUS Partition Manager. You
need at least 10GB of free space.

3. Download the version and flavor or Ubuntu you want to install.

4. Make a bootable USB or disc of the Ubuntu iso you have downloaded. You can use Rufus
tool on windows or you c an use the Startup Disk Creator if you have an existing Ubuntu or
Linux installation.

27 |P a g e
5. Reboot your PC with
the bootable
USB or disc.
6. Proceed with the install
ation until you get to
Installation type. If you
created a partition
already (Step 2), then
choo se it at your target
install and continue the
p rocess. If you skipped
Step 2, choose one of
a. Choose the option
which says the
followings -
alongside
“Install Ubuntu partition size by dragging the slider at them” and then specify your the
bottom.

b. Choose “Something else”. Select the partition yo u want to resize and enter the Size in GB and
then press Enter to shrink your existing partition. You need at least 10GB of space for Ubuntu
and then another 4-8GB for SWAP. Use your installed RAM as a guide for SWAP.
Create a partition for your Ubuntu and then set the rest of free space as SWAP. Select "Finish
partitioning and write changes to disk".

28 |P a g e
7. Continue and then finish your Ubuntu Installation.

8. Your grub installation will automatically look for other installed operating systems and hence
will discover your existing Windows 7. Upon boot up, you will be presented with the option to
select your OS of choice.

Installing Windows After Ubuntu


So you can go the other w ay round and install your Ubuntu first befo re installing Windows. The
problem with this is that the Master Boot Record will override your Grub installation from
Ubuntu.

29 |P a g e
30 |P a g e
Practical No.-6

Problem Statement:

As supervisor create and maintain user accounts, learn package installation, taking backups,
creation of scripts for file and user management, creation of startup and shutdown scripts
using at, batch, cron etc

Software and Hardware Requirements:

2 GHz dual core processor

2 GB RAM (system memory)

25 GB of hard-drive space (or USB stick, memory card or external drive but see LiveCD for an
alternative approach)

VGA capable of 1024x768 screen resolution

Either a CD/DVD drive or a USB port for the installer media

Internet access is helpful

Theory:

1. Create and maintain user account :- We cannot create users on non-root terminal. If
we try to create user at non-root then we can get the message_ permission denied. So try to be
in root login or move from non-root to root with command Su(Super user or root) # :- Root or
super use
$ :-non root user or simple user
[rajender@localhost Desktop] $ useradd piet : permission denied
[rajender@localhost Desktop] $ Su Password:
[root@localhost Desktop]#
# hash mean we are at root user and we can create user here.
[root@localhost Desktop]# useradd PIET [root@localhost
Desktop]# passwd PIET
Changing password for user PIET New
password:
Retype new password:
Passwd: all authentication tokens updated successfully.
[root@localhost Desktop]#
Useradd :- command to create user account
[Link] :- command to set passwprd for user account.

To delete user account :-


[root@localhost Desktop]# we are at root or admin but shell is here of localhost.

31 |P a g e
[root@localhost Desktop]# userdel_PIET This command will delete the user account. 2.
Switching between users :- If we want to switch between useraccounts then
use[]$ Su –useraccount.
$ Su - [to switch from non root user to root]

[root@localhost ~]#
~ This sign indicate that we are using Root shell and # means root.
3. Software package installation :- We can install different package at the time of
installation of OS and later on as well. Different package of Linux OS are:-
Desktop Environment --> GNOME,KDE
Applications--> Editors , games, graphics, sound and video
Development--> Development tool, Software development
Server--> FTP, web server, mail server, DWS Base
system--> Java, base, system tool
Language--> Hindi, English, Punjabi
4. Creation of script for file :-
$ script This command is used to record the interactive session.
$ script
Script started, file is typescript
___
___
___
Exit
$ script done, file is typescript
The name of the file is typescript and always when we want to record the session it will be save
in typescript file so always give a new name to this typescript file after we recorded the last session.
We can see the record session by
$ more typescript

Creation of startup and shutdown scripts using at, batch, cron etc.

1. at command
This command is capable of executing Linux command at a future date and time. The input
to this command comes from the standard input like keyboard. Then the command typed.
Once the job is submitted using at command, all the details regarding job id no., date and time
at which commands are executed to be displayed.
For example:
$ at 10:45 Jun 23 2021 at>
cat>[Link]
ctrl+d
job 18 submitted at 2021-06-23 10:45
$
So [Link] file created at 10:45 on Jun 23, 2021. This is future time to execute a
command.
2. batch command
Job submitted by using batch command are executed when the system is relatively free and
system load is light. Time at which command will be executed is decided by the system.
$ batch

32 |P a g e
at> grep unix [Link]
ctrl+d
job 16.b at 2021-06-23 11:15
$
The extension. b attached to a job identification number indicated that the job has been
submitted using batch command.

3. cron tab command

Cron is a daemon that keep sleeping most of the time. It executes commands that need to be run regularly
according to the time schedule. The schedule and corresponding time is stored in the file /etc/[Link]
daemon wakes up every minute and check its crontab file for any job that need to be executed during this
minute.
Every entry in crontab file /etc/crontab contain six fields separated by space or tab in the following form.

1 2 3 4 5 6
Minute Hour Day of Month Month day of week command to be executed

$ crontab –l
no crontab for rajender
$
This file is submitted to crontab as:
$ crontab contentfile

$ crontab -l list user’s crontab


-e edit user’s crontab
-r delete user’s crontab
-i prompt before deleting user’s crontab
-s selinux context

33 |P a g e
Practical No.-7

Problem Statement:

Familiarize with zip, unzip, compress and uncompress commands.

Software and Hardware Requirements:

2 GHz dual core processor

2 GB RAM (system memory)

25 GB of hard-drive space (or USB stick, memory card or external drive but see LiveCD for an
alternative approach)

VGA capable of 1024x768 screen resolution

Either a CD/DVD drive or a USB port for the installer media

Internet access is helpful

Theory:

ZIP is a compression and file packaging utility for Unix. ... zip is used to compress the files
toreduce file size and also used as file package utility. zip is available in many operating systems
like unix, Linux, windows etc.

ZIP is a compression and file packaging utility for Unix. Each file is stored in single .zip
{.zipfilename} file with the extension .zip.

• zip is used to compress the files to reduce file size and also used as file package
utility.
zip is available in many operating systems like unix, linux, windows etc.
• If you have a limited bandwidth between two servers and want to transfer the files
faster, then zip the files and transfer.
• The zip program puts one or more compressed files into a single zip archive, along
with information about the files (name, path, date, time of last modification, protection, and
check information to verify file integrity). An entire directory structure can be packed
into a zip archive with a single command.
• Compression ratios of 2:1 to 3:1 are common for text files. zip has one compression
method (deflation) and can also store files without compression. zip automatically chooses the
better of the two for each file to be compressed. The program is useful for packaging a set of
files for distribution; for archiving files; and for saving disk space by temporarily compressing
unused files or directories.

Syntax:
zip [options] zipfile files_list

34 |P a g e
Syntax for Creating a zip file:

$zip [Link] [Link]

Extracting files from zip file

Unzip will list, test, or extract files from a ZIP archive, commonly found on Unix systems.
The default behaviour (with no options) is to extract into the current directory (and sub-
directories below it) all files from the specified ZIP archive.

Command for ZIP

$ zip file [Link]

Command for UNZIP

$ unzip file [Link]

File Compression:

Sometimes it is useful to store a group of files in one file so that they can be backed up, easily
transferred to another directory, or even transferred to a different computer. It is also sometimes
useful to compress files into one file so that they use less disk space and download faster.

It is important to understand the difference and relationship between an archive file and a
compressed file. An archive file is a collection of files and directories that are stored in onefile.
The archive file is not compressed — it uses the same amount of disk space as all the individual
files and directories combined. A compressed file is a collection of files and directories that are
stored in one file and stored in a way that uses less disk space than all the individual files and
directories combined. If you do not have enough disk space on your computer, you can compress
files that you do not use very often or files that you want to save but do not use anymore. You
can even create an archive file and then compress it to save disk space.

File Compression advantages:

Compressed files use less disk space and download faster than large, uncompressed files. In Red
Hat Linux you can compress files with the compression tools gzip, bzip2, or zip.

The bzip2 compression tool is recommended because it provides the most compression and is
found on most UNIX-like operating systems. The gzip compression tool can also be found on
most UNIX-like operating systems. If you need to transfer files between Linux and other
operating system such as MS Windows, you should use zip because it is more commonly used
on these other operating systems.

35 |P a g e
Compression Tools

Un-compression Tool

Compression Tool File Extension

.gz Gunzip
Gzip

bzip2 .bz2 bunzip2

.zip Unzip
Zip

Command for Compress

$ compress file [Link]

Command for Un-Compress

$uncompress file [Link]

36 |P a g e
Practical No.-8

Problem Statement:

Familiarize with file permissions in Linux to keep users file secure from unauthorized access.

Software and Hardware Requirements:

2 GHz dual core processor

2 GB RAM (system memory)

25 GB of hard-drive space (or USB stick, memory card or external drive but see LiveCD for an alternative
approach)

VGA capable of 1024x768 screen resolution

Either a CD/DVD drive or a USB port for the installer media

Internet access is helpful

Theory:

File Permissions:

In a multiuser environment like Linux it becomes important for user to keep their files secure.
When a user creates a file, he becomes the owner of that file. Each user belongs to a group. A
user can also access to other groups. Hence there are three types of users in Linux- first is owner,
second is group and third others.

Types of Permissions:

Every file has 03 types of permissions associated with it:

1. Read
2. Write
3. Execute

These permission may be granted to all 03 categories of users. If you have read permission you
can view its contents. Write permission allow you change the file contents. Execute permission
means that you can run the file just like a program. Example: Shell program or C program.

Display of File Permission:

37 |P a g e
$ls –l: ls command with –l option displays the permissions for the file. Note that the
outputdisplayed in the first column. The first character tells you about the type of the file. The
remaining 09 characters represent the permissions. The file permissions are divided into 03 sets
of

rwx rwx rwx

The first set is for owner.

Second set is for group.

Third set is for others.

File Permission: rw- r-x r- -

It means the owner of the file has read and write (modify) permission but do not have permission
to run(execute) the file as a program.

Group can read and execute file but cannot write.

Other can only read and execute file but cannot write (modify) or run (execute) the file as program.

Dash (-) in each set of permission represents the denied permission.

Change File Permission with Chmod Command:

In linus you can change the file permissions by using chmod command. You can allow or prevent
yourself (owner), group or others from reading, writing and executing the file. In chmod
command, first specify for whom you are changing the permission,

u for user

o for others

Second you have to specify whether you want to allow or prevent the permission.

+ (Plus) sign means allow.

- (Minus) sign means denied.

Third specify the name of file.

$ls –l: This command will list all the files and directories.

Output:

38 |P a g e
File→ -rw-rw-r--.1 rajender rajender 28 Sep 7 22:32 name

Directory→ drwxrwxr-x.2 rajender rajender 22 sep 8 10:10 Hello

The name file has initially rw-rw-r—permission. Itmeans this file has not execute permission of
owner and group and It cannot modify nad execute by others.

If we want to execute this file by user (owner) and group then we have to write a command as

rw-rw-r—

$ chmod ug+X name2:

u for user
g for group

+ means permission allow X


means execute permission
name2 is filename.

This command will allow user and group to execute the name2 file as program. After this command
the file permission of file name2 will look like rwxrwxr—

$ chmod u+X name2

Or

$ chmod 774 name 2

If you want to set permissions to all types of user then use


$ chmod a+r: [Set read permission to all users.]

$ chmod a-r: [Denied read permission to all users.]

$ chmod –R u+rw: [Change file permissions of all fi les.]

File Permissions in Octal Code:

39 |P a g e
We can also set file permission using octal codes. In this code 03 digits are used.

One for owner (user), one for group and one for others.

Owner (u) Group (g) Other (o)

Read(r) 4 0 0

Write (w) 2 0 0

Execute (e) 1 0 0

Sum 7 0 0

Each column represents a user and each row represents a file permission. If user has read
permission the owner 04 is added in that column, to set written permission 2 is added, to set
execute permission 1 is added the sum of each column is the code of that user permission.

$ chmod 700 abc


Each digit in “700” represents the permission. The first digit is for user, second for group, third for
others.

$ chmod 750 letter

Owner (u) Group (g) Other (o)

Read(r) 4 4 0

Write (w) 2 0 0

Execute (e) 1 1 0

Sum 7 5 0

The above chmod command set read, write and execute permission for owner and read and execute
permission for the group.

$ chmod 774 letter

40 |P a g e
It means read, write and execute permission for owner and group and only read is set for others.

41 |P a g e
Practical No.-9

Problem Statement:

Familiarize with SED and its Commands in Linux.

Software and Hardware Requirements:

2 GHz dual core processor

2 GB RAM (system memory)

25 GB of hard-drive space (or USB stick, memory card or external drive but see LiveCD for an alternative
approach)

VGA capable of 1024x768 screen resolution

Either a CD/DVD drive or a USB port for the installer media

Internet access is helpful

Theory:

SED Command in UNIX/LINUX :

Sed is a stream editor used for modifying the files in UNIX or LINUX.
Whenever you want to make changes to the file automatically, sed is helpful in that . Apart
from replacing text, you can do many things with sed.

Features of SED with example :-

Consider the below text file as an input .

cat [Link]

unix is great OS. unix is open source. unix is free OS.

learn operating system.

unixlinux which one you choose.

(1) Replacing or substituting stringIt is mostly used to replace the text in a file.$ sed
‘s/unix/linux/’ [Link] linux is great OS. unix is open
source. unix is free OS. learn operating system.

42 |P a g e
linuxlinux which one you choose.

Here ‘s’ specify substitution operation.

By default sed command replaces the first occurrence of pattern of each line and it would not
replace the second, third ….. occurrence in the line.

(2) Replacing the nth occurrence of a pattern in a line

$ sed ‘s/unix/linux/2’ [Link]

unix is great OS. linux is open source. unix is free OS.

learn operating system.

unixlinux which one you choose.

(3) Replacing all occurrence of a pattern in a line

$ sed ‘s/unix/linux/g’ [Link]

linux is great OS. linux is open source. linux is free OS.

learn operating system.

linuxlinux which one you choose.

(4) Replacing from nth occurrence to all occurrences in a line

$ sed ‘s/unix/linux/3g’ [Link]

unix is great OS. unix is open source. linux is free OS.

learn operating system.

unixlinux which one you choose.

(5) Using ‘&’ as the matched string

$ sed ‘s/unix/{&}/’ [Link]

43 |P a g e
{unix} is great OS. unix is open source. unix is free OS.

learn operating system.

{unix}linux which one you choose.

$ sed ‘s/unix/{&&}/’ [Link]

{unixunix} is great OS. unix is open source. unix is free OS.

learn operating system.

{unixunix}linux which one you choose.

(6) Switch words


If you want to switch the words “unix linux” as “linuxunix”
the sed ommand is :

$ sed ‘s/ \(unix\)\(linux\)/\2\1/’ [Link]

unix is great OS. unix is open source. unix is free OS.

learn operating system.

linuxunix which one you choose.

(7) Switching First 03 characters in a line

$ sed ‘s/ \(.\)\(.\)\(.\)/\3\2\1/’ [Link]

inux is great OS. unix is open source. unix is free OS.

learn operating system.

inuxlinux which one you choose.

(8) Duplicating the replaced line


Each line is displayed 02 times with /p tag.

$ sed ‘s/unix/linux/p’ [Link]

linux is great OS. unix is open source. unix is free OS.

44 |P a g e
linux is great OS. unix is open source. unix is free OS.

learn operating system.

linuxlinux which one you choose.

linuxlinux which one you choose.

(9) Printing only replaced line

$ sed -n ‘s/unix/linux/p’ [Link]

linux is great OS. unix is open source. unix is free OS.

linuxlinux which one you choose.

(10) Running Multiple sed commands

$ sed ‘s/unix/linux/’ [Link] | sed ‘s/os/system/’

linux is great OS. unix is open source. unix is free OS.

learn operating system.

linuxlinux which one you chosysteme.

(11) Multiple sed commands with –e option

$ sed -e ‘s/unix/linux/’ -e ‘s/os/system/’ [Link]

linux is great OS. unix is open source. unix is free OS.

learn operating system.

linuxlinux which one you chosysteme.

(12) Replacing string on a specific line no.

45 |P a g e
$ sed ‘3 s/unix/linux/’ [Link]

unix is great OS. unix is open source. unix is free OS.

learn operating system.

linuxlinux which one you choose.

Replacing with range of 1 to 3 line

$ sed ‘1,3 s/unix/linux/’ [Link]

linux is great OS. unix is open source. unix is free OS.

learn operating system.

linuxlinux which one you choose.

(13) Delete line from file

$ sed ‘2 d’ [Link]

unix is great OS. unix is open source. unix is free OS.

unixlinux which one you choose.

(14) Duplicating lines

$ sed ‘p’ [Link]

unix is great OS. unix is open source. unix is free OS.

unix is great OS. unix is open source. unix is free OS.

learn operating system.

learn operating system.

unix linux which one you choose.

unix linux which one you choose.

46 |P a g e
(15) Sed as grep command

$ sed -n‘/unix/p’ [Link]

unix is great OS. unix is open source. unix is free OS.

unix linux which one you choose. OR

$ grep ‘unix’ [Link]

unix is great OS. unix is open source. unix is free OS.

unix linux which one you choose.

(16) Add a line after a match

$ sed ‘/unix/a “Add a new line” ’ [Link]

unix is great OS. unix is open source. unix is free OS.

“Add a new line”.

learn operating system.

unix linux which one you choose.


“Add a new line”.

(17) Add a line before a match

$ sed ‘/unix/i “Add a new line” ’ [Link]

“Add a new line”.

unix is great OS. unix is open source. unix is free OS.

learn operating system.

“Add a new line”.


unix linux which one you choose.

47 |P a g e
(18) Change a line with “c” option
$ sed ‘/unix/c “change line” ’ [Link]

“change line”.

learn operating system.

“change line”.

48 |P a g e
Practical No.-10

Problem Statement:

Familiarize with AWK and its commands in Linux.

Software and Hardware Requirements:

2 GHz dual core processor

2 GB RAM (system memory)

25 GB of hard-drive space (or USB stick, memory card or external drive but see LiveCD for an
alternative approach)

VGA capable of 1024x768 screen resolution

Either a CD/DVD drive or a USB port for the installer media

Internet access is helpful

Theory:

AWK (Aho, Weinberger and Kernigham) is a filter Program that was originally developed in
1977 as a pattern scanning language. It is a programming language with block control structures,
functions and variables.

Syntax of an AWK statement:

$ awk options “pattern{action}” filename/s

Where

1. Use of option is optional.


2. Filenames will have zero or more files.
3. Pattern specifies the basis for line or record selection and manipulation.
4. Action statement is surrounded by curly bracket {}.

$ awk ‘{print}’ [Link]

49 |P a g e
This command will show the complete records of [Link]

2. Show specific column of file:

$ awk ‘{print $2, $5}’ [Link]

Name Salary
A $20000
B $25000
C $26000
D $15000
E $11000

3. Use of inbuilt function (NF)

NF total no. of field

$awk ‘{print $2, $NF}’ [Link]

Name Salary
A $20000
B $25000
C $26000
D $15000
E $11000

4. Specific line records

$ awk ‘ $ 1>3’ [Link]

[Link]
Sr. No. Name Designation Department Salary
4 D AP MECH $15000
5 E AP CIVIL $11000

5. Print complete line with column match word

$awk ‘$4~/civil/’ [Link]

50 |P a g e
[Link]
Sr. No. Name Designation Department Salary
5 E AP CIVIL $11000

This command will print the complete line if there is a match of column 4=civil, otherwise we
will not get any records.

6. Print no. of employee from CSE

$awk “BEGIN {count=0;}” $4~/CSE/{count++;}

>END {print “no. of employee from CSE are=” count;}[Link]

Output

No. of employee from CSE are=2

7. Use of tab and new line character

$awk “BEGIN {print “ name \t designation”;} {print $2, “/t”, $3}

>END {print “report generated/n-------------- - ”;}” [Link]

Output:

Name Designation
A AP
B AP
C AP
D AP
E AP

Report generated

-------------------------------

8. Use of NR inbuilt function

NR no. of seconds or count lines in a file

$awk “{print NR}” [Link]

51 |P a g e
2

$awk “END {print NR}” [Link]

6 display only total lines in a file

9. Use of OFS inbuilt variable

OFS output field separator

$awk “BEGIN {OFS= “::”}{ print$1, $5}” empl [Link]

Output:

S. No. :: Salary

1 :: $20000

2 :: $25000

3 :: $26000

4 :: $15000

5 :: $11000

2. Program “ square of a number” :

$ awk “BEGIN {for(i=1; i<=5; i++) print “s quare of” i, “is”, i*i}”

Output:

sqaure of 1 is 1

52 |P a g e
sqaure of 2 is 4

sqaure of 3 is 9

sqaure of 4 is 16

sqaure of 5 is 25

53 |P a g e
Practical No.-11
Problem Statement:
Familiarize with PERL and its commands in Linux. Software
and Hardware Requirements:
2 GHz dual core processor

2 GB RAM (system memory)

25 GB of hard-drive space (or USB stick, memory card or external drive but see LiveCD for an
alternative approach)

VGA capable of 1024x768 screen resolution

Either a CD/DVD drive or a USB port for the installer media

Internet access is helpful

Theory:

PERL stand for Practical Extraction and Report Language. It was developed in 1986 by Larry
Wall as an interpreter on UNIX. Initially it had an emphasis on system management and text
handling but with subsequent revisions Perl also become capable of handling regular expressions,
signals and network sockets too. So its program bears a similar resemblance to that of C programs.

Program: Program to use chomp operator:

Filename: chomp-operator

Without using chomp operator:

$string01= “String1 \n”;

$string02= “String2 \n”;

$string03= “String3 \n”;

Print “$string01 $string02 $string03”;

Output:

$perl [Link]

54 |P a g e
String1

String2

String3

With chomp operator:

$string01= “String1 \n”;

$string02= “String2 \n”;

$string03= “String3 \n”; Chomp


$string01;

Chomp $string02;

Chomp $string03;

Print “$string01 $string02 $string03”;

Output:

$perl [Link]

String1 String2 String3

Use of if statement:

if

print (“This is true”);

Output: This is true

Use of if-else if statement:

If (“A” gt “a”)

55 |P a g e
Print (“This is ture \n\n”);

Elseif (“B” ge “A”)

Print (“This is true again \n\n”);

Else

Print(“default \n\n”);

Output:

$perl if_else.pl
This is true again

Program: Program to find out largest of three numbers.

Filename: [Link]

Print “enter three numbers\t”;

$n1=<STDIN>;

$n2=<STDIN>;

$n3=<STDIN>;

If ($n1>$n2 && $n1>$n3)

Print”$n1 is largest”;

56 |P a g e
}

Else if ($n2>$n1 $$ n2>$n3)

Print” $n2 is largest”;

Else

Print “$n3 is largest”;

Output:- $perl [Link]

Enter three numbers 23

12

23 is largest

Program:-program of string concatenation and string length.


Filename:- [Link]

Print “enter first string \t”;


Chomp (my $str1) = <STDIN>);

Print “Length of first string $str1 is: \ t” length ($str1),”\n”;

Print “enter second string\t;

Chomp (my $ str2= <STDIN>);

Print “Length of second string $str2 is: \ t”, leng th

57 |P a g e
($str2),”\n”; My $final-str = $str1.$str2;

Print “final string is: \ t $final–str \n and lengt h of final string after concatenation is \t “, leng th
($final–str),” \n”;

Output: $ perl [Link]

enter first string jagtar

Length of first string abc is: 6

enter second string singh

Length of second string is: 5

Final string is: jagtarsingh

And length of final string after concatenation is: 11

58 |P a g e

You might also like