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

UNIX Module 3 Notes

The document discusses file permissions and attributes in Unix/Linux systems. It describes the output of the ls -l command which lists file permissions, owners, sizes and other attributes. It explains the meaning of the file permission codes for owner, group and others. The document also discusses how to change file permissions using the chmod command in both relative and absolute modes.

Uploaded by

yadyad240
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)
18 views

UNIX Module 3 Notes

The document discusses file permissions and attributes in Unix/Linux systems. It describes the output of the ls -l command which lists file permissions, owners, sizes and other attributes. It explains the meaning of the file permission codes for owner, group and others. The document also discusses how to change file permissions using the chmod command in both relative and absolute modes.

Uploaded by

yadyad240
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/ 11

Unix Programming – Module 3

MODULE-3

THE BASIC FILE ATTRIBUTES


ls –l : listing file attributes
The ls command lists files and directories. If the pathname is a file, ls provides information
according to the specified options. If pathname is a directory, ls lists the files and subdirectories
contained in the specified directory. Information about the directory itself can be obtained using
the -d option.

If no pathname is provided, ls displays the files and directories in the current directory.

$ ls -l
total 32
-rw-rw-r-- 1 anjan anjan 9 Feb 14 10:02 d
-rw-rw-r-- 1 anjan anjan 6 Feb 14 09:47 dep
-rw-rw-r-- 1 anjan anjan 72 Feb 14 09:54 dept.lst
-rw-rw-r-- 1 anjan anjan 26 Feb 14 10:07 f1
-rw-rw-r-- 1 anjan anjan 26 Feb 16 03:41 f2
-rw-rw-r-- 1 anjan anjan 26 Feb 16 03:46 f3
-rw-rw-r-- 1 anjan anjan 26 Feb 14 10:03 file1
-rw-rw-r-- 1 anjan anjan 0 Feb 14 10:04 file2
-rwxrwxr-x 1 anjan anjan 548 Feb 14 10:16 pswd_chk.html

 The first column gives the type of the file (e.g., directory or ordinary file) and the file
permissions.
 The second column is the number of links to the file i.e., (more or less) the number of
names there are for the file. Generally an ordinary file will only have one link, but a
directory will have more, because you can refer to it as ``dirname'', ``dirname/.'' where the

Prof.SMITHA B N,Dept of CSE, SVIT Page 1


Unix Programming – Module 3

dot means ``current directory'', and if it has a subdirectory named ``subdir'',


``dirname/subdir/..'' (the ``..'' means ``parent directory'').
 The third and fourth columns are the user who owns the file and the Unix group of users
to which the file belongs. You almost certainly don't need to worry about Unix groups, as
you probably only belong to the default group ``users''. 
 The fifth column is the size of the file in bytes. 
 The next three columns are the time at which the file was last changed (for a directory, ths
is the time at which a file in that directory was last created or deleted).
 The last column is the name of the file.

Prefix Description
- Regular file, such as an ASCII text file, binary executable, or hard link.
b Block special file. Block input/output device file such as a physical hard drive.
c Character special file. Raw input/output device file such as a physical hard drive
d Directory file that contains a listing of other files and directories.
l Symbolic link file. Links on any regular file.
p Named pipe. A mechanism for interprocess communications
s Socket used for interprocess communication.

The –d option:
display information about named directory, rather than directory contents
ls –ld dir1 dir2
drwxr-xr-x 1 anjan anjan 9 Feb 14 10:02 dir1
drwxr-xr-x1 anjan anjan 6 Feb 14 09:47 dir2

FILE OWNERSHIP
When you create a file, you will become the owner of the file. Several users may belong to
same group. When the system admin creates a user account, he has to assign these
parameters to the user:
 The user id (UID)
 The group id(GID)

Prof.SMITHA B N,Dept of CSE, SVIT Page 2


Unix Programming – Module 3

The file /etc/passwd maintains UID


The file /etc/group maintains GID
To know your UID and GID then
$id
Uid=4532 gid=7532

FILE PERMISSIONS
Unix - File Permission
File ownership is an important component of UNIX that provides a secure method for storing
files. Every file in UNIX has the following attributes:
 Owner permissions: The owner's permissions determine what actions the owner of the
file can perform on the file.
 Group permissions: The group's permissions determine what actions a user, who is a
member of the group that a file belongs to, can perform on the file.
 Other (world) permissions: The permissions for others indicate what action all other
