0355-0357.html
来自「linux-unix130.linux.and.unix.ebooks130 l」· HTML 代码 · 共 535 行
HTML
535 行
<HTML>
<HEAD>
<TITLE>Developer.com - Online Reference Library - 0672311739:RED HAT LINUX 2ND EDITION:GNU Project Utilities</TITLE>
<META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW">
<SCRIPT>
<!--
function displayWindow(url, width, height) {
var Win = window.open(url,"displayWindow",'width=' + width +
',height=' + height + ',resizable=1,scrollbars=yes');
}
//-->
</SCRIPT>
</HEAD>
-->
<!-- ISBN=0672311739 //-->
<!-- TITLE=RED HAT LINUX 2ND EDITION //-->
<!-- AUTHOR=DAVID PITTS ET AL //-->
<!-- PUBLISHER=MACMILLAN //-->
<!-- IMPRINT=SAMS PUBLISHING //-->
<!-- PUBLICATION DATE=1998 //-->
<!-- CHAPTER=17 //-->
<!-- PAGES=0351-0372 //-->
<!-- UNASSIGNED1 //-->
<!-- UNASSIGNED2 //-->
<P><CENTER>
<a href="0351-0354.html">Previous</A> | <a href="../ewtoc.html">Table of Contents</A> | <a href="0358-0361.html">Next</A>
</CENTER></P>
<A NAME="PAGENUM-355"><P>Page 355</P></A>
<H4><A NAME="ch17_ 5">
Changing File Attributes
</A></H4>
<P>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.
</P>
<P>The four commands chown, chgrp, chmod, and
touch enable users to change file attributes.
</P>
<P>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
</P>
<!-- CODE SNIP //-->
<PRE>
chown [options] [owner] [ [:.] [group] ] [files]
</PRE>
<!-- END CODE SNIP //-->
<P>where either owner or group is optional and the separator can be either a
. or a :. Thus, a command of the form
</P>
<!-- CODE SNIP //-->
<PRE>
chown ranga:users *
</PRE>
<!-- END CODE SNIP //-->
<P>or
</P>
<!-- CODE SNIP //-->
<PRE>
chown ranga.users *
</PRE>
<!-- END CODE SNIP //-->
<P>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.
</P>
<P>In addition to giving usernames and group names,
uid (user IDs) and gid (group IDs) can be given to
chown. The command
</P>
<!-- CODE SNIP //-->
<PRE>
chown 500:100 foo.pl
</PRE>
<!-- END CODE SNIP //-->
<P>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.
</P>
<P>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,
</P>
<!-- CODE SNIP //-->
<PRE>
chown larry: camel.txt llama.txt
</PRE>
<!-- END CODE SNIP //-->
<P>changes only the owner of the files camel.txt and
llama.txt to larry.
</P>
<P>Similarly, if only the group of a file (or files) needs to be changed, the username or
uid can be omitted. Thus,
</P>
<!-- CODE SNIP //-->
<PRE>
chown :100 bar.sh
</PRE>
<!-- END CODE SNIP //-->
<A NAME="PAGENUM-356"><P>Page 356</P></A>
<P>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
</P>
<!-- CODE SNIP //-->
<PRE>
chgrp [options] [group] [files]
</PRE>
<!-- END CODE SNIP //-->
<P>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
</P>
<!-- CODE SNIP //-->
<PRE>
chgrp 100 bar.sh
</PRE>
<!-- END CODE SNIP //-->
<P>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
</P>
<!-- CODE SNIP //-->
<PRE>
chmod
[options][[g][u][o][a]][-/+/=][[r][w][x]][files]
</PRE>
<!-- END CODE SNIP //-->
<P>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.
</P>
<P>The letters in the user part have the following meanings:
</P>
<TABLE WIDTH="360">
<TR><TD>
u
</TD><TD>
The user who owns the file
</TD></TR>
<TR><TD>
g
</TD><TD>
Other users who are in the file's group
</TD></TR>
<TR><TD>
o
</TD><TD>
All other users
</TD></TR>
<TR><TD>
a
</TD><TD>
All users; the same as ugo
</TD></TR>
</TABLE>
<P>The functions of the operators are as follows:
</P>
<TABLE WIDTH="360">
<TR><TD>
+
</TD><TD>
Adds the specified permissions to the file
</TD></TR>
<TR><TD>
-
</TD><TD>
Removes the specified permissions from a file
</TD></TR>
<TR><TD>
=
</TD><TD>
Sets the permissions on a file to the specified permissions
</TD></TR>
</TABLE>
<P>The letters in the permissions part have the following meanings:
</P>
<TABLE WIDTH="360">
<TR><TD>
r
</TD><TD>
Permission to read the file
</TD></TR>
<TR><TD>
w
</TD><TD>
Permission to write to the file
</TD></TR>
<TR><TD>
x
</TD><TD>
Permission to execute the file
</TD></TR>
</TABLE>
<P>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:
</P>
<!-- CODE SNIP //-->
<PRE>
chmod a+r *
</PRE>
<!-- END CODE SNIP //-->
<P>Instead of a, guo could also be used. To stop anyone except the owner of
.profile from writing to it, use this:
</P>
<!-- CODE SNIP //-->
<PRE>
chmod go-w .profile
</PRE>
<!-- END CODE SNIP //-->
<A NAME="PAGENUM-357"><P>Page 357</P></A>
<P>To be a file miser, use this:
</P>
<!-- CODE SNIP //-->
<PRE>
chmod go-rwx ~/*
</PRE>
<!-- END CODE SNIP //-->
<P>When specifying the user part or the permissions part, the order in which the letters are
given is irrelevant. Thus the commands
</P>
<!-- CODE SNIP //-->
<PRE>
chmod guo+rx *
</PRE>
<!-- END CODE SNIP //-->
<P>and
</P>
<!-- CODE SNIP //-->
<PRE>
chmod uog+xr *
</PRE>
<!-- END CODE SNIP //-->
<P>are equivalent.
</P>
<P>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,
</P>
<!-- CODE SNIP //-->
<PRE>
chmod go-w,a+x a.out
</PRE>
<!-- END CODE SNIP //-->
<P>removes the groups and world write permission on
a.out, and adds the execute permission for everyone.
</P>
<P>The commands chown, chgrp, and chmod accept the following options:
</P>
<TABLE WIDTH="360">
<TR><TD>
-c or --changes
</TD><TD>
Describes files to which a change was made
</TD></TR>
<TR><TD>
-f, --silent, or --quiet
</TD><TD>
Prints no output or errors
</TD></TR>
<TR><TD>
-v or --verbose
</TD><TD>
Describes the actions done to each file
</TD></TR>
<TR><TD>
-R or --recursive
</TD><TD>
Recursively applies changes to a directory and
its contents
</TD></TR>
</TABLE>
<P>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
</P>
<!-- CODE SNIP //-->
<PRE>
touch [options] [files]
</PRE>
<!-- END CODE SNIP //-->
<P>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,
</P>
<!-- CODE SNIP //-->
<PRE>
touch foo bar blatz
</PRE>
<!-- END CODE SNIP //-->
<P>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.
</P>
<P>Some of the options that touch understands are as follows:
</P>
<TABLE WIDTH="360">
<TR><TD>
-a, --time=atime, <B>or</B> --time=access
</TD><TD>
Changes access time only
</TD></TR>
<TR><TD>
-c
</TD><TD>
Doesn't create files that don't exist
</TD></TR>
</TABLE>
<P><CENTER>
<a href="0351-0354.html">Previous</A> | <a href="../ewtoc.html">Table of Contents</A> | <a href="0358-0361.html">Next</A>
</CENTER></P>
</td>
</tr>
</table>
<!-- begin footer information -->
</body></html>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?