unit02
unit02
unit02
The UNIX file system allows the user to access other files not belonging to them and
without infringing on security. A file has a number of attributes (properties) that are
stored in the inode. In this chapter, we discuss,
ls command is used to obtain a list of all filenames in the current directory. The
output in UNIX lingo is often referred to as the listing. Sometimes we combine this
option with other options for displaying other attributes, or ordering the list in a different
sequence. ls look up the file’s inode to fetch its attributes. It lists seven attributes of all
files in the current directory and they are:
The file type and its permissions are associated with each file. Links indicate the
number of file names maintained by the system. This does not mean that there are so
many copies of the file. File is created by the owner. Every user is attached to a group
owner. File size in bytes is displayed. Last modification time is the next field. If you
change only the permissions or ownership of the file, the modification time remains
unchanged. In the last field, it displays the file name.
For example,
$ ls –l
total 72
-rw-r--r-- 1 kumar metal 19514 may 10 13:45 chap01
-rw-r--r-- 1 kumar metal 4174 may 10 15:01 chap02
-rw-rw-rw- 1 kumar metal 84 feb 12 12:30 dept.lst
-rw-r--r-- 1 kumar metal 9156 mar 12 1999 genie.sh
drwxr-xr-x 2 kumar metal 512 may 9 10:31 helpdir
drwxr-xr-x 2 kumar metal 512 may 9 09:57 progs
Directories are easily identified in the listing by the first character of the first
column, which here shows a 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.
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:
The user-id (UID) – both its name and numeric representation
The group-id (GID) – both its name and numeric representation
File Permissions
UNIX follows a three-tiered file protection system that determines a file’s access
rights. It is displayed in the following format:
For Example:
The first group has all three permissions. The file is readable, writable and
executable by the owner of the file. The second group has a hyphen in the middle slot,
which indicates the absence of write permission by the group owner of the file. The third
group 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. It’s important that you understand them because a little learning here can be a
dangerous thing. Faulty file permission is a sure recipe for disaster
Relative Permissions
chmod only changes the permissions specified in the command line and leaves the
other permissions unchanged. Its syntax is:
Let initially,
Then, it becomes
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:
will assign all permissions to the owner, read and write permissions for the group and
only execute permission to the others.
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 can not 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,
-rwxrwxrwx
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
This makes all the files and subdirectories found in the shell_scripts directory, executable
by all users. When you know the shell meta characters well, you will appreciate that the *
doesn’t match filenames beginning with a dot. The dot is generally a safer but note that
both commands change the permissions of directories also.
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)
Example:
mkdir c_progs
ls –ld c_progs
If a directory has write permission for group and others also, be assured that every
user can remove every file in the directory. As a rule, you must not make directories
universally writable unless you have definite reasons to do so.
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
ls -l 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 can not 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 superuser permission is required.
ls –l dept.lst
To write and edit some programs and scripts, we require editors. UNIX provides vi
editor for BSD system – created by Bill Joy. Bram Moolenaar improved vi editor and
called it as vim (vi improved) on Linux OS.
vi Basics
vi <filename>
In all probability, the file doesn’t exist, and vi presents you a full screen with the
filename shown at the bottom with the qualifier. The cursor is positioned at the top and
all remaining lines of the screen show a ~. They are non-existent lines. The last line is
reserved for commands that you can enter to act on text. This line is also used by the
system to display messages. This is the command mode. This is the mode where you can
pass commands to act on text, using most of the keys of the keyboard. This is the default
mode of the editor where every key pressed is interpreted as a command to run on text.
You will have to be in this mode to copy and delete text
For, text editing, vi uses 24 out of 25 lines that are normally available in the
terminal. To enter text, you must switch to the input mode. First press the key i, and you
are in this mode ready to input text. Subsequent key depressions will then show up on the
screen as text input.
After text entry is complete, the cursor is positioned on the last character of the
last line. This is known as current line and the character where the cursor is stationed is
the current cursor position. This mode is used to handle files and perform substitution.
After the command is run, you are back to the default command mode. If a word has been
misspelled, use ctrl-w to erase the entire word.
Now press esc key to revert to command mode. Press it again and you will hear a
beep. A beep in vi indicates that a key has been pressed unnecessarily. Actually, the text
entered has not been saved on disk but exists in some temporary storage called a buffer.
To save the entered text, you must switch to the execute mode (the last line mode).
Invoke the execute mode from the command mode by entering a: which shows up in the
last line.
ctrl-l
:set showmode
Messages like INSERT MODE, REPLACE MODE, CHANGE MODE, etc will appear in
the last line.
Pressing ‘i’ changes the mode from command to input mode. To append text to the right
of the cursor position, we use a, text. I and A behave same as i and a, but at line extremes
I inserts text at the beginning of line. A appends text at end of line. o opens a new line
below the current line
COMMAND FUNCTION
i inserts text
a appends text
I inserts at beginning of line
A appends text at end of line
o opens line below
O opens line above
r replaces a single character
s replaces with a text
S replaces entire line
When you edit a file using vi, the original file is not distributed as such, but only a
copy of it that is placed in a buffer. From time to time, you should save your work by
writing the buffer contents to disk to keep the disk file current. When we talk of saving a
file, we actually mean saving this buffer. You may also need to quit vi after or without
saving the buffer. Some of the save and exit commands of the ex mode is:
Command Action
:W saves file and remains in editing mode
:x saves and quits editing mode
:wq saves and quits editing mode
:w <filename> save as
:w! <filename> save as, but overwrites existing file
:q quits editing mode
:q! quits editing mode by rejecting changes made
:sh escapes to UNIX shell
:recover recovers file from a crash
Navigation
A command mode command doesn’t show up on screen but simply performs a function.
To move the cursor in four directions,
k moves cursor up
j moves cursor down
h moves cursor left
l moves cursor right
Word Navigation
Moving by one character is not always enough. You will often need to move faster
along a line. vi understands a word as a navigation unit which can be defined in two
ways, depending on the key pressed. If your cursor is a number of words away from your
desired position, you can use the word-navigation commands to go there directly. There
are three basic commands:
Example,
0 or |
Scrolling
Faster movement can be achieved by scrolling text in the window using the
control keys. The two commands for scrolling a page at a time are
Absolute Movement
The editor displays the total number of lines in the last line
Editing Text
The editing facilitates in vi are very elaborate and invoke the use of operators. They use
operators, such as,
d delete
y yank (copy)
Deleting Text
Moving Text
Copying Text
Joining Lines
vim (LINUX) lets you undo and redo multiple editing instructions. u behaves
differently here; repeated use of this key progressively undoes your previous actions. You
could even have the original file in front of you. Further 10u reverses your last 10 editing
actions. The function of U remains the same.
You may overshoot the desired mark when you keep u pressed, in which case use
ctrl-r to redo your undone actions. Further, undoing with 10u can be completely reversed
with 10ctrl-r. The undoing limit is set by the execute mode command: set undolevels=n,
where n is set to 1000 by default.
The . (dot) command is used for repeating the last instruction in both editing and
command mode commands
For example:
2dd deletes 2 lines from current line and to repeat this operation, type. (dot)
/printf
The search begins forward to position the cursor on the first instance of the word
?pattern
Searches backward for the most previous instance of the pattern
Command Function
We can perform search and replace in execute mode using :s. Its syntax is,
:address/source_pattern/target_pattern/flags
Interactive substitution: sometimes you may like to selectively replace a string. In that
case, add the c parameter as the flag at the end:
:1,$s/director/member/gc
Each line is selected in turn, followed by a sequence of carets in the next line, just below
the pattern that requires substitution. The cursor is positioned at the end of this caret
sequence, waiting for your response.
The ex mode is also used for substitution. Both search and replace operations also
use regular expressions for matching multiple patterns.
The features of vi editor that have been highlighted so far are good enough for a
beginner who should not proceed any further before mastering most of them. There are
many more functions that make vi a very powerful editor. Can you copy three words or
even the entire file using simple keystrokes? Can you copy or move multiple sections of
text from one file to another in a single file switch? How do you compile your C and Java
programs without leaving the editor? vi can do all this.