0% found this document useful (0 votes)
8 views

OS Lab

The document outlines various practical exercises related to Linux/UNIX commands, including basic commands like 'cd', 'ls', and 'man', as well as advanced commands and filters such as 'grep', 'sort', and 'find'. It also includes shell scripting exercises for generating marksheets, multiplication tables, and checking for palindromes. Each command and script is accompanied by syntax, examples, and aims for clarity.

Uploaded by

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

OS Lab

The document outlines various practical exercises related to Linux/UNIX commands, including basic commands like 'cd', 'ls', and 'man', as well as advanced commands and filters such as 'grep', 'sort', and 'find'. It also includes shell scripting exercises for generating marksheets, multiplication tables, and checking for palindromes. Each command and script is accompanied by syntax, examples, and aims for clarity.

Uploaded by

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

OS PRACTICALS

Practical 1

1. Aim:- Study of Basic Commands of Linux/UNIX.

1. cd (Change Directory) Command:


● A command-line shell command used to change the current working directory in
various operating system
● move to users home directory from anywhere
● Syntax : cd [-Options] [Directory]

2. ls(change directory) Command:


● ls -a : List all files including hidden file starting with
● ls -r : List in reverse order
● s -l : To show long listing information about the file/director
Example:

3. man Command
● It is the interface used to view the system's reference manuals.
● Syntax : man [command name]
● Example

4. Echo Command:
● Display a line of text/string on standard output or a file.
● Syntax : echo [option] [string]
echo -n Do not output a trailing newline
echo -e Enable interpretation of backslash escape sequences
Example :

5. Cal command:
● Displays a simple, formatted calendar in your terminal.
● Syntax : cal [options] [[[day] month] year]
Example:
6. Date Command
● Print or set the system date and time.
● Syntax : date [OPTION]... [+FORMAT]
date +%a The abbreviated weekday name (e.g., Sun)
date +%A The full weekday name (e.g., Sunday)
date +%b The abbreviated month name (e.g., Jan)
date +%B Locale's full month name (e.g., January)
date +%C The current century; like %Y, except omit last two digits (e.g., 20)
date +%w day of week (0..6); 0 is Sunday 25 date Command Option Use
date +%d Display the day of the month
date +%m Displays the month of year (01 to 12)
date +%y Displays last two digits of the year(00 to 99)
date +%Y Display four-digit year.
date +%T Display the time in 24 hour format as HH:MM:SS
date +%H Display the hour
date +%M Display the minute
date +%S Display the seconds
date +%V ISO week number, with Monday as first day of week (01..53)
date +%P locale's equivalent of either AM or PM
Example:
7. Clear Command
● Clear the terminal screen.
● If you take a detailed look after running the clear command, you'll find that it
doesn't really clear the terminal. The tool just shifts the text upwards, out of the
viewable area.
● Syntax : clear
Example:

8. cat command:
● It is used to create, display and concatenate file contents.
● Syntax : cat [OPTION] [FILE]
Example :
$cat file.txt > newfile.txt
• Read the contents of file.txt and write them to newfile.txt, overwriting anything
newfile.txt previously contained. If newfile.txt does not exist, it will be created.
$cat file.txt >> newfile.txt
• Read the contents of file.txt and append them to the end of newfile.txt. If
newfile.txt does not exist, it will be created
cat file1.txt file2.txt
• It will read the contents of file1.txt and file2.txt and display the result in the
terminal.
cat -b:: Omits line numbers for blank space in the output

cat -E: Displays a $ (dollar sign) at the end of each line

9. pwd:(Print working directory) Command


● It prints the current working directory name with the complete path starting from
root (/).
● Syntax : pwd [-OPTION]
Example :

10. who Command


● It display the users that are currently logged into your Unix computer system.
● Syntax : who [-options] [filename]
who -b: Display the time of the last system boot
who -H: Print a line of column headings
who -q :Displays all login names, and a count of all logged-on users
who -a: Display all details of current logged in user
Example :
11. whoami Command
● This command prints the username associated with the current effective user Id
● Syntax : whoami [-OPTION]
Example :

