Unix Module 2
Unix Module 2
For example,
$ ls -l
total 72
-rw-r--r-- 1 kumar metal 19514 may 10 13:45 chap01
-rw-r--r-- 2 kumar metal 19555 may 10 15:45 chap02
drwxr-xr-x 2 kumar metal 512 may 09 12:55 helpdir
drwxr-xr-x 3 kumar metal 512 may 09 11:05 progs
$ls -d
This command will not list all subdirectories in the current directory .
For example,
$ls –ld helpdir progs
Directories are easily identified in the listing by the first character of the first column, which here
shows d.
The significance of the attributes of a directory differs a good deal from an ordinary file.
To see the attributes of a directory rather than the files contained in it, use ls –ld with the directory
name. Note that simply using ls –d will not list all subdirectories in the current directory. Strange
though it may seem, ls has no option to list only directories.
2. Changing file permissions: the relative and absolute permissions changing methods.
File Ownership
When you create a file, you become its owner. Every owner is attached to a group owner.
Several users may belong to a single group, but the privileges of the group are set by the owner of the file
and not by the group members.
When the system administrator creates a user account, he has to assign these parameters to the user:
File Permissions
UNIX follows a three-tiered file protection system that determines a file‘s access rights.
It is displayed in the following format: Filetype owner (rwx) groupowner (rwx) others (rwx).
For Example:
-rwxr-xr- - 1 kumar metal 20500 may 10 19:21 chap02
rwx r-x r- -
owner/user group owner others
The first group(rwx) has all three permissions. The file is readable, writable and executable by the owner of
the file.
The second group(r-x) has a hyphen in the middle slot, which indicates the absence of write permission by
the group owner of the file.
The third group(r- -) has the write and execute bits absent. This set of permissions is applicable to others.
You can set different permissions for the three categories of users – owner, group and others.
A file or a directory is created with a default set of permissions, which can be determined by umask.
Let us assume that the file permission for the created file is -rw-r-- r--. Using chmod command, we can
change the file permissions and allow the owner to execute his file.
Relative Permissions
chmod only changes the permissions specified in the command line and leaves the other permissions
unchanged.
Its syntax is:
chmod category operation permission filename(s)
chmod takes an expression as its argument which contains:
user category (user, group, others)
operation to be performed (assign or remove a permission)
type of permission (read, write, execute)
Initially,
-rw-r—r-- 1 kumar metal 1906 sep 23:38 xstart
The command assigns (+) execute (x) permission to the user (u), other permissions remain
unchanged.
$chmod ugo+x xstart or chmod a+x xstart or chmod +x xstart
$ls –l xstart
Let initially,
-rwxr-xr-x 1 kumar metal 1906 sep 23:38 xstart
$chmod go-r xstart
Then, it becomes
$ls –l xstart
-rwx—x--x 1 kumar metal 1906 sep 23:38 xstart
Absolute Permissions
Here, we need not to know the current file permissions. We can set all nine permissions explicitly.
A string of three octal digits is used as an expression.
The permission can be represented by one octal digit for each category. For each category, we add
octal digits.
If we represent the permissions of each category by one octal digit, this is how the permission can be
represented:
Read permission – 4 (octal 100)
Write permission – 2 (octal 010)
Execute permission – 1 (octal 001)
Octal Permissions Significance
0 --- no permissions
will assign all permissions to the owner, read and write permissions for the group and only execute
777 signify all permissions for all categories, but still we can prevent a file from being deleted.
000 signifies absence of all permissions for all categories, but still we can delete a file.
It is the directory permissions that determine whether a file can be deleted or not.
Only owner can change the file permissions. User cannot change other user‘s file‘s permissions.
But the system administrator can do anything.
This is simply useless but still the user can delete this file.
On the other hand,
The UNIX system by default, never allows this situation as you can never have a secure system. Hence,
directory permissions also play a very vital role here .
4. Directory Permissions
It is possible that a file cannot be accessed even though it has read permission, and can be removed even
when it is write protected. The default permissions of a directory are,
rwxr-xr-x (755)
$mkdir c_progs
$ls –ld c_progs
Usually, on BSD and AT&T systems, there are two commands meant to change the ownership of a file or
directory. Let kumar be the owner and metal be the group owner. If sharma copies a file of kumar, then
sharma will become its owner and he can manipulate the attributes.
chown changing file owner and chgrp changing group owner
On BSD, only system administrator can use chown
On other systems, only the owner can change both
chown
Changing ownership requires super user permission, so use su command
$ls -l note
-rwxr --- x 1 kumar metal 347 may 10 20:30 note
Once ownership of the file has been given away to sharma, the user file permissions that previously
applied to Kumar now apply to sharma. Thus, Kumar can no longer edit note since there is no write
privilege for group and others. He cannot get back the ownership either. But he can copy the file to his
own directory, in which case he becomes the owner of the copy.
chgrp
This command changes the file‘s group owner. No super user permission is required.
#ls –l dept.lst
-rw-r—r-- 1 kumar metal 139 jun 8 16:43 dept.lst
THE SHELL
The following activities are typically performed by the shell in its interpretive cycle:
The shell issues the prompt and waits for you to enter a command.
After a command is entered, the shell scans the command line for meta characters and
expands abbreviations (like the * in rm *) to recreate a simplified command line.
It then passes on the command line to the kernel for execution.
The shell waits for the command to complete and normally can’t do any work while the
command is running.
After the command execution is complete, the prompt reappears and the shell returns to its
waiting role to start the next cycle. You are free to enter another command.
1. Wild-Cards
A pattern is framed using ordinary characters and a meta character (like *) using well-
defined rules.
The pattern can then be used as an argument to the command, and the shell will expand it
suitably before the command is executed.
The meta characters that are used to construct the generalized pattern for matching filenames
belong to a category called wild-cards.
Wild-card Matches
* Any number of characters including none
? A single character
[ijk] A single character – either an i, j or k
[x-z] A single character that is within the ASCII range of characters x and z
[!ijk] A single character that is not an i, j or k (Not in C shell)
[!x-z] A single character that is not within the ASCII range of the characters x and z (Not in C
Shell)
{pat1,pat2...} Pat1, pat2, etc. (Not in Bourne shell)
The * and ?
The metacharacter *, is one of the characters of the shell’s wild card set.
It matches any number of characters (including none).
To list all files that begin with chap.
$ ls chap*
chap chap01 chap02 chap03 chap04 chap15 chap16 chap17 chapx chapy chapz
chap* matches the string chap. When the shell encounters this command line, it identifies the *
immediately as a wild card.
It then looks in the current directory and recreates the command line as below from the
filenames that match the pattern chap*:
ls chap chap01 chap02 chap03 chap04 chap15 chap16 chap17 chapx chapy chapz
Both * and ? operate with some restrictions. for example, the * doesn’t match all files
beginning with a . (dot) or the / of a pathname.
If you wish to list all hidden filenames in your directory having at least three characters after
the dot, the dot must be matched explicitly.
$ ls .???*
.bash_profile .exrc .netscape .profile
However, if the filename contains a dot anywhere but at the beginning, it need not be
matched explicitly.
Similarly, these characters don’t match the / in a pathname. So, you cannot use, $cd
/usr?local to change to /usr/local.
The character class comprises a set of characters enclosed by the rectangular brackets, [ and ], but
it matches a single character in the class.
The pattern [abd] is character class, and it matches a single character – an a,b or d.
Examples:
$ ls chap0[124]
chap01 chap02 chap04
$ls chap[x-z]
chapx chapy chapz
Negating the character class(!)
You can negate a character class to reverse matching criteria. For example,
*.[!co] - To match all filenames with a single-character extension but not the .c or .o files
[!a-zA-Z]* - To match all filenames that don’t begin with an alphabetic character
A filename can contain a whitespace character also. Hence to remove a file named My
Documend.doc, which has a space embedded, a similar reasoning should be followed:
$rm My\ Document.doc
Quoting is enclosing the wild-card, or even the entire pattern, within quotes. Anything
within these quotes (barring a few exceptions) are left alone by the shell and not interpreted.
When a command argument is enclosed in quotes, the meanings of all enclosed special
characters are turned off.
Examples:
Standard input
The standard input can represent three input sources:
Standard output
The standard output can represent three possible destinations:
Standard error:
A file is opened by referring to its pathname, but subsequent read and write operations
identify the file by a unique number called a file descriptor.
The kernel maintains a table of file descriptors for every process running in the system.
The first three slots are generally allocated to the three standard streams as,
0 – Standard input
1 – Standard output
2 – Standard error
These descriptors are implicitly prefixed to the redirection symbols.
The shell can connect these streams using a special operator, the | (pipe) and avoid creation of the disk
file.
The pipe is the third source and destination of standard input and standard output
Examples
$ ls -l | wc –l Displays number of file in current directory
In a pipeline, all programs run simultaneously. A pipe has a built in mechanism to control the flow of the
stream.
Pipe is both being read and written, the reader and writer have to act in unison.
If one operates faster than the other, then the appropriate driver has to readjust the flow.
grep scans its input for a pattern displays lines containing the pattern, the line numbers or filenames
where the pattern occurs. The command uses the following syntax:
$grep options pattern filename(s)
grep searches for pattern in one or more filename(s), or the standard input if no filename is specified.
The first argument (except the options) is the pattern and the remaining arguments are filenames.
Examples:
grep silently returns the prompt because no pattern as “president” found in file emp.lst.
when grep is used with multiple filenames, it displays the filenames along with the output.
Option Significance
-i Ignores case for matching
-v Doesn't display lines matching expression
-n Displays line numbers along with lines
-c Displays count of number of occurrences
-l Displays list of filenames only
-e exp Matches multiple patterns
-f filename Takes patterns from file, one per line
-E Treats patterns as an ERE
-F Matches multiple fixed strings
When you look for a name but are not sure of the case, use the -i (ignore) option.
The -v option selects all the lines except those containing the pattern.
It can play an inverse role by selecting lines that does not containing the pattern.
The -n(number) option displays the line numbers containing the pattern, along with the
lines.
here, first column displays the line number in emp.lst where pattern is found
With the -e option, you can match the three agarwals by using the grep like this:
You can place all the patterns in a separate file, one pattern per line.
Grep uses -f option to take patterns from a file:
$ grep -f patterns.lst emp.lst
Like the shell's wild-cards which matches similar filenames with a single expression, grep
uses an expression of a different type to match a group of similar patterns.
Unlike shell's wild-cards, grep uses following set of meta-characters to design an expression
that matches different patterns.
If an expression uses any of these meta-characters, it is termed as Regular Expression (RE).
The below table shows the BASIC REGULAR EXPRESSION (BRE) character set-
A RE lets you specify a group of characters enclosed within a pair of rectangular brackets, [ ], in which
case the match is performed for a single character in the group.
The *
Expression Significance
ch+ Matches one or more occurrences of character ch
ch? Matches zero or one occurrence of character ch
exp1 | exp2 Matches exp1 or exp2
GIF | JPEG Matches GIF or JPEG
(x1|x2)x3 Matches x1x3 or x2x3
(hard|soft)ware Matches hardware or software
The + and ?
+ - Matches one or more occurrences of the previous character