0% found this document useful (0 votes)
12 views37 pages

SPL Lecture2

This document serves as a guide for a Systems Programming Laboratory course at IIT Kharagpur, covering basic UNIX commands, file system structure, file attributes, permissions, and commands for managing files and directories. It details the usage of commands like 'ls', 'chmod', 'chown', and 'chgrp', explaining how to list file attributes, change permissions, and manage file ownership. Additionally, it introduces the concept of inodes and their role in storing metadata about files.
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)
12 views37 pages

SPL Lecture2

This document serves as a guide for a Systems Programming Laboratory course at IIT Kharagpur, covering basic UNIX commands, file system structure, file attributes, permissions, and commands for managing files and directories. It details the usage of commands like 'ls', 'chmod', 'chown', and 'chgrp', explaining how to list file attributes, change permissions, and manage file ownership. Additionally, it introduces the concept of inodes and their role in storing metadata about files.
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/ 37

Systems Programming Laboratory,

Spring 2025

Instructor: Koustav Rudra


Teaching Assistants: Dipankar Mandal, Subhankar Maity

Department of Artificial Intelligence (AI)


Indian Institute of Technology Kharagpur
January 13, 2025
A Few Basic Commands

❏ Using date Every UNIX system maintains an internal clock that you can access to print the
current system date and time.

❏ If your account has a password that is already known to others, you should change it
immediately:
The UNIX File System Tree

/: root dir

/bin: commands

/etc: system data


files
(e.g., /etc/passwd)

/dev: file
representing I/O
devices
The UNIX File System
❏ /sbin and /usr/sbin If there’s a command that you can’t execute but the system
administrator can, then it would probably be in one of these directories.
❏ /dev This directory contains all device files.
❏ /lib and /usr/lib These directories contain all library files in binary form. You need to
link your C programs with files in these directories.
❏ /usr/include This directory contains the standard header files used by C programs. The
statement #include <stdio.h> used in most C programs refers to the file stdio.h in this
directory.
❏ /usr/share/man This is where the man pages are stored. There are separate
subdirectories here (like man1, man2, etc.) that contain the pages for each section.
❏ /tmp The directories where users are allowed to create temporary files. These files are
wiped away regularly by the system.
❏ /var The variable part of the file system. Contains all of your print jobs and your outgoing
and incoming mail.
❏ /home On many systems, users are housed here. romeo would have his home directory in
/home/romeo. However, your system may use a different location for home directories.
A data structure storing metadata about files. Each file is
associated with an inode, identified by a unique inode
Basic File Attributes number (within its file system).

● A file has a number of attributes (properties) that are stored in inode.

● The two basic attributes are: permissions and ownership – both of them are changeable
by well-defined rules.
# Listing File Attributes:
$ ls -l

● The -l option (i.e., long listing format) displays most attributes of a file, such as
permissions, size and ownership.

● ls looks up the file’s inode to fetch its attributes.


Listing of Files with ls -l
File Types & Permissions
● The first column of the first field shows the file type. Here we see three possible values, a -
(ordinary file), d (directory), or l (symbolic link).

● The first character in this column is mostly a - (hyphen), which indicates the file is an ordinary
one. Most files here are ordinary files, but c_progs and shell_scripts are directories.

● The directories are indicated by d at the same position. A file can have 3 types of
permissions: r (read), w (write), x (execute)
Links
● The second field in file details shows the number of links associated with a file.

● UNIX lets a file have multiple names, and each name is interpreted as a link.

● Directories have a link count of at least two, but here two ordinary files (backup.sh and
restore.sh) also have two links each.
Ownership and Group Ownership
● Every file has an owner. The third field shows romeo as the owner of most of the files.

● A user also belongs to a group, and the fourth field shows metal as the group owner of
most of the files.

● The owner can tamper (create/modify/remove) with a file in every possible way—a
privilege that is also available to the root user.
File Size
● The fifth field shows the file size in bytes.

● The kernel allocates space in blocks of 1024 bytes or more, so even though backup.sh
contains 163 bytes, it could occupy 1024 bytes on this system.
Last Modification Time & File Name
● The sixth field displays the last modification time in three columns—a time stamp that is
stored to the nearest second. A file is modified only if its contents have changed in any way.

● If we change the permissions or ownership of the file, modification time remains unchanged.

● The last field displays the filename, which can be up to 255 characters long.
Listing Directory Attributes

● The -d option in ls command lists the attributes of a directory, rather than its
contents.
File Permissions
● UNIX has a simple and well-defined system of assigning permissions to files.

$ ls -l backup.sh

Structure of a File’s Permissions String


chmod: Changing File Permissions
● The chmod command (change mode) is used to set the permissions of one or more files for all 3
categories of users (i.e., user, group, and others). The command uses the following syntax:

chmod [-R] mode file …

● In a relative manner by specifying the changes to the current permissions.

● In an absolute manner by specifying the final permissions.

● We’ll consider both ways of using chmod, but just remember that only the owner of this file (romeo)
can change these permissions.
Relative Permissions
● When changing permissions in a relative manner, chmod only changes the permissions specified in
mode and leaves the other permissions unchanged.