12. uname (unix name) Command:


● Print information about the current system.
● Syntax : uname [-OPTION]
Example :

13. rmdir Command:


● This command removes empty directories from your filesystem.
● Syntax : rmdir [-OPTION] DIRECTORY
Example :

14. mkdir Command


● This command is used to make Directories.
● Syntax : mkdir [-OPTION] DIRECTORY
Example :
15. cp(copy) Command
● This command is used to copy files and directories.
● Syntax : cp [option] source destination/directory
Example :

16. mv(move) Command


● mv command is used to move files and directories.
● Syntax : mv [-options] source dest
Example :

17. rmdir Command:


18. cut Command
● The cut command extracts a given number of characters or columns from a file.
● Syntax : cut [-options] [file]
cut -d Specifies a delimiter to by used as a field. Default field is TAB and this
option overwrites this default behavior
Example :

19. paste Command


● The paste command displays the corresponding lines of multiple files
side-by-side.
● Syntax : paste [-options] [file]
Example :

20. more Command


Example

21. cmp Command


● cmp command in Linux/UNIX is used to compare the two files byte by byte and
helps you to find out whether the two files are identical or not.
● If a difference is found, it reports the byte and line number where the first
difference is found.
● If no differences are found, by default, cmp returns no output.
● Syntax
: • cmp [OPTION]... FILE1 [FILE2 [SKIP1 [SKIP2]]]
example:

22. comm Command


● Compare two sorted files line by line.
● Syntax : comm [OPTION]... FILE1 FILE2
● Example

23. diff(difference) Command


● This command is used to display the differences in the files by comparing the
files line by line
● diff analyzes two files and prints the lines that are different. Essentially, it outputs
a set of instructions for how to change one file to make it identical to the second
file.
● Syntax : diff [options] File1 File2
Example

24. chmod(change mode) Command


● chmod is used to change the permissions of files or directories.
● Syntax : chmod [reference][operator][mode] file...
Example

25. file Command


● The file command is used to determine a file's type.
● Syntax : file [OPTIONS] file1 file2 …
Example :

26. sleep Command


● The sleep command is used to delay for a specified amount of time.
● Syntax : sleep NUMBER[SUFFIX]...
Example :

27. ps Command
● Reports a snapshot of the status of currently running processes.
● Syntax : ps [option]
Example

28. kill Command


● It is used to terminate processes manually.
● kill command sends a signal to a process which terminates the process.
● If the user doesn’t specify any signal which is to be sent along with kill command
then default TERM signal is sent that terminates the process..
● Syntax : kill [option] PID
Practical 2

Aim:to study Advance commands and filters of Linux/UNIX

1. wc Command
● It s used to find out number of newline count, word count, byte and characters
count in a files specified by the file arguments.
● Syntax : wc [options] filenames

2. ln Command
● ln creates links between files.
● ln creates hard links by default, or symbolic links if the -s (-- symbolic) option is
specified. When creating hard links, each TARGET must exist.
● Syntax : ln [OPTION]… [-T] TARGET LINK_NAME

3. nl command
● nl command numbers the lines in a file.
● Syntax : nl [OPTION]... [FILE]...
Example :

4. head Command
● head makes it easy to output the first part (10 lines by default) of files.
● Syntax : head [OPTION]... [FILE]...
● Example :

5..tail Command
● tail is a command which prints the last few number of lines (10 lines by default) of
a certain file, then terminates.
● Syntax : tail [OPTION]... [FILE]...

6.sort Command
● sort command is used to sort a file, arranging the records in a particular order.
● By default, the sort command sorts file assuming the contents are ASCII. Using
options in sort command, it can also be used to sort numerically.
● Syntax : sort [OPTION]... [FILE]...

7. find Command
● find command searches for files in a directory hierarchy.
● Syntax : find [option] [path...] [expression]

