📄 ch08.htm
字号:
<DD>
<HR>
<A NAME="Heading31<FONT COLOR="#000077"><B>WARNING: </B></FONT>As soon as a
file is removed, it is gone! Always think about what you're doing before you remove
a file. You can use one of the following techniques to keep out of trouble when using
wildcards. 1. Run <TT>ls</TT> using the same file specification you use with the
<TT>rm</TT> command. For instance:<FONT COLOR="#0066FF"></FONT>
</DL>
<DL>
<DD><FONT COLOR="#0066FF"> darkstar:~$ ls *duck<BR>
dead_duck guiduck lame-duck<BR>
:~$ rm *duck</FONT>
</DL>
<PRE><FONT COLOR="#0066FF"></FONT></PRE>
<BLOCKQUOTE>
<P>In this case, you thought you wanted to remove all files that matched <TT>*duck</TT>.
To verify that this indeed was the case, you listed all the <TT>*duck</TT> files
(wildcards work the same way with all commands). The listing looked okay, so you
went ahead and removed the files. 2. Use the <TT>i</TT> (interactive) option with
<TT>rm</TT>:<FONT COLOR="#0066FF"></FONT>
</BLOCKQUOTE>
<PRE><FONT COLOR="#0066FF"> darkstar:~$ rm -i *duck
rm: remove `dead_duck'? y
rm: remove `guiduck'? n
rm: remove `lame-duck'? y
darkstar:~$
</FONT></PRE>
<BLOCKQUOTE>
<P>When you use <TT>rm -i</TT>, the command goes through the list of files to be
deleted one by one, prompting you for the okay to remove the file. If you type <TT>y</TT>
or <TT>Y</TT>, <TT>rm</TT> removes the file. If you type any other character, <TT>rm</TT>
does not remove it. The only disadvantage of using this interactive mode is that
it can be very tedious when the list of files to be removed is long.
<HR>
</BLOCKQUOTE>
<H4 ALIGN="CENTER"><A NAME="Heading32<FONT COLOR="#000077">Removing Directories</FONT></H4>
<P>The command normally used to remove (delete) directories is <TT>rmdir</TT>. The
syntax is <TT>rmdir <</TT>directory<TT>></TT>.</P>
<P>Before you can remove a directory, it must be empty (the directory can't hold
any files or subdirectories). Otherwise, you see<FONT COLOR="#0066FF"></FONT>
<PRE><FONT COLOR="#0066FF">rmdir: <directory>: Directory not empty
</FONT></PRE>
<P>This is as close to a safety feature as you will see in Linux!
<DL>
<DT></DT>
</DL>
<DL>
<DD>
<HR>
<A NAME="Heading33<FONT COLOR="#000077"><B>TIP: </B></FONT>This one might mystify
you:<FONT COLOR="#0066FF"></FONT>
</DL>
<DL>
<DD><FONT COLOR="#0066FF">darkstar:/home$ ls<BR>
fido/ root/ zippy/<BR>
darkstar:/home$ ls zippy<BR>
core kazoo stuff<BR>
darkstar:/home$ rm zippy/*<BR>
darkstar:/home/zippy$ ls zippy<BR>
darkstar:/home$ rmdir zippy<BR>
rmdir: zippy: Directory not empty<BR>
darkstar:~$</FONT>
</DL>
<PRE><FONT COLOR="#0066FF"></FONT></PRE>
<BLOCKQUOTE>
<P>The reason for the <TT>Directory not empty</TT> message is that files starting
with <TT>.</TT> usually are special system files and are usually hidden from the
user. To list files whose names start with <TT>.</TT>, you have to use <TT>ls -a</TT>.
To delete these files, use <TT>rm .*</TT>:<FONT COLOR="#0066FF"></FONT>
</BLOCKQUOTE>
<DL>
<DD><FONT COLOR="#0066FF">darkstar:/home$ ls -a zippy<BR>
./ ../ .bashrc .profile<BR>
darkstar:/home$ rm zippy/.*<BR>
rm: cannot remove `.' or `..'<BR>
darkstar:/home$ ls -a zippy<BR>
./ ../<BR>
darkstar:/home$ rmdir zippy<BR>
darkstar:/home$ ls<BR>
fido/ root/<BR>
darkstar:~$</FONT>
</DL>
<PRE><FONT COLOR="#0066FF"></FONT></PRE>
<BLOCKQUOTE>
<P>You will most often come across this situation in a system administrator role.
<HR>
</BLOCKQUOTE>
<P>Sometimes you want to remove a directory with many layers of subdirectories. Emptying
and then deleting all the subdirectories one by one would be very tedious. Linux
offers a way to remove a directory and all the files and subdirectories it contains
in one easy step. This is the <TT>r</TT> (recursive) option of the <TT>rm</TT> command.
The syntax is <TT>rm -r <</TT>directory<TT>></TT>. The directory and all its
contents are removed.
<DL>
<DT></DT>
</DL>
<DL>
<DD>
<HR>
<A NAME="Heading34<FONT COLOR="#000077"><B>WARNING: </B></FONT>You should use
<TT>rm -r</TT> only when you really have to. To paraphrase an old saying, "It's
only a shortcut until you make a mistake." For instance, if you're logged in
as root, the following command removes all files from your hard disk, and then it's
"Hello, installation procedure" time (do not type the following command!):
</DL>
<DL>
<DD><FONT COLOR="#0066FF">rm -rf /</FONT>
</DL>
<PRE><FONT COLOR="#0066FF"></FONT></PRE>
<BLOCKQUOTE>
<P>Believe it or not, people do this all too often. Don't join the club!
<HR>
</BLOCKQUOTE>
<H3 ALIGN="CENTER"><A NAME="Heading35<FONT COLOR="#000077">File Permissions
and Ownership</FONT></H3>
<P>All Linux files and directories have ownership and permissions. You can change
permissions, and sometimes ownership, to provide greater or lesser access to your
files and directories. File permissions also determine whether a file can be executed
as a command.</P>
<P>If you type <TT>ls -l</TT> or <TT>dir</TT>, you see entries that look like this:<FONT
COLOR="#0066FF"></FONT>
<PRE><FONT COLOR="#0066FF">-rw-r--r-- 1 fido users 163 Dec 7 14:31 myfile
</FONT></PRE>
<P>The <TT>-rw-r--r--</TT> represents the permissions for the file <TT>myfile</TT>.
The file's ownership includes <TT>fido</TT> as the owner and <TT>users</TT> as the
group.
<H4 ALIGN="CENTER"><A NAME="Heading36<FONT COLOR="#000077">File and Directory
Ownership</FONT></H4>
<P>When you create a file, you are that file's owner. Being the file's owner gives
you the privilege of changing the file's permissions or ownership. Of course, once
you change the ownership to another user, you can't change the ownership or permissions
anymore!</P>
<P>File owners are set up by the system during installation. Linux system files are
owned by IDs such as <TT>root</TT>, <TT>uucp</TT>, and <TT>bin</TT>. Do not change
the ownership of these files.</P>
<P>Use the <TT>chown</TT> (change ownership) command to change ownership of a file.
The syntax is <TT>chown <</TT>owner<TT>> <</TT>filename<TT>></TT>. In
the following example, you change the ownership of the file <TT>myfile</TT> to root:<FONT
COLOR="#0066FF"></FONT>
<PRE><FONT COLOR="#0066FF">darkstar:~$ ls -l myfile
-rw-r--r-- 1 fido users 114 Dec 7 14:31 myfile
darkstar:~$ chown root myfile
darkstar:~$ ls -l myfile
-rw-r--r-- 1 root users 114 Dec 7 14:31 myfile
</FONT></PRE>
<P>To make any further changes to the file <TT>myfile</TT>, or to <TT>chown</TT>
it back to <TT>fido</TT>, you must use <TT>su</TT> or log in as <TT>root</TT>.</P>
<P>Files (and users) also belong to groups. Groups are a convenient way of providing
access to files for more than one user but not to every user on the system. For instance,
users working on a special project could all belong to the group project. Files used
by the whole group would also belong to the group project, giving those users special
access. Groups normally are used in larger installations. You may never need to worry
about groups.</P>
<P>The <TT>chgrp</TT> command is used to change the group the file belongs to. It
works just like <TT>chown</TT>.
<H4 ALIGN="CENTER"><A NAME="Heading37<FONT COLOR="#000077">File Permissions</FONT></H4>
<P>Linux lets you specify read, write, and execute permissions for each of the following:
the owner, the group, and "others" (everyone else).</P>
<P>Read permission enables you to look at the file. In the case of a directory, it
lets you list the directory's contents using <TT>ls</TT>.</P>
<P>Write permission enables you to modify (or delete!) the file. In the case of a
directory, you must have write permission in order to create, move, or delete files
in that directory.</P>
<P>Execute permission enables you to execute the file by typing its name. With directories,
execute permission enables you to <TT>cd</TT> into them.</P>
<P>For a concrete example, let's look at <TT>myfile</TT> again:<FONT COLOR="#0066FF"></FONT>
<PRE><FONT COLOR="#0066FF">-rw-r--r-- 1 fido users 163 Dec 7 14:31 myfile
</FONT></PRE>
<P>The first character of the permissions is <TT>-</TT>, which indicates that it's
an ordinary file. If this were a directory, the first character would be <TT>d</TT>.
There are also some other, more exotic classes. These are beyond the scope of this
chapter.</P>
<P>The next nine characters are broken into three groups of three, giving permissions
for owner, group, and other. Each triplet gives read, write, and execute permissions,
always in that order. Permission to read is signified by an <TT>r</TT> in the first
position, permission to write is shown by a <TT>w</TT> in the second position, and
permission to execute is shown by an <TT>x</TT> in the third position. If the particular
permission is absent, its space is filled by -.</P>
<P>In the case of <TT>myfile</TT>, the owner has <TT>rw-</TT>, which means read and
write permissions. This file can't be executed by typing <TT>myfile</TT> at the Linux
prompt.</P>
<P>The group permissions are <TT>r--</TT>, which means that members of the group
"users" (by default, all ordinary users on the system) can read the file
but not change it or execute it.</P>
<P>Likewise, the permissions for all others are <TT>r--</TT>: read-only.</P>
<P>File permissions are often given as a three-digit number--for instance, 751. It's
important to understand how the numbering system works, because these numbers are
used to change a file's permissions. Also, error messages that involve permissions
use these numbers.</P>
<P>The first digit codes permissions for the owner, the second digit codes permissions
for the group, and the third digit codes permissions for other (everyone else).</P>
<P>The individual digits are encoded by summing up all the "allowed" permissions
for that particular user as follows:
<TABLE BORDER="0">
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT">read permission </TD>
<TD ALIGN="LEFT"> <P>
</TD>
<TD ALIGN="LEFT">4 </TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT">write permission </TD>
<TD ALIGN="LEFT"> <P>
</TD>
<TD ALIGN="LEFT">2 </TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT">
<BLOCKQUOTE>
<P>execute permission
</BLOCKQUOTE>
<P>
</TD>
<TD ALIGN="LEFT"> <P>
</TD>
<TD ALIGN="LEFT">
<BLOCKQUOTE>
<P>1
</BLOCKQUOTE>
<P>
</TD>
</TR>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -