Sunteți pe pagina 1din 13

RAGHU ENGINEERING

COLLEGE
Permanently Afliated to JNTUK, Approved by AICTE
Accredited by NBA
Ranked AAA by careers 360
Ranked A Grade by AP State Knowledge Mission
Ranked 63rd among top 100 private engineering colleges
in
India by higher education review magazine.
www.raghuenggcollege.com

DEPARTMENT OF COMPUTER SCIENCE &


ENGINEERING
III B.Tech - I Semester(R16)
UNIX PROGRAMMING
UNIT – II

Raghu Engineering College Department of CSE Unit-II Unix Programming


Page 1
UNIT II Syllabus III-B.Tech.,I-Sem.,
(R16)

Objectives: Facility with UNIX command syntax and


semantics.
UNIT-II
The File system –The Basics of Files-What’s in a File-
Directories and File Names-Permissions-I Nodes-The
Directory Hierarchy, File Attributes and Permissions-The
File Command knowing the File Type-The Chmod
Command Changing File Permissions-The Chown
Command Changing the Owner of a File-The Chgrp
Command Changing the Group of a File

Day #4
1. What is fle? Explain types of fles in Unix.
Ans)

File:
File is a logical name for physical storage device. File is
an individual storage clause to store information.

In Unix, there are three basic types of fles –


i) Ordinary Files − An ordinary fle is a fle on the
system that contains data, text, or program
instructions. In this tutorial, you look at working with
ordinary fles.

ii) Directories − Directories store both special and


ordinary fles. For users familiar with Windows or Mac
OS, Unix directories are equivalent to folders.

iii) Special Files − Some special fles provide access to


hardware such as hard drives, CD-ROM drives,
modems, and Ethernet adapters. Other special fles are

Raghu Engineering College Department of CSE Unit-II Unix Programming


Page 2
similar to aliases or shortcuts and enable you to access
a single fle using diferent names.

Here is the information about all the listed columns for


fle/directory –
i) First Column: Represents the fle type and the
permission given on the fle. Below is the description of
all type of fles.

ii) Second Column: Represents the number of memory


blocks taken by the fle or directory.

iii) Third Column: Represents the owner of the fle. This


is the Unix user who created this fle.

iv) Fourth Column: Represents the group of the owner.


Every Unix user will have an associated group.

v) Fifth Column: Represents the fle size in bytes.

vi) Sixth Column: Represents the date and the time


when this fle was created or modifed for the last time.

vii) Seventh Column: Represents the fle or the


directory name.

2. Explain fle permission attributes in Unix.


Ans)
File ownership is an important component of Unix that
provides a secure method for storing fles. Every fle in
Unix has the following attributes –

i) Owner permissions − The owner's permissions


determine what actions the owner of the fle can
perform on the fle.

Raghu Engineering College Department of CSE Unit-II Unix Programming


Page 3
ii) Group permissions − The group's permissions
determine what actions a user, who is a member of the
group that a fle belongs to, can perform on the fle.

iii) Other (world) permissions − The permissions for


others indicate what action all other users can perform
on the fle.
The permissions are broken into groups of threes, and
each position in the group denotes a specifc
permission, in this order: read (r), write (w), execute (x)

a) The frst three characters (2-4) represent the
permissions for the fle's owner. For example, -rwxr-xr--
represents that the owner has read (r), write (w) and
execute (x) permission.

b) The second group of three characters (5-7) consists


of the permissions for the group to which the fle
belongs. For example, -rwxr-xr-- represents that the
group has read (r) and execute (x) permission, but no
write permission.

c) The last group of three characters (8-10) represents


the permissions for everyone else. For example, -rwxr-
xr-- represents that there is read (r) only permission.

Command for changing permissions :

syntax: chmod <options/operator> <flename>

+ Adds the designated permission(s) to a fle or


directory.

- Removes the designated permission(s) from a fle or


directory.

= Sets the designated permission(s).