users can perform on the file.
$ ls -l
total 32
-rw-rw-r-- 1 anjan anjan 9 Feb 14 10:02 d
-rw-rw-r-- 1 anjan anjan 6 Feb 14 09:47 dep
-rw-rw-r-- 1 anjan anjan 72 Feb 14 09:54 dept.lst
-rw-rw-r-- 1 anjan anjan 26 Feb 14 10:07 f1
-rw-rw-r-- 1 anjan anjan 26 Feb 16 03:41 f2
-rw-rw-r-- 1 anjan anjan 26 Feb 16 03:46 f3
-rw-rw-r-- 1 anjan anjan 26 Feb 14 10:03 file1
-rw-rw-r-- 1 anjan anjan 0 Feb 14 10:04 file2
-rwxrwxr-x 1 anjan anjan 548 Feb 14 10:16 pswd_chk.html
Here first column represents different access mode ie. permission associated with a file or
directory.
The permissions are broken into groups of threes, and each position in the group denotes a
specific permission, in this order: read (r), write (w), execute (x):

Prof.SMITHA B N,Dept of CSE, SVIT Page 3


Unix Programming – Module 3

 The first three characters (2-4) represent the permissions for the file's owner. For example
-rwxr-xr-- represents that onwer has read (r), write (w) and execute (x) permission.
 The second group of three characters (5-7) consists of the permissions for the group to
which the file belongs. For example -rwxr-xr-- represents that group has read (r) and
execute (x) permission but no write permission.
 The last group of three characters (8-10) represents the permissions for everyone else.
For example -rwxr-xr-- represents that other world has read (r) only permission.

File Access Modes: The permissions of a file are the first line of defense in the security of a Unix
system. The basic building blocks of Unix permissions are the read, write, and execute
permissions, which are described below:
Read: Grants the capability to read ie. view the contents of the file.
Write: Grants the capability to modify, or remove the content of the file.
Execute: User with execute permissions can run a file as a program.
Directory Access Modes: Directory access modes are listed and organized in the same manner
as any other file. There are a few differences that need to be mentioned:
Read: Access to a directory means that the user can read the contents. The user can look at the
filenames inside the directory.
Write: Access means that the user can add or delete files to the contents of the directory.
Execute: Executing a directory doesn't really make a lot of sense so think of this as traverse
permission.
Chmod: changing the file permissions
To change file or directory permissions, you use the chmod (change mode) command. There are
two ways to use chmod: relative mode and absolute mode.

i. Relative permissions
The easiest way for a beginner to modify file or directory permissions is to use the symbolic
mode. With symbolic permissions you can add, delete, or specify the permission set you want by
using the operators in the following table.

Prof.SMITHA B N,Dept of CSE, SVIT Page 4


Unix Programming – Module 3

Chmod operator Description


+ Adds the designated permission(s) to a file or directory.
- Removes the designated permission(s) from a file or directory.
= Sets the designated permission(s).

Here is an example using testfile. Running ls -1 on testfile shows that the file's permissions are as
follows:
$ls -l testfile
-rwxrwxr-- 1 anjan users 1024 Nov 2 00:10 testfile
Then each example chmod command from the preceding table is run on testfile, followed by ls -l
so you can see the permission changes: [anjan]$chmod o+wx testfile
$ls -l testfile
-rwxrwxrwx 1 anjan users 1024 Nov 2 00:10 testfile
$chmod u-x testfile
$ls -l testfile
-rw-rwxrwx 1 anjan users 1024 Nov 2 00:10 testfile
$chmod g=r-x testfile
$ls -l testfile
-rw-r-xrwx 1 anjan users 1024 Nov 2 00:10 testfile
Here is how you could combine these commands on a single line:
$chmod o+wx,u-x,g=r-x testfile
$ls -l testfile
-rw-r-xrwx 1 anjan users 1024 Nov 2 00:10 testfile

ii. Absolute Permissions:


The second way to modify permissions with the chmod command is to use a number to specify
each set of permissions for the file.
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.

Prof.SMITHA B N,Dept of CSE, SVIT Page 5


Unix Programming – Module 3

Number Octal Permission Representation Ref


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

Here is an example using testfile. Running ls -1 on testfile shows that the file's permissions are as
follows:
$ls -l testfile
-rwxrwxr-- 1 anjan users 1024 Nov 2 00:10 testfile
Then each example chmod command from the preceding table is run on testfile, followed by ls -l
so you can see the permission changes:
$ chmod 755 testfile
$ls -l testfile
-rwxr-xr-x 1 anjan users 1024 Nov 2 00:10 testfile
$chmod 743 testfile
$ls -l testfile
-rwxr---wx 1 anjan users 1024 Nov 2 00:10 testfile
$chmod 043 testfile
$ls -l testfile
----r---wx 1 anjan users 1024 Nov 2 00:10 testfile

