lOMoARcPSD|51657930
Sanghvi Institute of
Management & Science
Session 2024 – 25
B.Tech
Computer Science & Engineering
Subject – Lab ( Linux )
Semester – 5th Sem
Submitted by – Dev Patil
Roll No. – 0837CS21100
lOMoARcPSD|51657930
LIST OF EXPERIEMENTS
S.No. List of Experiments
1. To Study basic & User status Unix/Linux Commands.
2. Study & use of commands for performing arithmetic operations with Unix/Linux.
Create a file and display how many lines, words and characters are present in that 3.
file using shell commands.
Give two files each of which contains names. use shell command to display only 4.
those names that are found on both the files.
5. Implement shell command to find out the inode number of any desired file.
6. Study & use of the Command for changing file permissions.
Write a pipeline of commands, which displays on the monitor as well as saves the 7.
information about the number of users using the system at same time.
8. Execute shell commands through vi editor.
9. Installation, Configuration & Customizations of Unix/Linux.
Write a shell script that accepts any number of arguments and prints them in the 10.
reverse order.
Write a shell script to find the smallest of three numbers that are read from the 11.
keyboard.
Experiment -1
Aim: To Study basic & User status Unix/Linux Commands
To run basic commands we need open a terminal . you can open it by pressing CTRL + ALT + T or by
searching it from start menu or if you want to open from desktop then you can right click then click on
"open in terminal ".
ls : If you want to see the list of files on your UNIX or Linux system, use the 'ls' command. It shows
the files /directories in your current directory. The ls command will show you the list of files in
your current directory.
$ ls
1. Directories are denoted in blue color.
2. Files are denoted in white.
3. You will find similar color schemes in different flavors of Linux.
lOMoARcPSD|51657930
pwd : When you first open the terminal, you are in the home directory of your user. To know which directory
you are in, you can use the “pwd” command. It gives us the absolute path, which means the path that starts
from the root. The root is the base of the Linux file system. It is denoted by a forward slash( / ). The user
directory is usually something like "/home/username".
$ pwd
cd : Use the "cd" command to change directory. For example, if you are in the home folder, and you want to
go to the downloads folder, then you can type in “cd Downloads”.
syntax : cd /Directory_path
Example :
$cd Desktop
mkdir & rmdir : Use the mkdir command when you need to create a folder or a directory.Use rmdir to delete
a directory. But rmdir can only be used to delete an empty directory.
mkdir name_of_directory rmdir
name_directory_you_wnttodelete
Example :
$ mkdir abc $ rmdir abc
rm : Use the rm command to delete files and directories.
rm file_or_directory_name
After deleting the file
rm -r : Use "rm -r" to delete just the directory. It deletes both the folder and the files it contains when using
only the rm command.
Example : Before deleting
After deleting directory .touch : The touch command is used to create a file. It can be anything, from an empty
txt file to an empty zip file.
Syntax : touch filename
After applying command cat : It is generally used to concatenate the files. It gives the output on the
standard output.
syntax : cat filename1 filename2
more :It is a filter for paging through text one screenful at a time.
Syntax : more filename $more nams.txt
file : file command is used to determine the type of a file. .file type may be of humanreadable(e.g. ‘ASCII
text’) or MIME type(e.g. ‘text/plain; charset=us-ascii’). This command tests each argument in an attempt to
categorize it.
It has three sets of tests as follows:
lOMoARcPSD|51657930
1. filesystem test: This test is based on the result which returns from a stat system call. The program
verifies that if the file is empty, or if it’s some sort of special file. This test causes the file type to be
printed.
2. magic test: These tests are used to check for files with data in particular fixed formats.
3. language test: This test search for particular strings which can appear anywhere in the first few blocks
of a file.
//Command displays the file type
file nams.txt
//This is used to display just file type in brief mode
file -b nams.txt
//This is used to display all files filetypes in particular directory.
file ab/*
//To display the file type of files in specific range
file [a-z]*
//Cause a checking printout of the parsed form of the magic file. This option is usually used in conjunction
with the -m flag to debug a new magic file before installing it.
file -c
man : man command in Linux is used to display the user manual of any command that we can run on the
terminal. It provides a detailed view of the command which includes NAME, SYNOPSIS, DESCRIPTION,
OPTIONS, EXIT STATUS, RETURN VALUES, ERRORS, FILES, VERSIONS, EXAMPLES, AUTHORS .
lOMoARcPSD|51657930
Syntax : man [OPTION]... [COMMAND NAME]
who : who command is used to find out Time of last system boot , Current run level of the system , List of
logged in users and more.
lOMoARcPSD|51657930
Description Example
The who command displays the following information for each user
$who
currently logged in to the system if no option is provided
To display host name and user associated with standard input such as
$who -m -H
keyboard
To show all active processes which are spawned by INIT process $who -p -H
To show status of the users message as +, – or ? $who -T -H
To show list of users logged in to system $who -u
To show time of the system when it booted last time $who -b -H
To show details of all dead processes $who -d -H
To show system login process details $who -l -H
To count number of users logged on to system $who -q -H
To display current run level of the system $who -r
To display all details of current logged in user $who -a
To display system’s username $whoami
To display list of users and their activities $w
Description Example
To display user identification information $ id
cat : It is generally used to concatenate the files. It gives the output on the standard output.
lOMoARcPSD|51657930
syntax : cat filename1 filename2
cd : Use the "cd" command to change directory. For example, if you are in the home folder, and you want to
go to the downloads folder, then you can type in “cd Downloads”.
syntax : cd /Directory_path
Example :
$cd Desktop
ls : If you want to see the list of files on your UNIX or Linux system, use the 'ls' command. It shows the files
/directories in your current directory. The ls command will show you the list of files in your current directory.
$ ls
1. Directories are denoted in blue color.
2. Files are denoted in white.
3. You will find similar color schemes in different flavors of Linux.
pwd : When you first open the terminal, you are in the home directory of your user. To know which directory
you are in, you can use the “pwd” command. It gives us the absolute path, which means the path that starts
from the root. The root is the base of the Linux file system. It is denoted by a forward slash( / ).
$ pwd
mkdir & rmdir : Use the mkdir command when you need to create a folder or a directory.Use rmdir to delete
a directory. But rmdir can only be used to delete an empty directory.
mkdir name_of_directory rmdir
name_directory_you_wnttodelete Example :
$ mkdir abc $ rmdir abc
rm : Use the rm command to delete files and directories.
rm file_or_directory_name
cp : cp stands for copy. This command is used to copy files or group of files or directory. It creates an exact
image of a file on a disk with different file name. cp command require at least two filenames in its arguments.
lOMoARcPSD|51657930
Syntax : cp Src_file Dest_file
ps : The ps command reports information on current running processes, outputting to standard output. It is
frequently used to find process identifier numbers. It supports searching for processes by user, group, process
id or executable name.
Syntax : $ ps
mv : mv stands for move. mv is used to move one or more files or directories from one place to another in file
system like UNIX. It has two distinct functions:
(i) It rename a file or folder.
(ii) It moves group of files to different directory.
Syntax: mv [Option] source destination
echo : echo command in linux is used to display line of text/string that are passed as an argument . This is a
built in command that is mostly used in shell scripts and batch files to output status text to the screen or a
file.
Syntax : echo "enter text"
more : more command is used to view the text files in the command prompt, displaying one screen at a time
in case the file is large (For example log files). The more command also allows the user do scroll up and
down through the page.
-d : Use this command in order to help the user to navigate. It displays “[Press space to continue, ‘q’ to quit.]”
and displays “[Press ‘h’ for instructions.]” when wrong key is pressed.
syntax : more -d sample.txt
Experiment -2
Aim: Study & use of commands for performing arithmetic operations with Unix/Linux
Arithmetic Operators
lOMoARcPSD|51657930
Program
lOMoARcPSD|51657930
echo "enter a"
read a
echo "enter b"
read b
val=$(( a + b))
echo "a + b : $val"
val=$((a - b))
echo "a - b : $val"
val=$((a * b))
echo "a * b : $val"
val=$((b / a))
echo "b / a : $val"
val=$((b % a))
echo "b % a : $val"
if [ $a == $b ]
then
echo "a is equal to b"
fi
Experiment -3
AIM: Create a file called wlcc.txt with some lines and display how many lines, words and characters
are present in that file.
In Unix, to get the line, word, or character count of a document, use the " wc "command.
syntax : wc filename
For each file, wc will output three numbers. The first is the line count, the second is the word count, and the
third is the character count.
wc leap.sh
lOMoARcPSD|51657930
Experiment -4
AIM: Use shell commands to display only those names that are found on both the files
Comm : Compare sorted files FILE1 and FILE2 line by line , With no options, produce three-column output.
• Column one contains lines unique to FILE1
• column two contains lines unique to FILE2, Column three contains lines common to both files.
• -1 : suppress column 1 (lines unique to FILE1)
• -2 : suppress column 2 (lines unique to FILE2)
• -3 : suppress column 3 (lines that appear in both files)
• --check-order : check that the input is correctly sorted, even if all input lines are pairable
• --nocheck-order : do not check that the input is correctly sorted
• --output-delimiter=STR : separate columns with STR
Syntax : comm [OPTION]... FILE1 FILE2
Examples
• comm -12 file1 file2 Print only lines present in both file1 and file2.
• comm -3 file1 file2 Print lines in file1 not in file2, and vice versa.
comm -12 --nocheck-order one.txt two.txt
comm -1 --nocheck-order file2.txt file1.txt
comm -2 --nocheck-order file2.txt file1.txt
comm -3 --nocheck-order file2.txt file1.txt
comm --nocheck-order file2.txt file1.txt
Experiment -5
AIM: Create a program to find out the inode number of any desired file.
Inode number : Inode number also called index number .An Inode number points to an Inode. An Inode is a
data structure that stores the following information about a file :
• Size of file
• Device ID
• User ID of the file
• Group ID of the file
• The file mode information and access privileges for owner, group and others
• File protection flags
• The timestamps for file creation, modification etc
• link counter to determine the number of hard links
• Pointers to the blocks storing file’s contents
Check Inodes on Filesystem
lOMoARcPSD|51657930
You can find a total number of inodes on disk by using ‘-i‘ option with
df command
Find Inode number of File: To check inode number of file use following command. The first field in output is
an inode number of the file.
ls -il filename
Inode Changes with Copy, Move and Delete :
Copy file: cp allocates a free inode number and placing a new entry in inode table.
cp v.txt v_.txt
Move or Rename a file: if destination is same filesystem as the source, Has no impact on inode number, it
only changes the time stamps in inode table.
mv filename path_where_you_want_to_copy
mv v.txt /home/cloudera
Delete a file: Deleting a file in Linux decrements the link count and freeing the inode number to be reused.
Commands to access Inode numbers
1) Ls -i Command :the flag -i is used to print the Inode number for eachfile.
2) Df -i Command: df -i command displays the inode information of the filesystem.
3) Stat Command: Stat command is used to display file statistics that alsodisplays inode number of a file.
Output :
ls -i df -i stat
a.txt
lOMoARcPSD|51657930
Experiment -6
AIM: Study & use of the Command for changing file permissions.
Ownership of Linux files
• User : A user is the owner of the file. By default, the person who created a file becomes its owner.
Hence, a user is also sometimes called an owner.
• Group : A user- group can contain multiple users. All users belonging to a group will have the same
access permissions to the file. Suppose you have a project where a number of people require access
to a file. Instead of manually assigning permissions to each user, you could add all users to a group,
and assign group permission to file such that only this group members and no one else can read or
modify the files.
• Other : Any other user who has access to a file. This person has neither created the file, nor he
belongs to a usergroup who could own the file. Practically, it means everybody else. Hence, when
you set the permission for others, it is also referred as set permissions for the world.
Permissions
• Read: This permission give you the authority to open and read a file. Read permission on a
directory gives you the ability to lists its content.
• Write: The write permission gives you the authority to modify the contents of a file. The write
permission on a directory gives you the authority to add, remove and rename files stored in the
directory. Consider a scenario where you have to write permission on file but do not have write
permission on the directory where the file is stored. You will be able to modify the file contents.
But you will not be able to rename, move or remove the file from the directory.
• Execute: In Windows, an executable program usually has an extension ".exe" and which you can
easily run. In Unix/Linux, you cannot run a program unless the execute permission is set. If the
execute permission is not set, you might still be able to see/modify the program code(provided read
& write permissions are set), but not run it.
Understanding the security permissions
First, you must think of those nine characters as three sets of three characters (see the box at the bottom). Each
of the three “rwx” characters refers to a different operation you can perform on the file.
--- --- ---
rwx rwx rwx
user group other
The ‘r’ means you can “read” the file’s contents.
The ‘w’ means you can “write”, or modify, the file’s contents.
The ‘x’ means you can “execute” the file. This permission is given only if the file is a program.
If any of the “rwx” characters is replaced by a ‘-‘, then that permission has been revoked.
Symbolic Mode
In the Absolute mode, you change permissions for all 3 owners. In the symbolic mode, you can modify
permissions of a specific owner. It makes use of mathematical symbols to modify the file permissions.
lOMoARcPSD|51657930
Operator Description
+ Adds a permission to a file or directory
- Removes the permission
= Sets the permission and overrides the permissions set earlier.
The various owners are represented as -
User Denotations
u user/owner
g group
o other
a all
As we know that " ls " is used to list information about files and directories within the file system and " -l " (
lowercase L) option causes ls to print files in a long listing format. it shows the following :-
• The file type
• The file permissions
• Number of hard links to the file
• File owner
• File group
• File size
• Date and Time
• File name
Example Changing file permissions
We can use the 'chmod' command which stands for 'change mode'. Using the command, we can set
permissions (read, write, execute) on a file/directory for the owner, group and the world.
Syntax : chmod permissions filename
Current file permission
• 7 = read + write + execute (rwx)
• 4 = read (r--)
• 3 = execute + write (-wx)
'743' absolute code says the following:
lOMoARcPSD|51657930
• Owner can read, write and execute
• Usergroup can read only
• World can execute and write
Experiment -7
Aim: Write a pipeline of commands, which displays on the monitor as well as saves the information
about the number of users using the system at present on a file called usere.ux.
w command
Shows information about the users currently on the machine, and their processes.w command in Linux is used
to show who is logged on and what they are doing. This command shows the information about the users
currently on the machine and their processes. The header shows, in this order, the current time, how long the
system has been running, how many users are currently logged on, and the system load averages for the past
1, 5, and 15 minutes.
Syntax:w [options] user [...]
lOMoARcPSD|51657930
who command
who command is used to find out the following information :
1. Time of last system boot
2. Current run level of the system
3. List of logged in users and more.
The who command is used to get information about currently logged in user on to system.
Syntax : who [options] [filename]
lOMoARcPSD|51657930
lOMoARcPSD|51657930
Experiment -8
Aim: Execute shell commands through vi editor.
The vi editor is also known as visual editor. It is user-friendly and works same on different platforms. The vi
editor tool is an interactive tool as it displays changes made in the file on the screen while you edit the file.In
vi editor you can insert, edit or remove a word as cursor moves throughout the file.
Note : The vi editor is case-sensitive
The vi editor has two modes:
• Command Mode: In command mode, actions are taken on the file. The vi editor starts in command
mode. Here, the typed words will act as commands in vi editor. To pass a command, you need to
be in command mode.
• Insert Mode: In insert mode, entered text will be inserted into the file. The Esc key will take you
to the command mode from insert mode.
By default, the vi editor starts in command mode. To enter text, you have to be in insert mode, just type 'i' and
you'll be in insert mode.Now you can type anything.To exit from insert mode press Esc key, you'll be directed
to command mode.
Syntax : vi filename
In the terminal when you'll type vi command with a file name, the content of the file will be displayed. If there
is no such file, then a new file will be created .
we are creating a new file f.txt as this file doesn't exist and have entered the full path for the directory
Documents.
vi /home/cloudera/Documents/f.txt
Command mode : By default vi editor is in command mode .
Insert mode : To be in insert mode we need to press " i "
To save and quit
You can save and quit vi editor from command mode. Before writing save or quit command you have to press
colon (:). Colon allows you to give instructions to vi.
lOMoARcPSD|51657930
Commands Action
:wq Save and quit
:w Save
:q Quit
:w fname Save as fname
ZZ Save and quit
:q! Quit discarding changes made
:w! Save (and write to non-writable file)
press escape (esc ) and then :wq will save and quit the vi editor. When you'll type it in command mode, it will
automatically come at bottom left corner. If you want to quit without saving the file, use :q . file is modified
and hence on typing :q file can be saved with the command .
" !q." It discards the changes made in the file and save it.
Example : let create a program file named as "abc.sh " . then write a program in it and then save it by pressing
"esc : wq! " . then run the program by passing " sh abc.sh " command to terminal .
Experiment -9
Aim: Installation, Configuration & Customizations of Unix/Linux.
Installing Linux using Virtual Machine
This is a popular method to install a Linux operating system. The virtual installation offers you the freedom
of running Linux on an existing OS already installed on your computer. This means if you have Windows
running, then you can just run Linux with a click of a button.Virtual machine software like Oracle VM can
install Ubuntu in easy steps. Let us look at them.
Steps to follow
Step 1 : To download virtual box VMWARE and then click on window host....
Installing Linux using Virtual Machine
This is a popular method to install a Linux operating system. The virtual installation offers you the freedom
of running Linux on an existing OS already installed on your computer. This means if you have Windows
running, then you can just run Linux with a click of a button.Virtual machine software like Oracle VM can
install Ubuntu in easy steps. Let us look at them.
Steps to follow
Step 1 : To download virtual box Click Here... and then click on window host....
lOMoARcPSD|51657930
Step 2: Click On next
Step 3 : Select you're the directory to install VirtualBox and click on next
Step 4 : Select Desktop icon and click on next, now click on yes Step 5 : Click On
install.
Step 6 : Now installation of the virtual box will start. Once complete, click on Finish Button to start Virtual
Box
The virtual box dashboard looks like this-
Step 2: Click On next
Step 3 : Select you're the directory to install VirtualBox and click on next
Step 4 : Select Desktop icon and click on next, now click on yes
Step 5 : Click On install.
Step 6 : Now installation of the virtual box will start. Once complete, click on Finish Button to start Virtual
Box
Experiment -10
Aim: Write a shell script that accepts any number of arguments and prints them in the reverse order
• $# Stores the number of command-line arguments that were passed to the shell program.
• $* Stores all the arguments that were entered on the #command line ($1 $2 ...).
• $@ Stores all the arguments that were entered on the #command line, individually quoted ("$1""$2"
...)
Program 1
echo "input string is :$*"
for (( i=$#;i>0;i-- ));do
echo "${!i}"
done
Program 2
echo "input string is :$*"
for x in "$@"
do
y=$x" "$y
done
echo "reversed string is: $y"
lOMoARcPSD|51657930
Experiment -11
Aim: Write a shell script to find the smallest of three numbers that are read from the keyboard
Program 1
echo "enter a: "
read a
echo "enter b : "
read b
echo "enter c : "
read c
s=$a
if [ $b -lt $s ]
then
s=$b
fi
if [ $c -lt $s ]
then
s=$c
fi
echo Smallest of $a $b $c is $s
Program 2
echo "enter a: "
read a
echo "enter b: "
read b
echo "enter c: "
read c
if [ $a -le $b -a $a -le $c ]
then
echo "a is Smallest"
elif [ $b -le $c -a $b -le $a ]
then
echo "b is Smallest"
else
echo "c is Smallest"
fi