-->

Previous | Table of Contents | Next

Page 355

Changing File Attributes

In addition to having a name, contents, and a file type, every file in UNIX has several other pieces of information associated with it. The most commonly encountered of these are the file's owner, a group, permissions, and timestamps. All the pieces of information stored about a file make up its attributes.

The four commands chown, chgrp, chmod, and touch enable users to change file attributes.

The chown command is used to change the owner and/or group of a file, depending on how it is invoked. The basic syntax is


chown [options] [owner] [ [:.] [group] ] [files]

where either owner or group is optional and the separator can be either a . or a :. Thus, a command of the form


chown ranga:users *

or


chown ranga.users *

changes the owner of all files in the current directory to ranga and the group of all the files in the current directory to users, provided that ranga was a valid username and users was a valid group name. To find out which usernames and group names are valid, check in the files /etc/passwd and /etc/group.

In addition to giving usernames and group names, uid (user IDs) and gid (group IDs) can be given to chown. The command


chown 500:100 foo.pl

changes the owner of foo.pl to the user with uid 500 and the group of foo.pl to the group with gid 100. When using numeric IDs, make sure that the IDs are valid, as chown only works for valid names.

If only the owner of a file (or files) needs to be changed, then the group name or group ID can be omitted. For example,


chown larry: camel.txt llama.txt

changes only the owner of the files camel.txt and llama.txt to larry.

Similarly, if only the group of a file (or files) needs to be changed, the username or uid can be omitted. Thus,


chown :100 bar.sh

Page 356

changes only the group of bar.sh to 100. If only a group change is required, the chgrp command can be used. Its basic syntax is


chgrp [options] [group] [files]

where group can be either the gid or a group name. To change the group of bar.sh to 100 with the chgrp command, the command would be


chgrp 100 bar.sh

In addition to changing the owner and group of a file, it is often necessary to change the permissions that the owner, group, and the "world" have in respect to files. This is done via the chmod command. The basic syntax is


chmod

[options][[g][u][o][a]][-/+/=][[r][w][x]][files]

where the letters g, u, o, and a (called the user part) specify whose access to the file is modified; the -, +, and = operators (called the operator part) specify how the access is changed; and the letters r, w, and x (called the permissions part) specify the permissions.

The letters in the user part have the following meanings:

u The user who owns the file
g Other users who are in the file's group
o All other users
a All users; the same as ugo

The functions of the operators are as follows:

+ Adds the specified permissions to the file
- Removes the specified permissions from a file
= Sets the permissions on a file to the specified permissions

The letters in the permissions part have the following meanings:

r Permission to read the file
w Permission to write to the file
x Permission to execute the file

Here are a few examples to illustrate the usage of chmod. In order to give the world read access to all files in a directory, use this:


chmod a+r *

Instead of a, guo could also be used. To stop anyone except the owner of .profile from writing to it, use this:


chmod go-w .profile

Page 357

To be a file miser, use this:


chmod go-rwx ~/*

When specifying the user part or the permissions part, the order in which the letters are given is irrelevant. Thus the commands


chmod guo+rx *

and


chmod uog+xr *

are equivalent.

If more than one set of permissions changes need to be applied to a file or files, a comma- separated list can be used: For example,


chmod go-w,a+x a.out

removes the groups and world write permission on a.out, and adds the execute permission for everyone.

The commands chown, chgrp, and chmod accept the following options:

-c or --changes Describes files to which a change was made
-f, --silent, or --quiet Prints no output or errors
-v or --verbose Describes the actions done to each file
-R or --recursive Recursively applies changes to a directory and its contents

The final file attribute that often needs to be changed is the timestamp. This is done via the touch command. The touch command is normally used to change the access or modification times of a file, but can also be used to create empty files. The basic syntax is


touch [options] [files]

By default touch will change the access and modification times of a file to the current time and will create files that do not exist. For example,


touch foo bar blatz

results in the files foo, bar, and blatz having their access and modification times changed to the current time. If either foo, bar, or blatz do not exist, then touch will try to create the file. The only limitation is that touch cannot change files that the current user does not own or does not have write permissions for.

Some of the options that touch understands are as follows:

-a, --time=atime, or --time=access Changes access time only
-c Doesn't create files that don't exist

Previous | Table of Contents | Next