Raghu Engineering College Department of CSE Unit-II Unix Programming


Page 4
3. Explain ‘chmod’ command with examples.
Ans)
Command for changing permissions :

syntax: chmod <options/operator> <flename>

+ Adds the designated permission(s) to a fle or


directory.

- Removes the designated permission(s) from a fle or


directory.

= Sets the designated permission(s).

Example(s):
$ls -l testfle
-rwxrwxr-- 1 amrood users 1024 Nov 2 00:10 testfle

$chmod o+wx testfle


$ls -l testfle
-rwxrwxrwx 1 amrood users 1024 Nov 2 00:10 testfle
$chmod o+wx testfle
$ls -l testfle
-rwxrwxrwx 1 amrood users 1024 Nov 2 00:10 testfle
$chmod u-x testfle
$ls -l testfle
-rw-rwxrwx 1 amrood users 1024 Nov 2 00:10 testfle

$chmod g=rx testfle


$ls -l testfle
-rw-r-xrwx 1 amrood users 1024 Nov 2 00:10 testfle

Raghu Engineering College Department of CSE Unit-II Unix Programming


Page 5
The second way to modify permissions with the chmod
command is to use a number to specify each set of
permissions for the fle.
Each permission is assigned a value, as the following
table shows, and the total of each set of permissions
provides a number for that set.
Octal Permission Representation
0 No permission ---
1 Execute permission --x
2 Write permission -w-
3 Execute and write permission: 1 (execute) + 2
(write) = 3 -wx
4 Read permission r--
5 Read and execute permission: 4 (read) + 1
(execute) = 5 r-x
6 Read and write permission:
4 (read) + 2 (write) = 6 rw-
7 All permissions:
4 (read) + 2 (write) + 1 (execute) = 7 rwx
Example(s):
$ chmod 755 testfle
$ls -l testfle
-rwxr-xr-x 1 amrood users 1024 Nov 2 00:10 testfle
$chmod 743 testfle
$ls -l testfle
-rwxr---wx 1 amrood users 1024 Nov 2 00:10 testfle
$chmod 043 testfle
$ls -l testfle
----r---wx 1 amrood users 1024 Nov 2 00:10 testfle

Raghu Engineering College Department of CSE Unit-II Unix Programming


Page 6
4. What is iNode? Explain iNode fle structure.
Ans)
Internal representation of a fle is called an "inode"
(contraction of term - index node) which contains all
the required information and description of the fle data
and its layout on disk. This article deals in detail with
the information stored in inode and the way it is
represented in the kernel.
Inodes resides on the disk and the kernel reads them
into an into memory which we can call as in-core
inodes. Disk inodes contains the following information:
- File access permissions and time (last access /
modifed etc.)
- File ownership information.
- Type of the fle (regular / directory / block special /
pipe)
- Number of links to the fle
- File size and organization on disk (the fle data may
spread across several diferent and far-spaced disk
location)

The In-core copy of inodes contains all of the above


information, but it also contains the following additional
information:
- Status (locked / process is waiting for it to become
unlocked / in-core copy has been modifed and thus
difers from the copy on the disk / mounted)
- Logical Device number of the fle system

Raghu Engineering College Department of CSE Unit-II Unix Programming


Page 7
- Inode Number. Since inodes are stored in a sequential
manner on the disk, the kernel uses an identifer of that
array to refer to its in-core copy.
- Pointer to other in-core inodes. Kernel maintains a
hash queue of inodes according to the logical device
number and the inode numbers. Kernel also maintains
a list of free inodes.
- Reference count which indicates the number of
instances of the fle that are currently active.

5. Explain directory hierarchical structure.


Ans)

Unix uses a hierarchical fle system structure, much like


an upside-down tree, with root (/) at the base of the fle
system and all other directories spreading from there.

A Unix flesystem is a collection of fles and directories


that has the following properties −

Raghu Engineering College Department of CSE Unit-II Unix Programming


