📄 external.html
字号:
></P><P>The <ICLASS="FIRSTTERM">rm</I> command will, by itself, fail to remove filenames beginning with a dash. Why? Because <ICLASS="FIRSTTERM">rm</I> sees a dash-prefixed filename as an <ICLASS="FIRSTTERM">option</I>.</P><P> <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>rm -badname</B></TT> <TTCLASS="COMPUTEROUTPUT">rm: invalid option -- b Try `rm --help' for more information.</TT></PRE></TD></TR></TABLE> </P><P> One clever workaround is to precede the filename with a <SPANCLASS="QUOTE">" -- "</SPAN> (the <ICLASS="FIRSTTERM">end-of-options</I> flag). <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>rm -- -badname</B></TT></PRE></TD></TR></TABLE> </P><P> Another method to is to preface the filename to be removed with a <TTCLASS="FILENAME">dot-slash</TT> . <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>rm ./-badname</B></TT></PRE></TD></TR></TABLE> </P></TD></TR></TABLE></DIV><DIVCLASS="WARNING"><TABLECLASS="WARNING"WIDTH="90%"BORDER="0"><TR><TDWIDTH="25"ALIGN="CENTER"VALIGN="TOP"><IMGSRC="common/warning.png"HSPACE="5"ALT="Warning"></TD><TDALIGN="LEFT"VALIGN="TOP"><P><ANAME="RMRECURS"></A>When used with the recursive flag <TTCLASS="OPTION">-r</TT>, this command removes files all the way down the directory tree from the current directory. A careless <BCLASS="COMMAND">rm -rf *</B> can wipe out a big chunk of a directory structure.</P></TD></TR></TABLE></DIV></DD><DT><ANAME="RMDIRREF"></A><BCLASS="COMMAND">rmdir</B></DT><DD><P>Remove directory. The directory must be empty of all files -- including <SPANCLASS="QUOTE">"invisible"</SPAN> <ICLASS="FIRSTTERM">dotfiles</I> <ANAME="AEN9301"HREF="#FTN.AEN9301">[1]</A> -- for this command to succeed.</P></DD><DT><ANAME="MKDIRREF"></A><BCLASS="COMMAND">mkdir</B></DT><DD><P>Make directory, creates a new directory. For example, <TTCLASS="USERINPUT"><B>mkdir -p project/programs/December</B></TT> creates the named directory. The <TTCLASS="REPLACEABLE"><I>-p</I></TT> option automatically creates any necessary parent directories.</P></DD><DT><ANAME="CHMODREF"></A><BCLASS="COMMAND">chmod</B></DT><DD><P>Changes the attributes of an existing file or directory (see <AHREF="internal.html#EX44">Example 14-13</A>).</P><P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 chmod +x filename 2 # Makes "filename" executable for all users. 3 4 chmod u+s filename 5 # Sets "suid" bit on "filename" permissions. 6 # An ordinary user may execute "filename" with same privileges as the file's owner. 7 # (This does not apply to shell scripts.)</PRE></TD></TR></TABLE></P><P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 chmod 644 filename 2 # Makes "filename" readable/writable to owner, readable to others 3 # (octal mode). 4 5 chmod 444 filename 6 # Makes "filename" read-only for all. 7 # Modifying the file (for example, with a text editor) 8 #+ not allowed for a user who does not own the file (except for root), 9 #+ and even the file owner must force a file-save 10 #+ if she modifies the file. 11 # Same restrictions apply for deleting the file.</PRE></TD></TR></TABLE></P><P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 chmod 1777 directory-name 2 # Gives everyone read, write, and execute permission in directory, 3 #+ however also sets the "sticky bit". 4 # This means that only the owner of the directory, 5 #+ owner of the file, and, of course, root 6 #+ can delete any particular file in that directory. 7 8 chmod 111 directory-name 9 # Gives everyone execute-only permission in a directory. 10 # This means that you can execute and READ the files in that directory 11 #+ (execute permission necessarily includes read permission 12 #+ because you can't execute a file without being able to read it). 13 # But you can't list the files or search for them with the "find" command. 14 # These restrictions do not apply to root. 15 16 chmod 000 directory-name 17 # No permissions at all for that directory. 18 # Can't read, write, or execute files in it. 19 # Can't even list files in it or "cd" to it. 20 # But, you can rename (mv) the directory 21 #+ or delete it (rmdir) if it is empty. 22 # You can even symlink to files in the directory, 23 #+ but you can't read, write, or execute the symlinks. 24 # These restrictions do not apply to root.</PRE></TD></TR></TABLE></P></DD><DT><ANAME="CHATTRREF"></A><BCLASS="COMMAND">chattr</B></DT><DD><P><BCLASS="COMMAND">Ch</B>ange file <BCLASS="COMMAND">attr</B>ibutes. This is analogous to <BCLASS="COMMAND">chmod</B> above, but with different options and a different invocation syntax, and it works only on an <ICLASS="FIRSTTERM">ext2</I> filesystem.</P><P>One particularly interesting <BCLASS="COMMAND">chattr</B> option is <TTCLASS="OPTION">i</TT>. A <BCLASS="COMMAND">chattr +i <TTCLASS="FILENAME">filename</TT></B> marks the file as immutable. The file cannot be modified, linked to, or deleted, <SPANCLASS="emphasis"><ICLASS="EMPHASIS">not even by root</I></SPAN>. This file attribute can be set or removed only by <ICLASS="FIRSTTERM">root</I>. In a similar fashion, the <TTCLASS="OPTION">a</TT> option marks the file as append only.</P><P> <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="PROMPT">root# </TT><TTCLASS="USERINPUT"><B>chattr +i file1.txt</B></TT> <TTCLASS="PROMPT">root# </TT><TTCLASS="USERINPUT"><B>rm file1.txt</B></TT> <TTCLASS="COMPUTEROUTPUT">rm: remove write-protected regular file `file1.txt'? y rm: cannot remove `file1.txt': Operation not permitted</TT> </PRE></TD></TR></TABLE> </P><P>If a file has the <TTCLASS="OPTION">s</TT> (secure) attribute set, then when it is deleted its block is zeroed out on the disk.</P><P>If a file has the <TTCLASS="OPTION">u</TT> (undelete) attribute set, then when it is deleted, its contents can still be retrieved (undeleted).</P><P>If a file has the <TTCLASS="OPTION">c</TT> (compress) attribute set, then it will automatically be compressed on writes to disk, and uncompressed on reads.</P><DIVCLASS="NOTE"><TABLECLASS="NOTE"WIDTH="90%"BORDER="0"><TR><TDWIDTH="25"ALIGN="CENTER"VALIGN="TOP"><IMGSRC="common/note.png"HSPACE="5"ALT="Note"></TD><TDALIGN="LEFT"VALIGN="TOP"><P>The file attributes set with <BCLASS="COMMAND">chattr</B> do not show in a file listing (<BCLASS="COMMAND">ls -l</B>).</P></TD></TR></TABLE></DIV></DD><DT><ANAME="LINKREF"></A><BCLASS="COMMAND">ln</B></DT><DD><P>Creates links to pre-existings files. A <SPANCLASS="QUOTE">"link"</SPAN> is a reference to a file, an alternate name for it. The <BCLASS="COMMAND">ln</B> command permits referencing the linked file by more than one name and is a superior alternative to aliasing (see <AHREF="othertypesv.html#EX18">Example 4-6</A>).</P><P>The <BCLASS="COMMAND">ln</B> creates only a reference, a pointer to the file only a few bytes in size.</P><P><ANAME="SYMLINKREF"></A></P><P>The <BCLASS="COMMAND">ln</B> command is most often used with the <TTCLASS="OPTION">-s</TT>, symbolic or <SPANCLASS="QUOTE">"soft"</SPAN> link flag. Advantages of using the <TTCLASS="OPTION">-s</TT> flag are that it permits linking across file systems or to directories.</P><P>The syntax of the command is a bit tricky. For example: <TTCLASS="USERINPUT"><B>ln -s oldfile newfile</B></TT> links the previously existing <TTCLASS="FILENAME">oldfile</TT> to the newly created link, <TTCLASS="FILENAME">newfile</TT>.</P><DIVCLASS="CAUTION"><TABLECLASS="CAUTION"WIDTH="90%"BORDER="0"><TR><TDWIDTH="25"ALIGN="CENTER"VALIGN="TOP"><IMGSRC="common/caution.png"HSPACE="5"ALT="Caution"></TD><TDALIGN="LEFT"VALIGN="TOP"><P>If a file named <TTCLASS="FILENAME">newfile</TT> has previously existed, an error message will result.</P></TD></TR></TABLE></DIV><TABLECLASS="SIDEBAR"BORDER="1"CELLPADDING="5"><TR><TD><DIVCLASS="SIDEBAR"><ANAME="AEN9407"></A><P><B>Which type of link to use?</B></P><P>As John Macdonald explains it:</P><P>Both of these [types of links] provide a certain measure of dual reference -- if you edit the contents of the file using any name, your changes will affect both the original name and either a hard or soft new name. The differences between them occurs when you work at a higher level. The advantage of a hard link is that the new name is totally independent of the old name -- if you remove or rename the old name, that does not affect the hard link, which continues to point to the data while it would leave a soft link hanging pointing to the old name which is no longer there. The advantage of a soft link is that it can refer to a different file system (since it is just a reference to a file name, not to actual data). And, unlike a hard link, a symbolic link can refer to a directory.</P></DIV></TD></TR></TABLE><P><ANAME="LINKMINVOK"></A></P><P>Links give the ability to invoke a script (or any other type of executable) with multiple names, and having that script behave according to how it was invoked.</P><DIVCLASS="EXAMPLE"><HR><ANAME="HELLOL"></A><P><B>Example 15-2. Hello or Good-bye</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 #!/bin/bash 2 # hello.sh: Saying "hello" or "goodbye" 3 #+ depending on how script is invoked. 4 5 # Make a link in current working directory ($PWD) to this script: 6 # ln -s hello.sh goodbye 7 # Now, try invoking this script both ways: 8 # ./hello.sh 9 # ./goodbye 10 11 12 HELLO_CALL=65 13 GOODBYE_CALL=66 14 15 if [ $0 = "./goodbye" ] 16 then 17 echo "Good-bye!" 18 # Some other goodbye-type commands, as appropriate. 19 exit $GOODBYE_CALL 20 fi 21 22 echo "Hello!" 23 # Some other hello-type commands, as appropriate. 24 exit $HELLO_CALL</PRE></TD></TR></TABLE><HR></DIV></DD><DT><ANAME="MANREF"></A><BCLASS="COMMAND">man</B>, <ANAME="INFOREF"></A><BCLASS="COMMAND">info</B></DT><DD><P>These commands access the manual and information pages on system commands and installed utilities. When available, the <ICLASS="FIRSTTERM">info</I> pages usually contain more detailed descriptions than do the <ICLASS="FIRSTTERM">man</I> pages.</P></DD></DL></DIV></DIV></DIV><H3CLASS="FOOTNOTES">Notes</H3><TABLEBORDER="0"CLASS="FOOTNOTES"WIDTH="100%"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="5%"><ANAME="FTN.AEN9301"HREF="external.html#AEN9301">[1]</A></TD><TDALIGN="LEFT"VALIGN="TOP"WIDTH="95%"><P><ANAME="DOTFILESREF"></A></P><P><ICLASS="FIRSTTERM">Dotfiles</I> are files whose names begin with a <ICLASS="FIRSTTERM">dot</I>, such as <TTCLASS="FILENAME">~/.Xdefaults</TT>. Such filenames do not appear in a normal <BCLASS="COMMAND">ls</B> listing (although an <BCLASS="COMMAND">ls -a</B> will show them), and they cannot be deleted by an accidental <BCLASS="COMMAND">rm -rf *</B>. Dotfiles are generally used as setup and configuration files in a user's home directory.</P></TD></TR></TABLE><DIVCLASS="NAVFOOTER"><HRALIGN="LEFT"WIDTH="100%"><TABLESUMMARY="Footer navigation table"WIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top"><AHREF="internal.html"ACCESSKEY="P">Prev</A></TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="index.html"ACCESSKEY="H">Home</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top"><AHREF="moreadv.html"ACCESSKEY="N">Next</A></TD></TR><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top">Internal Commands and Builtins</TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="part4.html"ACCESSKEY="U">Up</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top">Complex Commands</TD></TR></TABLE></DIV></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -