Presentation is loading. Please wait.

Presentation is loading. Please wait.

UNIT-2 Basic File Attributes Course code: 10CS44

Similar presentations


Presentation on theme: "UNIT-2 Basic File Attributes Course code: 10CS44"— Presentation transcript:

1 UNIT-2 Basic File Attributes Course code: 10CS44
Engineered for Tomorrow Prepared by : Thanu Kurian Department :Computer Science Date :

2 Engineered for Tomorrow
Basic File Attributes A file has a number of attributes (properties) that are stored in the inode. The two basic attributes are – permissions and ownership –both of which are changeable by well-defined rules. ls –l :Listing File Attributes: The –l option (long listing format) displays most attributes of a file –like its permissions, size and ownership details. ls looks up the file’s inode to fetch its attributes.

3 -rw-r- -r-- 1 kumar metal 17890 Aug 15 12:30 chap01
Engineered for Tomorrow Example $ ls –l Total 72 -rw-r- -r kumar metal Aug :30 chap01 -rw-r- -r kumar metal Jan :45 chap02 -rw-r- -r kumar metal Feb :20 chap03 -rw-r- -r kumar metal Jul :50 chap05 drwxr-xr-x kumar metal Jun :10 helpdir drwxr-xr-x kumar metal Sep :35 progs

4 File Type and Permissions:
Engineered for Tomorrow File Type and Permissions: The first column shows the type and permissions associated with each file. The first character in this column is mostly a -(hyphen) ,which indicates that the file is an ordinary one. The directories if any are indicated by d at the same position. A file can have 3 types of permissions: -read, write and execute. Links: The second column indicates the number of links associated with the file. This is actually the number of filenames maintained by the system of that file. A link count greater than one indicates that the file has more than one name. This doesn’t mean that there are 2 copies of the file.

5 The 3rd column shows kumar as the owner of all the files.
Engineered for Tomorrow Ownership: The 3rd column shows kumar as the owner of all the files. When we create a file, we automatically become its owner. The owner has full authority with a file’s contents and permissions. The owner can create , modify or remove files in a directory.

6 The 4th column represents the group owner of the file.
Engineered for Tomorrow Group Ownership: When opening a user account, the system admin also assigns the user to some group. The 4th column represents the group owner of the file. Group members often work on the same file. File Size: The 5th column indicates the size of the file in bytes. i.e., amount of data it contains. It is only a character count of the file and not a measure of the disk space that it occupies.

7 Last Modification Time:
Engineered for Tomorrow Last Modification Time: The 6th,7th and 8th columns indicate the last modification time of the file. A file is modified only if its contents have changed in any way. If we change the permissions or ownership of the file , modification time remains unchanged. Filename: The last column displays the filenames arranged in ASCII collating sequence.

8 The –d option: Listing Directory Attributes:
Engineered for Tomorrow The –d option: Listing Directory Attributes: The -d option in ls command lists the attributes of a directory, rather than its contents. e.g.: $ ls –ld helpdir progs drwxr-xr-x kumar metal Jun :10 helpdir drwxr-xr-x kumar metal Sep :35 progs The 1st character of the 1st column says that it’s a Directory (d). Ordinary files are identified by a –(hyphen). Device files are identified by either a b or c.

9 Engineered for Tomorrow
File Ownership: When a user creates a file, he/she becomes the owner of the file. We can’t create files in other user’s home directories, b’coz these directories are not owned by us. Several users may belong to a single group. All files created by group members (who have separate user-ids) will have the same group owner.

10 The user-id (UID)- both its name and numeric representation.
Engineered for Tomorrow When the system admin creates a user account, he has to assign the following parameters to the user: The user-id (UID)- both its name and numeric representation. The group-id (GID)- both its name and numeric representation. The file /etc/passwd maintains the UID (both number and name) and GID (but only the number). The file /etc/group contains the GID (both number and name). Tip: To know your own UID and GID , use the id command. $ id uid=655537(kumar) gid=655535(metal)

11 e.g. Use ls –l command to view the permissions of few files.
Engineered for Tomorrow File Permissions: UNIX has a simple and well-defined system of assigning permissions to files. e.g. Use ls –l command to view the permissions of few files. $ ls –l chap02 dept.lst dateval.sh -rwxr-xr kumar metal May :21 chap02 -rwxr-xr-x kumar metal Jan :15 dept.lst -rw-rw-rw kumar metal Mar :30 dateval.sh The first column represents the file permissions. They are different for the 3 files above.

12 -rwxr-xr-- 1 kumar metal 2050 May 10 19:21 chap02
Engineered for Tomorrow Consider the file chap02. -rwxr-xr kumar metal May :21 chap02 -rwx r-x r- - Each group represents a category and contains 3 slots, representing read, write and execute permissions of the file. r-read permission: cat command can display that file. w-write permission: we can edit such a file with an editor. x-execute permission: the file can be executed as a program The – shows the absence of the corresponding permission.

13 The second group (r-x):
Engineered for Tomorrow The first group (rwx): has all 3 permissions. the file is readable, writable and executable by the owner (user) of the file, kumar. The second group (r-x): has only read and execute permissions and absence of write permissions by the group owner of the file. This group owner is metal and all users belonging to the metal group have read and execute permissions only. The third group (r--): has only read permission, write and execute permissions are absent. this set of permissions is applicable to others.

14 So there are 3 categories of users: Owner (u) Group (g) Others (o)
Engineered for Tomorrow So there are 3 categories of users: Owner (u) Group (g) Others (o) We can set different permissions for these 3 categories

15 chmod: Changing File Permissions
Engineered for Tomorrow chmod: Changing File Permissions Default setting write protects a file from all except the user The chmod (change mode) command is used to set the permissions of one or more files for all 3 categories of users (user, group and others) chmod command can be used in 2 ways: In a relative manner by specifying the changes to the current permissions. In an absolute manner by specifying the final permissions.

16 chmod category operation permission filename (s)
Engineered for Tomorrow Relative Permission: when changing permissions in a relative manner, chmod only changes the permissions specified in the command line and leaves other permissions unchanged. syntax is: chmod category operation permission filename (s)

17 Relative Permissions The 3 components are:
Engineered for Tomorrow Relative Permissions The 3 components are: i) user category (u, g and o) ii) the operation to be performed (assign or remove a permission) ii) the type of permission (R,W & Ex) The abbreviations used by chmod :

18 -rw-r- - r- - 1 kumar metal 1906 Sep 5 22:30 xstart
Engineered for Tomorrow Example: $ ls –l xstart -rw-r- - r kumar metal Sep 5 22:30 xstart By default, a file doesn’t have execute permission. To assign the execute permission (x) to the user of the file xstart, the following command is used: $ chmod u+x xstart S ls –l xstart -rwxr- -r kumar metal Sep 5 22:30 xstart The command assigns (+) execute (x) permission to user (u), but other permissions remain unchanged.

19 $ chmod ugo+x xstart ; ls –l xstart
Engineered for Tomorrow We can now execute the file if we are the owner of the file but other categories (g and o) still cannot. To enable all: $ chmod ugo+x xstart ; ls –l xstart -rwxr-xr-x 1 kumar metal Sep :30 xstart Alternatively, $ chmod a+x xstart // a implies ugo chmod +x xstart // By default, a is implied. chmod accepts multiple filenames in the command line. To assign the same permission to a group of files, use the following: chmod u+x note note1 note2

20 -rwxr- xr-x 1 kumar metal 1906 Sep 05 22:30 xstart
Engineered for Tomorrow Permissions are removed with the ‘–’ operator. To remove the read permission from both g and o: $ ls –l xstart -rwxr- xr-x kumar metal Sep :30 xstart $ chmod go-r xstart; ls –l xstart -rwx--x--x kumar metal Sep :30 xstart More than one permissions can also be set as shown below: $ chmod o+wx xstart ; ls –l xstart -rw-r--rwx kumar metal Sep :30 xstart chmod also accepts multiple expressions delimited by commas. $ chmod a-x, go+r xstart ; ls –l xstart -rw-r- -r kumar metal Sep :30 xstart

