Skip to main content

3.1.3 Changing properties of files

1. Changing permissions of files

To change the access permissions on a file, use the chmod command. chmod can be used with alphanumeric modespecs (involving the r, w, and x permissions for the user, group and others (u, g and o); you learned about this in Section 1.7, or chmod can be used with numeric modespecs (in octal). The general syntax for this command is:

chmod options modespecs file

You can specify several permissions in your modespecs, each separated by a comma.

To use alphanumeric modespecs:

The general format of this modespec is
who operator permission

The operators are + to add permissions, - to remove permissions amd = set exactly these permissions.
The who component is any mix of u, g and o (except a can be used instead of ugo combined).
The permission component is any mix of r, w, and x

Example:

The user harry (who is a member of the group chemistry) would like to change permissions on his file input-May05, so that no one else can read it. He does the following
 ls -l  input-May05 
this gives output
 -rw-rw-r--  1 harry   chemistry       29387 Jan 15 09:34  input-May05 
harry then types
 chmod go-r  input-May05  
and then types
 chmod g-w input-May05  
another ls -lrt gives the output
 -rw-------  1 harry   chemistry       29387 Jan 15 09:34  input-May05 
another way harry could have done this is with one command  
 chmod ug-r,g-w input-May05 


To use octal modespecs :

In this case, the values for each granted access right have to be counted together per group. Thus we get a 3-digit number, which is the symbolic value for the settings chmod has to make. The following table lists the most common combinations:

chmod 400 file To protect a file against accidental overwriting
chmod 500 directory To protect yourself from accidentally removing, renaming or moving files from this directory (similar to 'chmod u=rx,go= directory')
chmod 600 file A private file only changeable by the user who entered this command
chmod 644 file A publicly readable file that can only be changed by the issuing user
chmod 660 file Users belonging to your group can change this file, others don't have any access to it at all
chmod 700 file Protects a file against any access from other users, while the issuing user still has full access
chmod 755 directory For files that should be readable and executable by others, but only changeable by the issuing user
chmod 775 file Standard file sharing mode for a group
chmod 777 file Everybody can do everything to this file.


The umask

Under Linux, when a new file is created, it will automatically be assigned default permissions. This set of permissions is known as the umask value, and you can change these default permissions by using the command umask. This command has the form


umask n

where n is the 3 digit octal number (as shown in the table above). If you omit n, then the output will display your current umask setting.

Putting it into Practice:

Use the man pages to find out about options that can be used with chmod
How can you change the permissions of a directory such that all the files inside that directory also have these permissions? (i.e., how to set permissions recursively ?)


Back to top

2. Changing ownership of files

Somtimes when you need to share files with others, you may run into problems with permissions. Under Linux there is a command to change the owner of a file, chown, and a command to change the group ownership of a file, chgrp. The general syntax of these commands is:

chown newuser file
chgrp newgroup file

On a Linux system, each user has his or her own group, so this form can be used to make files private:

Example:

The user harry (who is a member of the group chemistry) would like to change permissions on his file input-May05, so that no one else in the chemistry group can read it. He does the following
 ls -l  input-May05 
this gives output
 -rw-rw-r--  1 harry   chemistry       29387 Jan 15 09:34  input-May05 
harry then types
 chgrp harry input-May05  
and then types
 chmod o-r input-May05  
another ls -lrt gives the output
 -rw-rw----  1 harry   harry       29387 Jan 15 09:34  input-May05 

As a regular user, you'll find that your ability to use chown and chgrp is pretty limited - principally because you won't always have the appropriate permissions to make any changes. But it comes in handy now and then.

Back to top

3. Using touch

The touch command can be used on a file to update the access and modification times of this file. The general syntax of this command is
touch options file

If the file does not exist, touch will create this file, although this can be blocked using the -c flag as an option. Read the man page on touch to find out about other options.

Back to top