8.uniq Command
● uniq reports or filters out repeated lines in a file.
● It can remove duplicates, show a count of occurrences, show only repeated lines,
ignore certain characters and compare on specific fields.
● Syntax : uniq [OPTION]... [INPUT [OUTPUT]]

9.grep Command
● The grep filter searches a file for a particular pattern of characters, and displays
all lines that contain that pattern.
● The pattern that is searched in the file is referred to as the regular expression
● . • grep stands for globally search for regular expression and print out.
● Syntax : grep [options] pattern [files]
10.pipe (|) Command
● It redirects the command STDOUT or standard output into the given next
command STDIN or standard input.
● In short, the output of each process directly as input to the next one like a
pipeline.
● The symbol '|' denotes a pipe.
● Pipes help you mash-up two or more commands at the same time and run them
consecutively. Syntax : command_1 | command_2 | command_3 | .... |
command_N
Practical-3

Aim:Write a shell script to generate marksheet of a student. Take 3 subjects,


calculate and display total marks, percentage and Class obtained by the student.

Program:
echo "*****************"
echo "Student Marksheet"
echo "*****************"
echo "Enter Operating System Marks:"
read os
echo "Enter C++ Marks:"
read cpp
echo "Enter Java Marks:"
read java
echo "*****************"
total=`expr $os + $cpp + $java`
echo "Total Marks:"$total
percentage=`expr $total / 3`
echo "Percentage:" $percentage %
if [ $percentage -ge 60 ]
then
echo "Class: First Class Distinction"
elif [ $percentage -ge 50 ]
then
echo "Class: First class"
elif [ $percentage -ge 40 ]
then
echo "Class: Second class"
else
echo "Class: Fail"
fi
echo "*****************"

output:
Practical-4

Aim:-Write a shell script to display multiplication table of given numbe


Program:-
echo "Enter Number to Generate Multiplication Table"
read -p "Enter the number : " number
echo "***********************"
i=1
while [ $i -le 10 ]
do
echo " $number * $i =`expr $number \* $i ` "
i=`expr $i + 1`
done
echo "***********************"
Output:

Practical-5

Aim:Write a shell script to find factorial of given number n.


Program:
read -p
"Enter the number : " number
fact=1
while [ $number -gt 1 ]
do
fact=$((fact * number))
number=$((number - 1))
done echo "Result:" $fact
Output
Practical 6:
Aim:Write a shell script which will accept a number b and display first n prime
numbers as output
Program:
read -p "Enter the Number: " n
echo "The prime numbers $n are: "
m=2
while [ $m -le $n ]
do
i=2
flag=0
while [ $i -le `expr $m / 2` ]
do
if [ `expr $m % $i` -eq 0 ]
then
flag=1
break
fi
i=`expr $i + 1`
done
if [ $flag -eq 0 ]
then
echo $m
fi
m=`expr $m + 1`
done
Output:
Practical 7:

Aim:Write a shell script which will generate first n fibonnacci numbers like: 1, 1, 2,
3, 5, 13, …

Program:

read -p "Enter the Number: " number


x=0
y=1
i=2
echo "Fibonacci Series Upto $number Number: "
echo "$x"
echo "$y"
while [ $i -lt $number ]
do
i=`expr $i + 1`
z=`expr $x + $y`
echo "$z"
x=$y
y=$z
done

Output:
Practical 8:

Aim:Write a menu driven shell script which will print the following menu and
execute the given task.

a. Display calendar of current month


b. Display today’s date and time
c. Display usernames those are currently logged in the system
d. Display your name at given x, y position
e. Display your terminal number

