Linux Exp
Linux Exp
Aim: Create a program to find out the inode number of any desired file.
Theory & Command Codes:
1. INODE
Every file in a Linux system has unique ID known as inode number. These unique IDs allow Linux
system to easily manage files in its filesystem database. In Linux, whenever a new file is created, it is
given a file name and an inode number. This number works as the unique identifier for that file. As a
user, we will use the file name to access the file but Linux OS will first map that filename with an
Inode number in a database to access the fil.
An inode is a data structure that points refer to the individual blocks, making the file. All of the
administrative data required to read a file is included in the inode. The metadata for each file is kept in
inodes in a table structure and this data includes:
File types ( executable, block special etc )
Permissions ( read, write etc )
UID ( Owner )
GID ( Group )
FileSize
Time stamps including last access, last modification and last inode number change.
File deletion time
Number of links ( soft/hard )
Location of ile on harddisk.
Some other metadata about file.
2. Commands:
Using ls command: The -i option with ls commands displays inode number.
Example: $ ls -li
Using stat command: stat command is used to view inode number of single file. It
basically provides lot of details about given file/directory, along with its inode number.
Example:$ stat ./html
OUTPUT
(NOTE: Output must draw on plane pages.)
Experiment No -8
Aim: Study & use of the Command for changing file permissions.
Theory & Command Codes:
1. FILE PERMISSIONS
File permissions are core to the security model used by Linux systems. They determine who can access
files and directories on a system and how. For effective security, Linux divides authorization into 2
levels.
1. Ownership
2. Permission
1.1 Linux File Ownership:
Every file and directory in Linux system is assigned 3 types of owner. They are:
1. 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.
2. Group: A user- group can contain multiple users. All users belonging to a group will have the
same Linux group permissions access to the file.
3. Other: Any other user who has access to a file. This person has neither created the file, nor he
belongs to a user group who could own the file.
1.1 Linux File Permissions:
Every file and directory in /Linux system has following 3 permissions defined for all the 3 owners.
They are:
1. 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.
2. 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.
3. Execute: In Linux, we cannot run a program unless the execute permission is set.
2. COMMANDS
ls – l
r = read permission
w = write permission
x = execute permission
– = no permission
CHMOD Command
chmod’ command which stands for ‘change mode’. Using the command, we can set permissions (read,
write, execute) on a file/directory. There are 2 ways to use the command –
Absolute mode
Symbolic mode
g group
o other
a all
EXAMPLES:
1. $ chmod 755 public_dir
2. $ chmod 700 my_script
OUTPUT
(NOTE: Output must draw on plane pages.)
$ ls -ld public_dir
drwxr--r-- 1 ignatz staff 6023 Nov 2 12:06 public_dir
$ chmod 755 public_dir
$ ls -ld public_dir
drwxr-xr-x 1 ignatz staff 6023 Aug 5 12:06 public_dir
$ ls -l my_script
-rw------- 1 ignatz staff 6023 Nov 2 12:06 my_script
$ chmod 700 my_script
$ ls -l my_script
-rwx------ 1 ignatz staff 6023 Aug 5 12:06 my_script
Experiment No -9
Aim: Write a shell script that accepts any number of arguments and prints them in
the reverse order.
Theory & Command Codes:
1. SHELL SCRIPTING
Shell Scripting is a program to write a series of commands for the shell to execute. It can combine
lengthy and repetitive sequences of commands into a single and simple script that can be stored and
executed anytime which, reduces programming efforts. Shell Scripts are written using text editors.
Each shell script is saved with `.sh` file extension e.g., myscript.sh.
A shell script comprises the following elements –
Shell Keywords – if, else, break etc.
Shell commands – cd, ls, echo, pwd, touch etc.
Functions
Control flow – if..then..else, case and shell loops etc.
OUTPUT
(NOTE: Output must draw on plane pages.)
Output 1
$ ./print-arguments-in-reverse.sh
no arguments given
Output 2
$ ./print-arguments-in-reverse.sh Indians are great
Total number of arguments: 3
The arguments are: Indians are great
The arguments in reverse order:
great are Indians
Experiment No -10
Aim: Write a shell script to find the smallest of three numbers that are read from
the keyboard.
Theory & Command Codes:
2. Shell Script
.#!/bin/bash
echo "Enter 3 numbers"
read a
read b
read c
if [ $a -eq $b -a $b -eq $c ]
then
echo "All numbers are equal"
exit
fi
if [ $a -lt $b ]
then
s1=$a
s2=$b
else
s1=$b
s2=$a
fi
if [ $s1 -gt $c ]
then
s2=$s1
s1=$c
fi
echo "smallest number is $s1
OUTPUT
(NOTE: Output must draw on plane pages.)
Output 1
$ ./reverse.sh
Enter 3 numbers
589
smallest number is 5
Output 2
$ ./reverse.sh
Enter 3 numbers
888
All numbers are equal