UNIT - 2
UNIX File Attributes
The Unix File Attributes packet consists of the following:
Filename
is the fully qualified filename.
File-Attributes
consists of the 13 fields of the stat() buffer in ASCII base64 format separated by spaces. These fields and their meanings are shown below. This stat() packet is in Unix format, and MUST be provided (constructed) for ALL systems.
Link
when the FT code is FT_LNK or FT_LNKSAVED, the item in question is a Unix link, and this field contains the fully qualified link name. When the FT code is not FT_LNK or FT_LNKSAVED, this field is null.
Extended-Attributes
The exact format of this field is operating system dependent. It contains additional or extended attributes of a system dependent nature. Currently, this field is used only on WIN32 systems where it contains a ASCII base64 representation of the WIN32_FILE_ATTRIBUTE_DATA structure as defined by Windows. The fields in the base64 representation of this structure are like the File-Attributes separated by spaces.
The File-attributes consist of the following:
Field No. | Stat Name | Unix | Win98/NT | MacOS |
1 | st_dev | Device number of filesystem | Drive number | vRefNum |
2 | st_ino | Inode number | Always 0 | fileID/dirID |
3 | st_mode | File mode | File mode | 777 dirs/apps; 666 docs; 444 locked docs |
4 | st_nlink | Number of links to the file | Number of link (only on NTFS) | Always 1 |
5 | st_uid | Owner ID | Always 0 | Always 0 |
6 | st_gid | Group ID | Always 0 | Always 0 |
7 | st_rdev | Device ID for special files | Drive No. | Always 0 |
8 | st_size | File size in bytes | File size in bytes | Data fork file size in bytes |
9 | st_blksize | Preferred block size | Always 0 | Preferred block size |
10 | st_blocks | Number of blocks allocated | Always 0 | Number of blocks allocated |
11 | st_atime | Last access time since epoch | Last access time since epoch | Last access time -66 years |
12 | st_mtime | Last modify time since epoch | Last modify time since epoch | Last access time -66 years |
13 | st_ctime | Inode change time since epoch | File create time since epoch | File create time -66 years |
There are two ways we can edit previous commands in the Kom and Bash shells and one way in the C shell, In the Korn and Bash shells, we can use the history file or we can use command-line editing. The history file is a special UNIX file that contains a list of commands used during a session. In the C shell, we can use only the history file. As each command is entered on the command/line, the Korn shell copies it to a special file. With command-line editing, we can edit the commands using either vi or emacs without opening the file. It's as though the shell keeps the file in a buffer that provides instant access to our commands. Whenever a command completes, the shell moves to the next command line and waits for the next command. This concept is shown in figure.
vi stands for visual editor. It is a full screen editor. It is available with almost all the UNIX variants. vi editor became famous because of its full screen editing capabilities and many other visual options. vi editor is available under Linux in the form of vim (vi improved). It uses the terminal display as a window to the buffer and 25 lines can be displayed at all times. All unused lines of a file are marked by a -(tilde) called as empty buffer line flag. The last line is reserved for commands. The vi editor works in three modes that are:
1. The Command mode : This is the mode into which the user gets into as soon as the vi command is invoked from shell by using vi <filename>. In this mode all the keys pressed by the user are interpreted to be the editor commands. The keys that are pressed are not displayed on the screen. The user can get into the input mode by giving o, O, r, R, i, I, s, S, a, A commands.
2. The input mode : This mode is also known as insert mode. This mode permits the insertion of new text, appending to the existing text and replacement of the text. To exit or switch from this mode to command mode press Esc key.
3. The ex mode : This mode is also known as last line mode and allows user to use the commands in the bottom line (command line) on the vi screen. From the command mode the user can get in to the ex mode by typing the colon character (). Here this character is displayed in the command line. A colon (:) indicates that the editor is now in the ex mode. The user can get back to the command mode by using the Enter key
Figure 1
1. The input mode command: (i) Insert (i, I): i - Insert text to the left of the cursor position. Existing text will be shifted right This is a file isimple [Esc] This is a simple file I- Insert text at the beginning of the line. This is a file I/*[Esc] /*This is a simple file
(ii) Append (a, A): a- Append text to the right of the cursor position. Existing text will be shifted right. This is a file isimple [Esc] This is a simple file A- Appends text at end of the line. This is a file A*/ [Esc] This is a simple file */
(iii) Open a line (o, O): o- Opens a new line below the current line. hello how you oare [Esc] hello how are you O-Opens a new line above the current line. hello how you Osumit [Esc] hello sumit how are you (iv) Replace (r, R, s, S): r- Replace single character. This is a dile rf This is a file R-Replaces all text on the right of the cursor position. This is a simple file Rdevice[Esc] This is a device file s- Replace one character with many. This is a device file sone[Esc] This is one device file S - Replaces the entire line irrespective of the cursor position (existing line disappears).
2. The ex-mode command: (i) :w- To save a file and remain in the editing mode. : w "ss", 8 lines, 150 characters The message shows the name of the file i.e. ss along with the number of lines and character saved. We can also specify another file in the last line (In MS word, save as). :w ss1 "ss1", 8 lines, 150 characters If ssl is already exist then we use exclamation mark (!) with w :w! ss1 Content of ss will be overwrite to ssl. If we want to save fifth line to the another file then we will use : 5w ss1 If we want to save ten to fifteenth line to the another file then we will use : 10, 15w ss1 There are two symbols used with w have special significance - The dot (,) and dollar ($). The dot (.) represents the current line and dollar ($) represent the last line. :.w ss1 //saves current line : $w ss1 //saves last line : .,$ ss1 //saves current line through end
(ii) : q - Aborting editing : It is also possible to abort the editing process and quitting from the editing mode without saving the content with the help of : q command. : q[enter] $_ vi also has a safety mechanism that prevents we from aborting accidentally if we have modified the file. The following message will be displayed. No write since last change If the buffer has been changed and we still want to abandon the changes then we will use :q! //ignores all changes made and quits (iii): wq - saving and quiting : We can also use: wq to save and exit from vi editor. :wq ss1 (iv):sh - Escapes to UNIX shell: Vi editor provide temporary escape to the shell to run the cc command. :sh This return a shell prompt execute cc or any other command here and then return to the editor using exit or [Ctrl - d] /* don't execute vi command once again - an undesirable situation */
3. The command mode command: A command mode command doesn't on the screen but simply performs a function. k- Moves cursor up i- Move cursor down h - Moves cursor left 1 - Moves cursor right The repeat factor can be used as a command prefix with all these four commands. Thus 4k command moves the cursor 4 lines up and 20h takes it 20 characters to the left
b - Moves back to the beginning of word w - Moves forward to the beginning of word e - Moves forward to the end of word 0 - Moves cursor to extreme left $ - Moves cursor to extreme right |(pipe) - Moves cursor to specific column The repeat factor can also be used with this command. Thus 5b takes the cursor 5 words back and 3w takes the cursor three words forward. [Ctrl – f]- Scroll forward [Ctrl - b]- Scrolls backward [Ctrl – d]- Scrolls half page forward [Ctrl – u]- Scrolls half page backward The repeat factor can also be used with this command. Thus 10[Ctrl – f], to scroll 10 pages forward. When we start vi editor, it displays the total number of lines in the last line. To know the current line number we will use. [Ctrl – g]- To know the current line number "file1" [read only] line 67 of 233 -32%- The cursor is on line 67 i.e. (32% of 233) (i) G- Move to the specific line number (absolute movement) 23G //Goes to line number 23 1G //Goes to line number 1 G //Goes to end of file (ii) x - Deletes a single character: The character under the cursor gets deleted and the text on the right shifts left to fill up the space. A repeat factor also used here. Thus 4x deletes the current character as well as three characters from the right dd - Deletes an entire line: dd command deletes a current line where cursor can be anywhere in the line. A repeat factor can also be used here, thus 4dd deletes the current line as well as three lines below. p- Put character on the right of the cursor When we delete parts of line P- Put character on the left of the cursor e.g. If we want to correct sdtio.h to stdio.h, then we will have to transpose the characters d and t, i.e. delete the d and put it after t. Move your cursor to the d in sdtio.h then press. x //sdtio.h becomes stio.h :cursor on t p //d put on right : stio.h becomes stdio.h
p - Put character on the above of the current line When we delete an entire line P- Put character on the below of the current line
p and P place text on the left and right only when you delete parts of lines. But the same keys get associated with above and below when we delete complete line.
(ii) Yanking (copying) the text: yh - Yank a character to the left of the current cursor position. 3yh - Yank three characters to the left of the current cursor position. yw - Yank from the current character to the next word. Yor yy - Yanks the current line. y2k - Yank current line and two lines above the current line. 3Y or 3yy - yank three lines including the current line. Y$ - Yank till the end of the current line. y) - Yank from the current character to next sentence. y} - Yank from the current character till the end of the paragraph. 3y - yank three character.
(iv) Joining the line: J - To join the current line and the line following it. 4J - To join the current line with 3 lines following it.
(v) Undoing last editing instruction : u - Undo the most recent single editing change by restoring the position before the change. U-Undo all the changes before move away from the line. [Ctrl -r] This command is used to redo the undone actions. The repeat factor also works here. 10u is completely reversed with 10[Ctrl -r]
(vi) Searching for a pattern (/ or?) : /pattern - Searches forward for pattern. ?pattern - Searches backward for pattern. eg. if we are looking for the string echo, Then use /echo @ The search begins forward to position the cursor on the first instance of the word, vi searches the entire line, so if the pattern can't be located till the end of the file is reached the search wraps around to resume from the beginning of the file. If the search still fails, vi respond with the message pattern not found ?echo @ (same like /) The search begins backward direction. Repeating the last pattern search: n - Repeats search in same direction along which previous search was made. N - Repeats search in opposite direction to that along which previous search was made.
|
References
- Sumitabha Das: UNIX – Concepts and Applications, 4th Edition, Tata McGraw Hill, 2006.
- Behrouz A. Forouzan and Richard F. Gilberg: UNIX and Shell Programming, Cengage Learning, 2005.
- M.G. Venkateshmurthy: UNIX & Shell Programming, Pearson Education, 2005.