rhl08.htm

来自「linux的初学电子书」· HTM 代码 · 共 2,355 行 · 第 1/5 页

HTM
2,355 行
字号
 rm: remove 'lame-duck'? y

 darkstar:~$</FONT></PRE></UL></UL>

<BLOCKQUOTE>

<BLOCKQUOTE>

<HR ALIGN=CENTER>

<BR>

<NOTE>When you use rm -i, the command goes through the list of files to be deleted one by one, prompting you for the OK to remove the file. If you type y or Y, rm removes the file. If you type any other character, rm 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.</NOTE>

<BR>

<HR ALIGN=CENTER>

</BLOCKQUOTE></BLOCKQUOTE>

<BR>

<A NAME="E69E62"></A>

<H4 ALIGN=CENTER>

<CENTER>

<FONT SIZE=4 COLOR="#FF0000"><B>Removing Directories</B></FONT></CENTER></H4>

<BR>

<P>The command normally used to remove (delete) directories is rmdir. The syntax is rmdir &lt;directory&gt;.

<BR>

<P>Before you can remove a directory, it must be empty (the directory can't hold any files or subdirectories). Otherwise, you see

<BR>

<BR>

<PRE>

<FONT COLOR="#000080">rmdir: &lt;directory&gt;: Directory not empty</FONT></PRE>

<P>This is as close to a safety feature as you will see in Linux!

<BR>

<BLOCKQUOTE>

<BLOCKQUOTE>

<HR ALIGN=CENTER>

<BR>

<NOTE>This one might mystify you:

<BR>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:~$

<BR>The reason for the Directory not empty message is that files starting with . usually are special system files and are usually hidden from the user. To list files whose names start with ., you have to use ls -a. To delete these files, use rm .*:

<BR>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:~$

<BR>You will most often come across this situation in a system administrator role.</NOTE>

<BR>

<HR ALIGN=CENTER>

</BLOCKQUOTE></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 r (recursive) option of the rm command. The syntax is rm -r &lt;directory&gt;. The directory and all its contents are removed.

<BR>

<BLOCKQUOTE>

<BLOCKQUOTE>

<HR ALIGN=CENTER>

<BR>

<NOTE>You should use rm -r only when you really have to. To paraphrase an old saying, &quot;It's only a shortcut until you make a mistake.&quot; For instance, if you're logged in as root, the following command removes all files from your hard disk, and 
then it's &quot;Hello, installation procedure&quot; time (do not type the following command!):

<BR>rm -rf /

<BR>Believe it or not, people do this all too often. Don't join the club!</NOTE>

<BR>

<HR ALIGN=CENTER>

</BLOCKQUOTE></BLOCKQUOTE>

<BR>

<A NAME="E68E57"></A>

<H3 ALIGN=CENTER>

<CENTER>

<FONT SIZE=5 COLOR="#FF0000"><B>File Permissions and Ownership</B></FONT></CENTER></H3>

<BR>

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

<BR>

<P>If you type ls -l or dir, you see entries that look like this:

<BR>

<BR>

<PRE>

<FONT COLOR="#000080">-rw-r&#151;r&#151; 1 fido users 163 Dec 7 14:31 myfile</FONT></PRE>

<P>The -rw-r&#151;r&#151; represents the permissions for the file myfile. The file's ownership includes fido as the owner and users as the group.

<BR>

<BR>

<A NAME="E69E63"></A>

<H4 ALIGN=CENTER>

<CENTER>

<FONT SIZE=4 COLOR="#FF0000"><B>File and Directory Ownership</B></FONT></CENTER></H4>

<BR>

<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!

<BR>

<P>File owners are set up by the system during installation. Linux system files are owned by IDs such as root, uucp, and bin. Do not change the ownership of these files.

<BR>

<P>Use the chown (change ownership) command to change ownership of a file. The syntax is chown &lt;owner&gt; &lt;filename&gt;. In the following example, you change the ownership of the file myfile to root:

<BR>

<PRE>

<FONT COLOR="#000080">darkstar:~$ ls -l myfile

-rw-r&#151;r&#151; 1 fido users 114 Dec 7 14:31 myfile

darkstar:~$ chown root myfile

darkstar:~$ ls -l myfile

-rw-r&#151;r&#151; 1 root users 114 Dec 7 14:31 myfile</FONT></PRE>

<P>To make any further changes to the file myfile, or to chown it back to fido, you must use su or log in as root.

<BR>

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

<BR>

<P>The chgrp command is used to change the group the file belongs to. It works just like chown.

<BR>

<BR>

<A NAME="E69E64"></A>

<H4 ALIGN=CENTER>

<CENTER>

<FONT SIZE=4 COLOR="#FF0000"><B>File Permissions</B></FONT></CENTER></H4>

<BR>

<P>Linux lets you specify read, write, and execute permissions for each of the following: the owner, the group, and &quot;others&quot; (everyone else).

<BR>

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

<BR>

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

<BR>

<P>execute permission enables you to execute the file by typing its name. With directories, execute permission enables you to cd into them.

<BR>

<P>For a concrete example, let's look at myfile again:

<BR>

<BR>

<PRE>

<FONT COLOR="#000080">-rw-r&#151;r&#151; 1 fido users 163 Dec 7 14:31 myfile</FONT></PRE>

<P>The first character of the permissions is -, which indicates that it's an ordinary file. If this were a directory, the first character would be d. There are also some other, more exotic classes. These are beyond the scope of this chapter.

<BR>

<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 r in the first position, 
permission to write is shown by a w in the second position, and permission to execute is shown by an x in the third position. If the particular permission is absent, its space is filled by -.

<BR>

<P>In the case of myfile, the owner has rw-, which means read and write permissions. This file can't be executed by typing myfile at the Linux prompt.

<BR>

<P>The group permissions are r&#151;, which means that members of the group &quot;users&quot; (by default, all ordinary users on the system) can read the file but not change it or execute it.

<BR>

<P>Likewise, the permissions for all others are r&#151;: read-only.

<BR>

<P>File permissions are often given as a three-digit number&#151;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.

<BR>

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

<BR>

<P>The individual digits are encoded by summing up all the &quot;allowed&quot; permissions for that particular user as follows:

<BR>



<TABLE  BORDERCOLOR=#000040 BORDER=1 CELLSPACING=2 WIDTH="100%" CELLPADDING=2 >

<TR>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

read permission

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

4</FONT>

<TR>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

write permission

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

2</FONT>

<TR>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

execute permission

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

1</FONT>

</TABLE><P>Therefore, a file permission of 751 means that the owner has read, write, and execute permission (4+2+1=7), the group has read and execute permission (4+1=5), and others have execute permission (1).

<BR>

<P>If you play with the numbers, you quickly see that the permission digits can range between 0 and 7, and that for each digit in that range there's only one possible combination of read, write, and execute permissions.

<BR>

<BLOCKQUOTE>

<BLOCKQUOTE>

<HR ALIGN=CENTER>

<BR>

<NOTE>If you're familiar with the binary system, think of rwx as a three-digit binary number. If permission is allowed, the corresponding digit is 1. If permission is denied, the digit is 0. So r-x would be the binary number 101, which is 4+0+1, or 5. 
&#151;x would be 001, which is 0+0+1, which is 1, and so on.</NOTE>

<BR>

<HR ALIGN=CENTER>

</BLOCKQUOTE></BLOCKQUOTE>

<P>The following combinations are possible:

<BR>

<UL>

<UL>

<P>0 or &#151;-: No permissions at all

<BR>

</UL></UL>

<UL>

<UL>

<P>4 or r&#151;: read-only

<BR>

</UL></UL>

<UL>

<UL>

<P>2 or -w-: write-only (rare)

<BR>

</UL></UL>

<UL>

<UL>

<P>1 or &#151;x: execute

<BR>

</UL></UL>

<UL>

<UL>

<P>6 or rw-: read and write

<BR>

</UL></UL>

<UL>

<UL>

<P>5 or r-x: read and execute

<BR>

</UL></UL>

<UL>

<UL>

<P>3 or -wx: write and execute (rare)

<BR>

</UL></UL>

<UL>

<UL>

<P>7 or rwx: read, write, and execute

<BR>

</UL></UL>

<BLOCKQUOTE>

<BLOCKQUOTE>

<HR ALIGN=CENTER>

<BR>

<NOTE>Anyone who has permission to read a file can then copy that file. When a file is copied, the copy is owned by the person doing the copying. He or she can then change ownership and permissions, edit the file, and so on.</NOTE>

<BR>

<HR ALIGN=CENTER>

</BLOCKQUOTE></BLOCKQUOTE>

<BLOCKQUOTE>

<BLOCKQUOTE>

<HR ALIGN=CENTER>

<BR>

<NOTE>Removing write permission from a file doesn't prevent the file from being deleted! It does prevent it from being deleted accidentally, since Linux asks you whether you want to override the file permissions. You have to answer y, or the file will not 
be deleted.</NOTE>

<BR>

<HR ALIGN=CENTER>

</BLOCKQUOTE></BLOCKQUOTE>

<BR>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?