21 Absolute Permissions:
Engineered for Tomorrow Absolute Permissions: The permission is absolute when used with octal numbers. The expression used by chmod in absolute permission is a string of 3 octal numbers (base 8) A set of 3 bits represent one octal digit. Permission of each category is represented by one octal digit. Read permission :4 (octal 100) Write permission :2(octal 010) Execute permission :1(octal 001)

22 Engineered for Tomorrow

23 Example: $ chmod 666 xstart ; ls –l xstart
Engineered for Tomorrow Example: $ chmod 666 xstart ; ls –l xstart -rw-rw-rw kumar metal Sep :30 xstart Here, 6 indicates read and write permissions(4+2). To restore the original permissions to the file, remove the write permission (2) from group and others. $ chmod 644 xstart ; ls –l xstart -rw-r--r kumar metal Sep :30 xstart To assign all permissions to the owner, R and W permissions to the group , and only Execute permission to the others, use this: $ chmod 761 xstart

24 Using chmod Recursively (-R):
Engineered for Tomorrow Using chmod Recursively (-R): chmod can descend a directory hierarchy and apply the expression to every file and subdirectory it finds .This is done with the - R (recursive ) option. chmod –R a+x shell_scripts This makes all files and subdirectories found in the tree-walk executable by all users.

25 Directory Permissions:
Engineered for Tomorrow Directory Permissions: The default permissions of a directory on the system is found as follows: $ mkdir c_progs ; ls –ld c_progs drwxr-xr-x kumar metal May :45 c_progs The default permissions of a directory on the system are : rwxr-xr-x (or 755) A directory must never be writable by group and others.

26 Changing File Ownership:
Engineered for Tomorrow Changing File Ownership: The two commands used to change the ownership of a file or directory are: chown and chgrp. chown :changing File Owner Usage is: chown options owner [:group] file(s) chown transfers ownership of a file to a user. Changing ownership requires superuser permission. This can be done as follows: $ su Password: ****** #_ //this is another shell

27 -rwxr----x 1 kumar metal 345 May 10 20:20 note
Engineered for Tomorrow Now , to renounce the ownership of the file note to sharma, use chown in the following way: # ls –l note -rwxr----x kumar metal May :20 note # chown sharma note ; ls –l note -rwxr----x sharma metal May :20 note # exit $_ Now, the user file permissions that previously applied to kumar now apply to sharma. Kumar can no longer edit the file note since there is no write privilege for group and others. Also , kumar can’t get back the ownership.

28 chgrp: Changing Group Owner
Engineered for Tomorrow chgrp: Changing Group Owner chgrp (change group owner) command changes a file’s group owner. the group owner of a file is the group to which the owner belongs. A user can belong to more than one group. in Linux system, a user can change the group owner of a file, but only to a group to which he/she also belongs. e.g.: $ ls –l dept.lst -rw-r- -r kumar metal Jun :45 dept.lst $ chgrp dba dept.lst ; ls –l dept.lst -rw-r- -r kumar dba Jun :45 dept.lst

29 chown sharma: dba dept.lst //ownership to sharma , group to dba
Engineered for Tomorrow kumar can reverse this action and restore the previous group ownership (to metal) because he is still owner of the file. Using chown to do both: UNIX allows the admin to use only chown to change both owner and group. The syntax is as follows: chown sharma: dba dept.lst //ownership to sharma , group to dba

30 Engineered for Tomorrow
The vi Editor UNIX provides a editor called vi editor to write C programs or shell (perl) scripts. Bill Joy created this editor in 1976. vi stands for “visual editor”. vi is a screen based editor. very powerful useful simple subset of commands full-screen

31 Engineered for Tomorrow
Starting the vi editor vi lets you create new files or edit existing files. Command to start vi: vi Opening an existing file: vi filename Creating a new file: vi filename

32 Vi Basics: vi operates in 3 modes: Command mode Input mode
Engineered for Tomorrow Vi Basics: vi operates in 3 modes: Command mode Input mode ex mode (Last Line Mode)

