File Attribute and Permissions, File Commands, File Permission, Changing File Permission , Command Line Arguments , Directory permission , Exit and Exit Status of Command

File Attribute and Permissions, File Commands, File Permission, Changing File Permission , Command Line Arguments , Directory permission ,  Exit and Exit Status of Command

 


FILE ATTRIBUTES AND PERMISSIONS  :

ls command with options :

 ls –l: listing file attributes 

  •  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:
File type and Permissions
 Links
 Ownership
 Group ownership 
 File size
 Last Modification 
date and time

Example


 1. File type and permissions: the first column shows the type and permissions associated with each file. The first character in the column is mostly a -, which indicates that the file is an ordinary one. Directories are indicated by d at the same position. 

 2. 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. 

 3.  Ownership: the user who creates the files automatically becomes the owner of the file. The third column shows kumar as the owner of all these files. The owner has full authority to tamper with a file’s content and permissions. 

4.  Group ownership: when opening a user account, the system administrator also assigns the user to some group. The fourth column represents the group owner of the file. 

 5. File size: the fifth column shows the size of the file in bytes, i.e. the amount of data it contains. It is only the character count of the file and not a measure of the disk space it occupies. 

 6. Last modification time: the sixth, seventh and eighth columns indicate the last modification time of the file. A file is said to be modified only if its content have changed in any way. If the file is less than a year old since its last modification date, the year won’t be displayed. 

 7. Filename: the last column displays the filenames arranged in ASCII collating sequence.


 ls –d: Listing Directory Attributes :

 This command will not list all subdirectories in the current directory. 

 For example, 



  • 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 Permission:

  • 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 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. 

Changing File Permission: 


  •  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. 

 The command can be used in two ways: 

  •  In a relative manner by specifying the changes to the current permissions. 
  •  In an absolute manner by specifying the final permissions.

Relative Permission: 

  •  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)


Category : u – user g – group o – others a - all (ugo) 

 operations : + assign - remove = absolute 

 permissions: r – read w – write x – execute 


 Examples: 

 Initially, let the permission be: 

-rw-r—r-- 1 kumar metal 1906 sep 23:38 xstart


  $ chmod u+x xstart  

-rwxr—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 

-rwxr-xr-x 1 kumar metal 1906 sep 23:38 xstart

 

  • chmod accepts multiple file names in command line 

 

  $ chmod u+x note note1 note3  

 

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 Permission: 

  •  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) 


  • The following table represent the octal value and its significance.


  • We have three categories and three permissions for each category, so three octal digits can describe a file‘s permissions completely. 
  •  The most significant digit represents user and the least one represents others. chmod can use this three-digit string as the expression.
Example: 
 $chmod 666 xstart 
 $chmod 644 xstart 
 $chmod 761 xstart
 
  • 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 cannot change other user‘s file‘s permissions. 
  •  But the system administrator can do anything.

Recursively Changing File Permission (-R):

We can use chmod Recursively. 
  $ chmod -R a+x shell_scripts  


  •  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) 

A directory must never be writable by group and others. 

 Example: 

 $ mkdir c_progs 
 $ls –ld c_progs 
 drwxr-xr-x 2 kumar metal 512 may 9 09:57 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.

Command Line Arguments :

  •  Shell scripts also accept arguments from the command line. Therefore e they can be run non interactively and be used with redirection and pipelines. 
  •  The arguments are assigned to special shell variables. Represented by $1, $2, etc; similar to C command arguments argv[0], argv[1], etc. 
  •  The following table lists the different shell parameters.


 exit and Exit Status of Command:

To terminate a program exit is used. Nonzero value indicates an error condition.

Example 1:

$ cat foo 
Cat: can’t open foo

  •  Returns nonzero exit status. The shell variable $? Stores this status.
Example 2: 

grep director emp.lst > /dev/null:echo $? 
 0 

  •  Exit status is used to devise program logic that branches into different paths depending on success or failure of a command.



You Can Learn Here THANK YOU .........for visiting for more info  CONTECT


Previous Post Next Post

Contact Form