Program:
echo "Select Anyone Option"
echo "**************************************************"
echo "1)Display Calener of current month"
echo "2)Display Today's Date and Time"
echo "3)Display Username who are currently logged in"
echo "4)Display your name on given x,y position"
echo "5)Display your terminal Number"
echo "6)Exit"
echo "Enter your choice:"
read ch
case $ch in
1)cal;;
2)date;;
3)who;;
4)row=$(tput lines)
col=$(tput cols)
echo "Terminal Window has Rows=$row Cols=$col"
echo "Enter desired X,Y position"
echo "X position="
read x
echo "Y position="
read y
echo "Enter the name"
read name
tput cup $x $y
echo "$name";;
5)tty;;
6)echo "Exit";;
*)echo "Enter valid choice";;
esac
Output:
Practical 9:

Aim:Write a shell script to read n numbers as command arguments and sort them
in descending order.

Program:

read -p "Enter The Number: " n


for((i=0; i<$n; i++))
do
read -p "Enter value of arr[$i]: " arr[$i]
done
#sorting code
for((i=0; i<$n; i++))
do
for((j=0; j<n-i-1; j++))
do
if [ ${arr[j]} -lt ${arr[$((j+1))]} ]
then
#swapping
temp=${arr[j]}
arr[$j]=${arr[$((j+1))]}
arr[$((j+1))]=$temp
fi
done
echo "Numbers in Descending order: " ${arr[*]}

Output:
Practical 10:

Aim:Write a shell script to display all executable files, directories and zero sized
files from current directory.

Program:
echo Executable files
files=$(find lab_solutions -executable -type f)
echo $files
echo
echo List of Directories
dir=$(ls -d )
echo $dir
echo
echo List of zero sized files
zero=$(find -size 0)
echo $zero

Output:
Practical 11:

Aim:Write a shell script to check entered string is palindrome or not.

Program:

echo "Input your string without space"


read vstr
for i in $(seq 0 ${#vstr})
do
rvstr=${vstr:$i:1}${rvstr}
done
echo "Input string was :" $vstr
echo "After Reversng String Is :" $rvstr
if [ "$vstr" = "$rvstr" ]
then
echo "String Is Palindrome."
Else
echo "String Is Not Plaindrome."
if
done

Output:
Practical 12:
Aim:Shell programming using filters (including grep, egrep, fgrep)
Program:
echo "Main Menu"
echo "Press 1 for using Grep Command"
echo "Press 2 for using Egrep Command"
echo "Press 3 for using Fgrep Command"
read a
case $a in
1) echo "For single pattern search, Enter Pattern below :"
read b
grep "$b" City_names.txt
;;
2) echo "For double Pattern search, Enter b,c pattern :"
read b
read c
egrep "$b" City_names.txt
grep -E "$c" City_names.txt
;;
3) echo "For Pattern From a File, Enter Pattern :"
read b
grep -F "$b" City_names.txt
;;
esac
Output:
Practical 14:

Aim: Write a shell script to validate the entered date. (eg. Date format is :
dd-mm-yyyy)

Program:
echo "Enter Valid Date"
read date
echo "You have entered $date"
date -d $date
if [ $? -eq 0 ]
then
echo "Enter Date is Valid"
else
echo "Enter Date is Invalid"
if

Output:

Practical 15:

Aim:Write an awk program using function, which convert each word in a given
text into capital

Program:
echo "Enter the String"
a=$(awk 'BEGIN{
getline str;
print toupper(str);
}')
echo $a

Output:
Practical 16:

Aim:Write a program for process creation using C. (Use of gcc compiler

Program:
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>

int main() {
pid_t pid, mypid, myppid;
pid = getpid();
printf("Before fork: Process id is %d\n", pid);
pid = fork();

if (pid < 0) {
perror("fork() failure\n");
return 1;
}
// Child process
if (pid == 0) {
printf("This is child process\n");
mypid = getpid();
myppid = getppid();
printf("Process id is %d and PPID is %d\n", mypid, myppid);
} else { // Parent process
sleep(2);
printf("This is parent process\n");
mypid = getpid();
myppid = getppid();
printf("Process id is %d and PPID is %d\n", mypid, myppid);
printf("Newly created process id or child pid is %d\n", pid);
}
return 0;
}
Output:

You might also like