33 Engineered for Tomorrow
Command mode: The default mode of the editor where every key pressed is interpreted as a command that operate on text. the user should be in this mode to copy and delete text. We can’t use the Command mode to enter or replace text. pressing a key doesn’t show it on screen ,but moves the cursor to next line or replace text. e.gs: move cursor, save, delete text, quit, vi etc.

34 Input mode: is used to enter text.
Engineered for Tomorrow Input mode: is used to enter text. to go to this mode , press the key marked i , and user is ready to input the text. now start inserting lines of text and finish with ESC. pressing ESC a few times will put you back in command mode.

35 ex mode (Last Line mode):
Engineered for Tomorrow ex mode (Last Line mode): is used for file handling (saving) and substitution. Pressing a : in the Command mode invokes this mode. then enter an ex mode command (for e.g.: wq command) followed by [Enter ]. After this command is run, we are back to the default Command mode.

36 The Three Modes Input mode ex mode : [Esc] [Enter] I,I,a,A,o,O,R,r,s,S
Engineered for Tomorrow The Three Modes Input mode ex mode : [Esc] [Enter] I,I,a,A,o,O,R,r,s,S And c operator Command mode :x, :q and ZZ vi hello Shell

37 Engineered for Tomorrow
The Repeat Factor: A feature available in the vi editor of using a number to prefix any command. Use of the repeat factor as a command prefix simply repeats the command that many times. e.g.: 10f for scrolling forward by 10 pages and 30b for scrolling back 30 pages. But the commands themselves are not displayed on the screen.

38 Input mode:-Entering and Replacing Text
Engineered for Tomorrow Input mode:-Entering and Replacing Text Insert and append (I,a,I and A) Replace (r, R, s and S) Open a line (o and O) Insertion of Text ( i and a) Pressing the key i changes the mode from Command to Input. When the i key is pressed with the cursor positioned on existing text, the text on its right will be shifted further without being overwritten.

39 now the existing text will also be shifted right.
Engineered for Tomorrow Key a: To append to the right of the cursor position , press the key a followed by the input text . now the existing text will also be shifted right. Insertion of Text at Line Extremes: (I and A) I Inserts text at beginning of line A Appends text at end of line

40 Opening a new line (o and O): o opens a new line below current line
Engineered for Tomorrow Opening a new line (o and O): o opens a new line below current line O opens a new line above current line Replacing Text (r, s, R and S): r replaces a single character with another e.g.: r followed by the character that replaces the one under the cursor. s replaces a single character with many e.g.: 3s replaces three characters with new text R replaces all text on the right of the cursor position S replaces the entire line irrespective of the cursor position (Existing line disappears)

41 Engineered for Tomorrow
Input mode commands

42 Engineered for Tomorrow

43 Saving Text and Quitting -The ex mode:
Engineered for Tomorrow Saving Text and Quitting -The ex mode: Save and Exit commands of the ex mode:

44 Engineered for Tomorrow

45 Movement in the Four Directions (h, j, k and l):
Engineered for Tomorrow Navigation: Movement in the Four Directions (h, j, k and l): vi provides the keys h, j, k and l to move the cursor in the four directions. k Moves cursor up j Moves cursor down h Moves cursor left l Moves cursor right

46 Word Navigation ( b, e and w): There are 3 basic commands:
Engineered for Tomorrow Word Navigation ( b, e and w): There are 3 basic commands: b Moves back to beginning of word e Moves forward to end of word w Moves forward to beginning of word Moving to Line Extremes (0,| and $): 0(zero) or | - to move to the first character of a line $ - to move to the end of the current line

47 Scrolling ([Ctrl-f], [Ctrl-b], [Ctrl-d] and [Ctrl-u]):
Engineered for Tomorrow Scrolling ([Ctrl-f], [Ctrl-b], [Ctrl-d] and [Ctrl-u]): The two commands for scrolling a page at a time are: [Ctrl-f] Scrolls forward [Ctrl-b] Scrolls backward 10 [Ctrl-f] - to scroll 10 pages at a time [Ctrl-d] Scrolls half page forward [Ctrl-u] Scrolls half page backward

