UNIX Module 3 Notes
UNIX Module 3 Notes
MODULE-3
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
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)
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):
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.
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
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
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.
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.
DIRECTORY PERMISSIONS
Directories also have their own permissions, default permissions for newly created directories is
755 .
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
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