Changing Owners and Groups:


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 Owner and Groups.
Two commands are available to change the owner and the group of files:

Prof.SMITHA B N,Dept of CSE, SVIT Page 6


Unix Programming – Module 3

1. chown: The chown command stands for "change owner" and is used to change the owner of
a file.

2. chgrp: The chgrp command stands for "change group" and is used to change the group of a
file.

Changing Ownership:
The chown command changes the ownership of a file. The basic syntax is as follows:
$ chown user filelist
The value of user can be either the name of a user on the system or the user id (uid) of a user on
the system
Following example: [anjan]
$ chown anjan testfile
$
changes the owner of the given file to the user anjan.
NOTE: The super user, root, has the unrestricted capability to change the ownership of a any file
but normal users can change only the owner of files they own.

Changing Group Ownership:


The chrgp command changes the group ownership of a file. The basic syntax is as follows:
$ chgrp group filelist
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:
$ chgrp special testfile

Changes the group of the given file to special group

Prof.SMITHA B N,Dept of CSE, SVIT Page 7


Unix Programming – Module 3

SECURITY IMPLICATIONS
Default permissions for the newly created file is 644, for example
-rw-r—r-- 1 anjan users 1024 Nov 2 00:10 testfile
Here only user can edit no one else, if we remove all then
Chmod u –rw,go –r testfile
Or
Chmod 000 testfile
Then
---------- 1 anjan users 1024 Nov 2 00:10 testfile
We can’t do anything on this except delete.
In other hand
Chmod u +x,go+wx testfile
Or
Chmod 777 testfile
Then
-rwxrwxrwx 1 anjan users 1024 Nov 2 00:10 testfile
This is also dangerous because anyone can do anything to the testfile.

USING CHMOD RECURSIVELY


It’s make chmod descend a directory hierarchy and apply the expression to every file and
subdirectory. This is done by using –R option.
Chmod –R 755 . works on hidden files also
Chmod –R a+x * leaves out hidden files

DIRECTORY PERMISSIONS
Directories also have their own permissions, default permissions for newly created directories is
755 .

drwxr-xr-x 1 anjan users 1024 Nov 2 00:10 vtu

Prof.SMITHA B N,Dept of CSE, SVIT Page 8


Unix Progamming - Module 3

Hard Link and Soft Link


A hard link always points a filename to data on a storage device. A soft link always
points a filename to another filename, which then points to information on a storage
device.

A soft link is similar to the file shortcut feature which is used in Windows Operating
systems. Each soft linked file contains a separate Inode value that points to the original
file. As similar to hard links, any changes to the data in either file is reflected in the
other.

Page 9
Unix Progamming - Module 3

To create a hard links on a Linux or Unix-like system:


Create hard link between sfile1file and link1file, run: ln sfile1file link1file
To make symbolic links instead of hard links, use: ln -s source link
To verify soft or hard links on Linux, run: ls -l source link

Sample outputs:
4063240 -rw-r--r-- 2 root root 15 Oct 1 15:30 bar
4063240 -rw-r--r-- 2 root root 15 Oct 1 15:30 foo
Where,
4063240: Inode. From the above sample output we can see that inodes are identical. We passed the -i
option to the
ls command to display the index number of each file (inodes).
2: The number of hard links to file is shown in the third column. So if you run, ln foo hlink2, the
counter will increase to three.
How do I delete a hard link on Linux or Unix?
The rm command deletes files on Linux or Unix including a hard link. However, data is still accessible
as long as another hard link exists even if you delete source file. To get rid of data you must remove
all hard links including source.

Page 10
Unix Progamming - Module 3

umask
The umask (UNIX shorthand for "user file-creation mode mask") is a four-digit octal
number that UNIX uses to determine the file permission for newly created files. Every
process has its own umask, inherited from its parent process.
The UNIX system has the following default permissions for all files and directories
 rw-rw-rw- (octal 666)
 rwxrwxrwx (octal 777)
$umask
022
666- 022= 644 for files
777-022 = 755 for directories
umask is a shell built-in command through it also exists as an external command. A
user can also use this command to set a new default. Here’s an extreme string:
umask 000 All read-write permissions on

find
The find command in UNIX is a command line utility for walking a file hierarchy. It
can be used to find files and directories and perform subsequent operations on them. It
supports searching by file, folder, name, creation date, modification date, owner and
permissions. By using the ‘-exec’ other UNIX commands can be executed on files or
folders found.
Syntax:
$ find [where to start searching from]
Or
$find path_list selection_criteria action
• First, it recursively examines all files in the directories specified in path_list.
• It then matches each file for one or more selection_criteria.
• Finally, it takes some action on those selected files

Page 11

You might also like