48 Absolute Movement (G):
Engineered for Tomorrow Absolute Movement (G): [Ctrl-g] -displays the current line number at any time in the vi editor. e.g..: “/etc/passwd” [Read only] line 80 of %-- 40G - Goes to line number 40. 1G - goes to the line number 1 (beginning of the file) Editing Text: The two operators used for editing the text are: d Delete y Yank (copy)

49 Deleting Text (x and dd):
Engineered for Tomorrow Deleting Text (x and dd): x - deletes a single character under the cursor 4x -deletes the current character + 3 characters from the right. dd - deletes a entire line irrespective of the cursor 6dd - deletes the current line and 5 lines below it. Copying text (y and p): vi uses the term yanking for copying text. yy - Yanks current line 10yy - Yanks current line and 9 lines below.

50 J - joins the current line and the line following it.
Engineered for Tomorrow The yanked (copied) text can be placed at the new location using the commands p and P. Joining Lines (J): J - joins the current line and the line following it. e.g.: 4J - joins following 3 lines with current one. Undoing Last Editing Instructions (u and U): u - to undo the last change made. //must use in Command mode U - reverses all changes made to the current line i.e., all modifications that have been done since the cursor moved to this line.

51 Repeating the Last Command (.):
Engineered for Tomorrow Repeating the Last Command (.): the dot (.) command is used for repeating both Input and Command Mode commands that perform editing tasks. Searching for a Pattern(/ and ?): Searching can be made in both forward and reverse directions , and can be repeated. e.g.: to search for the string printf /printf [Enter] // Searches forward The search begins forward to position the cursor on the first instance of the word.

52 ?pattern [Enter] // Searches backward
Engineered for Tomorrow The entire file is searched and if the pattern is still not found, the search wraps around to resume from the beginning of the file. If the search still fails, vi responds with the message: Pattern not found. The sequence ?pattern [Enter] // Searches backward This searches backward for the most previous instance of the pattern.

53 Repeating the Last Pattern Search (n and N):
Engineered for Tomorrow Repeating the Last Pattern Search (n and N): The n and N commands repeat a search. n - repeats the search in same direction of original search e.g.: if we used ?printf to search in the backward direction , then n also follows the same direction. N -reverses the direction pursued by n. i.e., in the above example, N will repeat the search in the forward direction.

54 Substitution - Search and Replace (:s)
Engineered for Tomorrow Substitution - Search and Replace (:s) The ex Mode is also used for substitution (:s). It lets us to replace a pattern in the file with something else. The syntax is shown below: :address/source_pattern/target_pattern/flags The source_pattern here is replaced with target_pattern in all lines specified by address. The address can be one or a pair of numbers separated by comma. For e.g. 1,$ addresses all lines in a file. The most commonly used flag is g , which carries out the substitution for all occurrences of the pattern in a line.

55 Example of a typical substitution command is shown below:
Engineered for Tomorrow Example of a typical substitution command is shown below: :1,$s/director/member/g Here, director is replaced with member globally throughout the file. if the pattern can’t be found, vi responds with the message Substitute pattern match failed. if flag g is not included, then substitution will be carried out for the first occurrence in each addressed line. :1,50s/unsigned//g //Deletes unsigned everywhere in lines 1 to 50

56 Interactive Substitution :
Engineered for Tomorrow Interactive Substitution : To selectively replace a string, add the c (confirmatory) parameter as a flag at the end: :1,$s/director/member/gc .9876| jai sharma | director |production|03/12/50/7000 ^^^^^^y 2365| barun gupta | director | production|02/12/75/7000 ^^^^^^^^^y 1006| lalith singh | director |production|14/11/68/7000 ^^^^^^^y

57 replace with msg (y/n/a/q)?
Engineered for Tomorrow The interactive substitution feature in vim editor (of Linux) is more powerful. on execution of the interactive substitution command , a prompt appears in the last line of the screen as shown below: replace with msg (y/n/a/q)? y: yes, n: no, q: aborting the substitution process, a: making it noninteractive


Download ppt "UNIT-2 Basic File Attributes Course code: 10CS44"

Similar presentations


Ads by Google