Structure of a chmod Command Abbreviations Used by chmod

● The mode as used in the syntax contains three components:


○ User category (user, group, others)
○ The operation to be performed (assign or remove a permission)
○ The type of permission (read, write, execute)
Examples…
❏ To make the file date.sh executable, we need to assign (+) execute permission (x) to
the user (u). The expression required is u+x:
❏ Permissions are removed with the - operator. This is how we revert to the original permissions:

❏ The expression can comprise multiple categories. This is how we assign execute permission to
all:

The synonym a is available for ugo, so ugo+x is the same as a+x (or even +x).
❏ chmod accepts multiple filenames in a command line. To assign same permissions to a
group of files:

$chmod u+x file1 file2 file3

❏ Permissions are removed with the ‘-’ operator. To remove the read and execute
permission from both g and o.

$chmod go-rx date.sh ; ls -l date.sh

-rwx------ 1 romeo metal 5 Aug 16 16:05 date.sh


❏ How do we revert now to the original permissions?

We need to remove the execute permission from user and assign read permission to
the other two categories. This requires two expressions, and using a comma as a
delimiter between them, we can use a single invocation of chmod:
Absolute Assignment

❏ The = operator can perform a limited form of absolute assignment. It assigns only the
specified permissions and removes other permissions. Thus, if a file is to be made read
only to all, we can simply use one of these three forms:

This technique has its limitations; you can’t just set all nine permission bits explicitly.

Absolute assignment is actually done with octal numbers.


❏ Octal numbers use the base 8, and octal digits have the values 0 to 7. This means that
a set of three bits can represent one octal digit. If we represent the permissions of each
category by one octal digit, then this is how the permissions can be represented:

❏ Read permission: 4 (Octal 100)


❏ Write permission: 2 (Octal 010)
❏ Execute permission: 1 (Octal 001)
❏ The default file permissions on our system are rw-r--r--. This is octal 644, so let’s use it
with chmod:
❏ It’s obvious that 000 indicates the absence of all permissions, and 777 signifies the
presence of all permissions for all categories.
Using chmod Recursively

❏ chmod -R descends a directory hierarchy and applies the expression to every file and
subdirectory it finds in the tree-walk:

$ chmod -R a+x shell_scripts

❏ So, to use chmod on your home directory tree, “cd” to it and use it in one of these ways:

$ chmod -R 755 . # Works on hidden files also

$ chmod -R a+x * # Leaves out hidden files


chmod Usage
The Directory

❏ What is a Directory?

❏ Stores filenames and inode numbers.

❏ Size depends on the number of files, not their size.


Read Permission

❏ Read permission for a directory means that the list of filenames stored in that directory is
accessible.

Removes read permission for the user on


the progs directory

Fails because the user can't list the contents


of the directory without read permission.
Write Permission
❏ Write permission for a directory implies that you are permitted to create or remove files
in it (that would make the kernel modify the directory entries).

Displays details of the current directory and


Removes write permission for the user on
Adds write permission to the the file date.sh
the file date.sh
directory for the user.

Deletes the file, prompting for


confirmation because the file
lacks write permission.
Execute Permission
❏ Means that a user can “pass through” the directory in searching for subdirectories.

e.g., $ cat /home/romeo/progs/date.sh

Shows that the directory now lacks execute


Removes execute permission from the directory
(x) permission
progs.

Fails because without execute permission, the


directory cannot be traversed.
umask: Default File and Directory Permissions
❏ By default, files are created with permissions of 666 (read and write for all users). Directories
are created with permissions of 777 (read, write, and execute for all users).

❏ Actually, this default is transformed by subtracting the user mask from it to remove one or
more permissions.

Creates a directory named Lists details of the


All categories have read and execute permissions
progs progs directory (not its
and only the user has write permission. contents)
Effect of umask Settings on Default Permissions
Default File and Directory Permissions
❏ rw-rw-rw- (octal 666) for regular files.

❏ rwxrwxrwx (octal 777) for directories.

❏ The two commands used to change the ownership of a file/directory are $ chown
(change file owner) and $ chgrp (changing group owner)

❏ Changing ownership requires superuser permission

$→#
chown: Changing File Ownership

❏ To renounce the ownership of the file date.sh to juliet, use chown in the following way:
chgrp: Changing Group Owner

❏ chgrp shares a similar syntax with chown. In the following example, romeo changes the
group ownership of a file to dba (no superuser permission required):
Inodes
Inode (Index Node): A data structure storing metadata about files. Each file is associated with an inode, identified
by a unique inode number (within its file system).

Attributes Stored in an Inode:

❏ File type (regular, directory, device, etc.)


❏ File permissions (the nine permissions and three more)
❏ Number of links (the number of aliases the file has)
❏ The UID of the owner
❏ The GID of the group owner
❏ File size in bytes
❏ Date and time of last modification
❏ Date and time of last access
❏ Date and time of last change of the inode
❏ An array of pointers that keep track of all disk blocks used by the file

Attributes Not Stored in Inodes:

❏ File name and inode number (stored in the directory). Both attributes are stored in the directory.
Thank you!

You might also like