Page 8
• It has a root directory (/) that contains other fles
and directories.
• Each fle or directory is uniquely identifed by its
name, the directory in which it resides, and a
unique identifer, typically called an inode.
• By convention, the root directory has an inode
number of 2 and the lost+found directory has an
inode number of 3. Inode numbers 0 and 1 are not
used. File inode numbers can be seen by specifying
the -i option to ls command.
• It is self-contained. There are no dependencies
between one flesystem and another.
The directories have specifc purposes and generally
hold the same types of information for easily locating
fles. Following are the directories that exist on the
major versions of Unix −

Raghu Engineering College Department of CSE Unit-II Unix Programming


Page 9
6. Explain fle command operators helps in
conditional expressions.
Ans)

File test operators: General syntax <operator> fle.


-b fle: Returns true when fle is a block fle
-c fle: Returns true when fle is character special
fle
-d fle: Returns true when fle is a directory
-f fle: Returns true when fle is ordinary type and
opposed to a directory or
special fle
-g fle: Checks the fle has set group ID
-k fle: Checks if the fle has sticky bit
-p fle: Checks if the fle is a named pipe
-t fle: Checks if the fle descriptor is open and
associated with a terminal
-u fle: Checks if the fle has its Set User ID
-r fle: Checks if the fle is readable
-w fle: Checks if the fle is writable
-x fle: Checks if the fle is executable
-s fle: Checks if the fle size is greater than 0
-e fle: Checks if the fle/directory name is existed

7. How to change ownership and group of fles.


Ans)
While creating an account on Unix, it assigns a owner
ID and a group ID to each user.

All the permissions mentioned above are also assigned


based on the Owner and the Groups.

Two commands are available to change the owner and


the group of fles −

Raghu Engineering College Department of CSE Unit-II Unix Programming


Page 10
chown − The chown command stands for "change
owner" and is used to change the owner of a fle.

chgrp − The chgrp command stands for "change group"


and is used to change the group of a fle.

Example(s) below:

The chown command changes the ownership of a fle.


The basic syntax is as follows −

$ chown user flelist

The value of the user can be either the name of a user


on the system or the user id (uid) of a user on the
system.

The following example will help you understand the


concept −

$ chown amrood testfle


$

Changes the owner of the given fle to the user


amrood.

The chgrp command changes the group ownership of a


fle. The basic syntax is as follows:

$ chgrp group flelist

The value of group can be the name of a group on the


system or the group ID (GID) of a group on the system.

Following example helps you understand the concept:

$ chgrp special testfle


$

Raghu Engineering College Department of CSE Unit-II Unix Programming


Page 11
Changes the group of the given fle to special group.

Classwork:
CW-2) You are using a program that continuously
reads from standard input. What key sequence would
you use to signal to that program that there is no more
input?

CW-3) Write a shell script domain2ip that takes a list of


domain names as command line arguments and
displays their IP addresses. Use the nslookup
command. The following is a sample run of this
program.

Homework:
HW-3) Suppose you were hacking an awesome Python
script in Vim and wanted to temporarily suspend your
editor and return to the shell to execute and test your
script. When you are fnishing testing, you wish to
resume your editting session. How would you
accomplish this?

HW-4) Oh noes! Your C program is stuck in an infnite


loop. Describe how you would terminate your program:
a) using a key sequence b) using the kill command.
HW-5) Ram is user of linux. And he is learning now
html and web designing. He has written all of html
code in diferent text fles and kept them in a directory
called webproject/. But his friend reminded that all the
fles of web programs should be in .html extension. And
all the fles should be in lower case!!!

Raghu Engineering College Department of CSE Unit-II Unix Programming


Page 12
Now kindly help him to rename all .txt fles to .html
using single command. And also convert all flenames
and extensions to lower case.

Raghu Engineering College Department of CSE Unit-II Unix Programming


Page 13

S-ar putea să vă placă și