Basic Commands in UNIX: Ex. No: 1 Date
Basic Commands in UNIX: Ex. No: 1 Date
Basic Commands in UNIX: Ex. No: 1 Date
No : 1
Date :
Aim
Procedure
9500671930
Command : date
Example : $date
Option Meaning
Command : echo
Command : clear
Syntax : $clear
Example : $clear
Command : tput
Example : $tput clear - Clears the screen and positions the cursor at
don’t blink.
3 smso Displays the text typed in the bold face (including the system prompt)
and reverses the background color of the screen in white and
foreground with black
4 rmso Displays the text in normal format (including the system prompt) and
resets the screen color.
7 cup RC Used to position the cursor at row number ‘R’ and column number.
Command : bc
Example : $bc
20+5
25
ctrl + d
Command : who
Function : Gives us the details of who all have logged into the UNIX system
currently
Syntax : $who
Example : $who
This option list only those terminals on which the system is waiting for
1 -l
someone to log
2 -b Displays the date and time the system was brought up.
Displays the short form listing which consists of the username terminal
7 -s
number and time fields.
Command : who am i
Syntax : $who am i
Example : $who am I
Command : ps
Function : To display information about processes that are alive when we run
the command
Syntax : $ps
Restricts listing to data about processes whose user ID are given in the ud
4 -u
list
Function : To display the numerical value that corresponds to the login name.
Syntax : $id
Example : $id
Command : uname
standard output.
Example : $uname -m
Command : finger
Function : To send a mail to another user and to see a mail received from
another user.
user.
ctrl + d
users.
Ex. No : 2
Date :
A. Files & File Types and The Basic UNIX Commands Used in File Handling
Aim
To study and execute the files & file types and the basic UNIX commands used in file
handling
Procedure
Step2: Perform copy, link, move, remove and difference operations using the
following commands.
Command : cat
Command : cp
Command : ln
Example : $ln sh1 sh2 - To create an additional name called sh2 for a
file sh1.
Command : mv
Function : To move the contents of one file to another file. The source file no
Example : $mv p1 p2
Command : rm
Example : $rm temp/sh1 - Deletes the file sh1 from the directory temp.
Options used with rm command
Command : diff
Function : To display the difference between two files and also the content of
Example : $diff a1 a2
Function : To display the name of all ordinary files from the current directory
Function : To display the name of all ordinary files from the current directory
Aim
Procedure
Command : sort
Example : $ sort –r a1
Aim
Procedure:
Command : wc
Date :
Aim
Procedure
Command : pwd
Syntax : $pwd
Example : $pwd
Command : ls
Example : $ls –l
Command : mkdir
Command : rmdir
Date :
UNIX Editor
Aim
Procedure
- insert mode
- Command mode
- Ex escape mode
4. insert mode - Helps to insert the text we want to.
by the editior.
10. “ / “ - Helps to search for a particular string within the file. The
- h – left move
- l – right move
- k – up
- j – down
13. w – Moves forward by a word. Assumes that punctuations are next words.
14. W – Moves forward by a word and this identifies punctuations as a part of the
word.
16. E – Takes us to the last character of the word and ignores any punctuations
22. G – Works like the GOTO command. For, example, to goto the second line in
Screen Commands
1. Ctrl F – Moves forward by a screen. To have continuing the last two lines of the
previous screen are displayed.
2. Ctrl B – Moves backward by a screen. Here again two lines of the previous screen
are displayed.
3. Ctrl D – Moves the cursor half the screen forward.
4. Ctrl U – Moves the cursor half the screen backward.
5. Ctrl L – To clear any message that vi has displayed or to clear any system messages
that appear on the screen.
6. Ctrl G – Displays the status on the status line. Give us the name of the file we are
editing, the number of line in the file, the current line number and the percentage of
the file (in lines) that precedes the cursor.
Editing Commands
Text Insertion In Vi
In order to insert a text within the file, first we must type in “i” to make sure that we are
in the insert moode.
1. i / I – Invokes insertion mode. Text insertion possible only after we invoke the insert
mode. The capital I invokes the insert mode and insertion takes place at the beginning.
2. O – Allows insertion by creating a blank line above the current line.
3. o – Allows insertion by creating a blank line above the current line.
4. a – Used for appending the text. Text is appended after the cursor position.
5. A – Used for appending the text. Text is appended at the end of the line.
Deleting Data in vi
Suppose we want to delete a character or a word or a line that we have typed, then we go in
for the delete commands.
To use undo command, we must first ensure that we are in the command mode and
then press “u”.
To join two lines, place the cursor at the end of the first line and then give the command
“J”.
Example
1. To copy the second line in our file below the fifth line then use “:2co5”
2. To copy the first three lines below the seventh line of our file, then use “:1,3co7”
3. To move the third line of the file to the ninth line of the file then “:3mo9”
4. To move the fourth line to the seventh line of a file to the end of the file then use
“:4,7mo$”
5. To delete from the fifth line to the seventh line in a file, then use “:5,7d”
6. To delete the fourth line from the current position, then use “: .+4d”
7. To delete the fifth line above the current cursor position, then “: .-5d”
1. :q! – Quit without writing the changes into the disk file.
2. :wq! – Quit after writing the changes into the disk file.
3. :x – Replaces the old copy of the file with the new one and then quits.
4. ZZ – Makes changes and quits from the command mode
Ex. No : 5
Date :
Pipes
Aim
Procedure
A pipe is a mechanism which takes the output of a command before the pipe symbol as
the input for the next command after the pipe symbol.
command line
Example1 : $who | wc –l
Here the output of the command “who” is taken as the input for
the command and the result of that will be displayed.
Date :
Simple Filters
Aim
Procedure
1. A filter takes input from the standard input processes it and then sends the output
the standard output. Filters also take input from a file.
2. Filters also used to store the intermediate results of a long pipe.
3. We can extract specific columns of a file and can merge two or more files together
using filters.
Command : head
Command : tail
Function : To display the bottom portion of a file. By default, the
Command : sort
alphabetical order.
The pr command
Command : pr
Example : $pr bs
Command : cut
second field
Command : paste
Function : To paste line from one or more files vertically and displays
Command : join
Command : uniq
output.
unique lines.
The nl command
Command : nl
Function : Used to add the line numbers to a file. The line number will
spaces
‘##’ character.
The tr command
Command : tr
welcome to c language
$ cat a1
welcome to c language
WELCOME TO C LANGUAGE
The more command
Command : more
file a1.txt
Command : cmp
Function : Compares any two files including binary files. It takes file
names as arguments
prompt.
Function : Produces the complete index of all lines that differ between
Command : comm
Date :
To study and execute the command line parameters with simple UNIX commands.
Procedure
Examples
$ a1.sh Ravi Ram Kavitha Parveen
a1.sh
$-
1. The exit command helps in terminating the shell script and tell us the exit status of
the script.
2. If the script was executed successfully before exiting, the exit command returns a
“0” else it return “1”.
Examples
$ mkdir lab1
$ echo $?
$ rmdir lab2
$ echo $?
$-
The set command: Assigning values to positional parameters
Example
$ set A B C D E F
$ echo $1 $3 $6
ACF
$-
1. In certain situations one may give more than nine arguments in a command line.
2. This results in excess values to be assigned to the nine positional parameters.
Examples
$ set A B C D E F G H I J K
A B C D E F G H I A0 A1
$-
Command : shift
Syntax : $shift n
Examples
$ set A B C D E F G H I J K
A B C D E F G H I A0 A1
$ shift 5
F G H I J K F0 F1
$-
When used without any argument the shift command shifts the contents of the
positional parameters by just one position to the left.
In the argument number consists of more that nine, enlose it within the flower braces.
Example
$ set A B C D E F G H I J K
$ echo $1 $2 $3 $4 $5 $6 $7 $8 $9 ${10} ${11}
ABCDEFGHIJK
$-
Ex. No : 8
Date :
Aim
1. The first character in the variable name must to an alphabet and can be followed by
any number of alphabets or digits or underscores.
2. No special character are allowed within a variable name except an underscore.
3. The variables should not be a keyword.
4. No commas or blank spaces are allowed within a variable name.
5. Variable names are case sensitive
6. A variable name should not be of length more than 20 characters.
Example
$ set b
Value
Example
$set b=10
Assigning Values to the shell variables and Accessing shell variables’ value
$ b=15
$ c=5.4
$ d=abcd
$-
Example
$ a=Ravi;b=Kumar
$ c=$a$b
$ echo $c
RaviKumar
$-
$ city=chennai
$ echo "${city}"
chennai
Bangalore
sh-3.00$ exit
‘bangalore’
chennai
sh-3.00$ exit
$-
Program for global variable
$ city=chennai
$ export city
$ echo "${city}"
chennai
chennai
Bangalore
sh-3.00$ exit
chennai
$ sh
chennai
sh-3.00$
The last two commands display that the variables can be exported or passed to sub
shells, but the reverse is not true.
Unsetting Variables: The unset command
Example
Hello
$-
B. Conditional Statements
Aim
To write a shell program to find out the biggest number using the conditional
statements.
Algorithm
Program
read a
read b
if test $a -gt $b
then
else
fi
Output
[user2@jayam ~]$ sh big.sh
A is Big
[user2@jayam ~]$
C. Testing and Loops
Testing
Aim
To write a shell program to find the mode of a given file using the test command.
Algorithm
Program
read f
if [ ! -e $f ]
then
elif [ ! -r $f ]
then
elif [ ! -w $f ]
then
else
fi
Output
test.sh
ex1
[user2@jayam ~]$
For Loop
Aim
To write a shell program to calculate the sum of the 5 numbers using simple for loop.
Algorithm
Program
sum=0
for i in 1 2 3 4 5
do
done
Output
[user2@jayam ~]$
While Loop
Aim
To calculate the factorial value of the given number using while loop
Algorithm
Program
read n
i=1
f=1
while [ $i -le $n ]
do
f=`expr $f \* $i`
i=`expr $i + 1`
done
Output
"fact1.sh" [New] 11L, 121C written
Enter a number
factorial is 120
Enter a number
factorial is 24
Enter a number
factorial is 5040
[user2@jayam ~]$
D. Simple shell Program
Program1
Aim
Algorithm
Program
if [ $1 = $2 ]
then
else
fi
Output
"simple.sh" [New] 7L, 83C written
[user2@jayam ~]$
Program2
Aim
To write a simple shell program to calculate the sum of the numbers within a given
upper limit.
Algorithm
Program
read n
i=2
sum=0
do
i=`expr $i + 2`
done
sum is: 6
sum is: 12
10
sum is: 20
[user2@jayam ~]$
E. Grep and Sed commands
Grep Command
Aim
Algorithm
Program
read name
ls $name
read file
Output
"grep.sh" 9L, 181C written
Grep Operation
[user2@jayam ~]$
Sed command
Aim
Algorithm
Syntax
Output
file updated.
Aim
Description
The awk programming language is a scripting language that can be used for very simple
single-line applications to large applications. awk is a general-purpose language, but it excels at
its original task of text processing. Its same as SED but has more powerful features . We can
perform arithmetic operations and many kind of analysis of the data in our file through
command line .
Structure
pattern { actions }
...
The structure of an awk program can be split into three sections. The BEGIN section is
performed before the first line is read from the input file(s), and the END section is performed
after the last line is processed from the input file(s). Between the optional BEGIN and END
sections is the awk pattern/action section. For each input file specified, each of the patterns is
compared in order, and if a pattern matches, then its associated action is performed.
Snark:67:48147:6325:650
Jupiter:55:110000:1976:9022
Atlas:75:260000:6300:17500
Titan:98:221500:6300:15000
Minuteman III:56:65000:6300:15000
Peacekeeper:71:195000:6000:15000
We can emit lines in much the same way that sed did, but include a search expression as
the pattern and the print command as the action:
(Command Line)
Thor:65:109330:1725:10250
Thor:65:109330:1725:10250
Snark:67:48147:6325:650
Jupiter:55:110000:1976:9022
Atlas:75:260000:6300:17500
Titan:98:221500:6300:15000
Minuteman III:56:65000:6300:15000
Peacekeeper:71:195000:6000:15000
Arithmetic expressions are also possible on the data. The following example, emits
those missiles that have a range of 12,000 miles or more:
Atlas
Titan
Minuteman III
Peacekeeper
Variable Description
We can also use if for and While statements in the awk script.
G. Security Commands
Basics
Users
/etc/shadow.
User information is stored in /etc/passwd, the place that was used to store
file.
john:x:30000:40000:John Doe:/home/john:/usr/local
/bin/tcsh
user”)
Superusr access). Once you are in the superuser account, the prompt
Groups
A user has a primary group (listed in /etc/passwd), and this is the one
NIS: % ypcat group (can display all the groups and their members)
% groups uid (display the groups that uid belongs to)
File Permissions
Permissions on Directories:
What is the most secure setting for your web directories? Why?
directory.
g: group
o: others
a: all
% getfacl
% setfacl
Q: What is the default file permission assigned to the newly created files?
% umask
Q: Can we allow a user to change the owner of files to another user?
Set-UID problem.
of XYZ
umask(2), umask(1)
Date:
UNIX C
Aim
Algorithm
Program
#include<stdio.h>
main()
int child;
{
printf("Child pid %d \n",getpid());
pause();
exit(child);
Output
[user2@jayam ~]$
Aim
Algorithm
1. Start the vi editor and create a file.
2. Get the parent process group using the setpgrp() system call.
3. Invoke all the child process available in the given parent process group using fork()
system call.
4. Get and display the pid of all the child process until getting the pid of the last child
process.
5. After getting all the pids of all the child process, kill the process using the
kill(0,SIGINT) command.
Program
#include<signal.h>
main()
register int i;
int j=0;
setpgrp();
for(i=0;i<=10;i++)
if(fork()==0)
if((i%2)==0)
setpgrp();
pause();
}
kill(0,SIGINT);
Output
child process
[user2@jayam ~]$
B. File Handling
Aim
To write a C program to copy the content of the one file to the another file in UNIX
environment.
Algorithm
Program
#include<stdio.h>
int fd1,fd2,n;
char buf[512];
if(argc<3)
exit(1);
}
if((fd1=open(argv[1],0))<0)
exit(1);
if((fd2=creat(argv[2],0644))<0)
exit(1);
n=read(fd1,buf,512);
write(fd2,buf,n);
close(fd1);
close(fd2);
}
Output
#include<signal.h>
main()
register int i;
int j=0;
setpgrp();
for(i=0;i<=10;i++)
if(fork()==0)
if((i%2)==0)
setpgrp();
pause();
kill(0,SIGINT);
}
[user2@jayam ~]$
Content Beyond the Syllabus
Ex. No:1
Date :
Fibonacci Series
Aim
Algorithm
Program
read n
a=0
b=1
echo $a
echo $b
i=2
do
c=`expr $a + $b`
echo $c
i=`expr $i + 1`
a=$b
b=$c
done
Output
fibo series
fibo series
13
[user2@jayam ~]$
Ex. No:2
Date :
Aim
Algorithm
Program
#include<stdio.h>
main()
int n,a,r=0;
scanf("%d",&n);
while(n>=1)
a=n%10;
r=r*10+a;
n=n/10;
}
printf("reverse=%d",r);
Output
reverse=321
[user2@jayam ~]$
Ex. No:3
Date :
Aim
Algorithm
Program
#include<stdio.h>
main()
{
int a[3][2],b[3][2],i,j;
printf("enter value for a matrix: ");
for(i=0;i<3;i++)
{
for(j=0;j<2;j++)
scanf("%d",&a[i][j]);
}
printf("enter value for b matrix: ");
for(i=0;i<3;i++)
{
for(j=0;j<2;j++)
scanf("%d",&b[i][j]);
}
printf("\na matrix is\n\n");
for(i=0;i<3;i++)
{
for(j=0;j<2;j++)
{
printf(" %d ",a[i][j]);
}
printf("\n");
}
Output
a matrix is
3 2
5 1
6 3
b matrix is
8 9
4 5
6 1
[user2@jayam ~]$
JAYARAM
COLLEGE OF ENGINEERING AND
TECHNOLOGY
Estd: 1994
Pagalavadi, Tiruchirappalli - 621014
Department of CSE
LABORATORY II
MANUAL