rhl08.htm

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

HTM
2,355 行
字号

<P>Neat! You can also add to the end of the file by using &gt;&gt;. Whenever you use &gt;&gt;, whether with cat or any other command, the output is always appended to the specified file. (Note that the ^D character does not appear on-screen. I show it in 
the examples for clarity.)

<BR>

<PRE>

<FONT COLOR="#000080">darkstar:~$ cat &gt;&gt; newfile

Some more lines

^D

darkstar:~$ cat newfile

Hello world

Here's some text

Some more lines

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

<P>To discover what cat actually stands for, let's first create another file.

<BR>

<PRE>

<FONT COLOR="#000080">darkstar:~$ cat &gt; anotherfile

Different text

^D

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

<P>Now, try this:

<BR>

<PRE>

<FONT COLOR="#000080">darkstar:~$ cat newfile anotherfile&gt; thirdfile

darkstar:~$ cat thirdfile

Hello world

Here's some text

Some more lines

Different text

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

<P>cat stands for concatenate; cat takes all the specified inputs and regurgitates them in a single lump. This by itself would not be very interesting, but combine it with the forms of input and output redirection available in Linux and you have a powerful 
and useful tool.

<BR>

<P>Sometimes you want to change just one line of a file, or perhaps you are creating a large and complicated file. For this you should use one of the editing programs available in Linux. They are discussed in <A HREF="rhl16.htm" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/rhl16.htm">Chapter 16</A>, &quot;Text 
Editors.&quot;

<BR>

<BR>

<A NAME="E69E58"></A>

<H4 ALIGN=CENTER>

<CENTER>

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

<BR>

<P>To create a new directory, use the mkdir command. The syntax is mkdir &lt;name&gt;, where &lt;name&gt; is replaced by whatever you want the directory to be called. This creates a subdirectory with the specified name in your current directory:

<BR>

<PRE>

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

anotherfile newfile thirdfile

darkstar:~$ mkdir newdir

darkstar:~$ ls

anotherfile newdir/ newfile thirdfile</FONT></PRE>

<BLOCKQUOTE>

<BLOCKQUOTE>

<HR ALIGN=CENTER>

<BR>

<NOTE>The mkdir command is already familiar to you if you have used MS-DOS systems. In MS-DOS, you can abbreviate mkdir as md. You might think that md would work in Linux, because, after all, most of the commands we've seen have extremely concise names. 
However, Linux doesn't recognize md; it insists on the full mkdir.

<BR>If you frequently switch between Linux and MS-DOS, you might want to use mkdir for both systems. However, be warned that you might start typing other Linux commands in MS-DOS&#151;for example, typing ls instead of dir!</NOTE>

<BR>

<HR ALIGN=CENTER>

</BLOCKQUOTE></BLOCKQUOTE>

<BR>

<A NAME="E69E59"></A>

<H4 ALIGN=CENTER>

<CENTER>

<FONT SIZE=4 COLOR="#FF0000"><B>Moving and Copying Files</B></FONT></CENTER></H4>

<BR>

<P>You often need to move or copy files. The mv command moves files, and the cp command copies files. The syntax for the two commands is similar:

<BR>

<PRE>

<FONT COLOR="#000080">mv &lt;source&gt; &lt;destination&gt;

cp &lt;source&gt; &lt;destination&gt;</FONT></PRE>

<P>As you can see, mv and cp are very simple commands. Here's an example:

<BR>

<PRE>

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

anotherfile newdir/ newfile thirdfile

darkstar:~$ mv anotherfile movedfile

darkstar:~$ ls

movedfile newdir/ newfile thirdfile

darkstar:~$ cp thirdfile xyz

darkstar:~$ ls

anotherfile newdir/ newfile thirdfile xyz</FONT></PRE>

<P>You can use cat (or more or less) at any time to verify that anotherfile became movedfile, and that the contents of file xyz are identical to the contents of thirdfile.

<BR>

<P>It can get more confusing if you're moving or copying files from one directory to another. This is because a file's real name includes its absolute path&#151;for instance, /home/fido/newfile. However, Linux lets you leave off parts of the file's name, 
because it's more convenient to refer to newfile rather than /home/fido/newfile.

<BR>

<P>For instance, suppose you want to move newfile into the newdir subdirectory. If you want the file to keep the same name, you type

<BR>

<BR>

<PRE>

<FONT COLOR="#000080">darkstar:~$ mv newfile newdir/newfile</FONT></PRE>

<P>However, it's much more common to type

<BR>

<BR>

<PRE>

<FONT COLOR="#000080">darkstar:~$ mv newfile newdir</FONT></PRE>

<P>Here, because you have typed a directory name for the destination, Linux assumes that you want the file to be placed in the specified directory.

<BR>

<P>You could also use cd to change to the directory you want to move the file to:

<BR>

<PRE>

<FONT COLOR="#000080">darkstar:~$ cd newdir

darkstar:~newdir$ copy ../newfile .</FONT></PRE>

<P>This example is a bit less intuitive than the first two! You specify that the source is ../newfile, which means &quot;the file newfile in the current directory's parent directory.&quot; The destination you simply specify as &quot;.&quot;, which is short 
for &quot;the current directory.&quot; In other words, you're telling mv to &quot;go up one level, grab newfile, and move it to right here.&quot; Because this is less intuitive, you might find yourself automatically pushing a file from your current 
directory to another directory rather than pulling a file from another directory into your current directory.

<BR>

<P>You can also change the name of the file while moving or copying it to another directory. The following is just one possible way:

<BR>

<BR>

<PRE>

<FONT COLOR="#000080">darkstar:~$ cp newfile newdir/anothername</FONT></PRE>

<P>This would create a copy of newfile in the directory newdir and name the copied file anothername.

<BR>

<BLOCKQUOTE>

<BLOCKQUOTE>

<HR ALIGN=CENTER>

<BR>

<NOTE>When moving or copying files between directories, you should always double-check that the file's destination directory exists and verify the directory's name. Otherwise, the results of your command can be unexpected, as the following two examples 
show.

<BR>If in the example just shown you mistyped newdir&#151;for instance, as mv newfile mewdir&#151;you would wind up with a file called mewdir in your current directory and no file newfile in the newdir subdirectory!

<BR>Another way you would get an unexpected result would be to type cp newfile newdir if you didn't realize that the directory newdir existed. In this case, you would be expecting to create an identical file called newdir in your current directory. What 
you would actually do is create a copy of newfile, called newfile, in the subdirectory newdir.</NOTE>

<BR>

<HR ALIGN=CENTER>

</BLOCKQUOTE></BLOCKQUOTE>

<P>The mv command is much more efficient than the cp command. When you use mv, the file's contents are not moved at all; rather, Linux makes a note that the file is to be found elsewhere within the file system's structure of directories.

<BR>

<P>When you use cp, you are actually making a second physical copy of your file and placing it on your disk. This can be slower (although for small files, you won't notice any difference), and it causes a bit more wear and tear on your computer. Don't make 
copies of files when all you really want to do is move them!

<BR>

<BR>

<A NAME="E69E60"></A>

<H4 ALIGN=CENTER>

<CENTER>

<FONT SIZE=4 COLOR="#FF0000"><B>Moving and Copying with Wildcards</B></FONT></CENTER></H4>

<BR>

<P>If you have 20 files in a directory, and you want to copy them to another directory, it would be very tedious to use the cp command on each one. Fortunately, you can use the wildcards * and ? to copy more than one file at a time.

<BR>

<P>If you want to move or copy all files in a directory, use the wildcard *:

<BR>

<BR>

<PRE>

<FONT COLOR="#000080">darkstar:~$ cp * /tmp</FONT></PRE>

<P>This command copies every file in your current directory to the directory /tmp.

<BR>

<P>You can use *, along with other characters, to match only certain files. For instance, suppose you have a directory that contains the files book1, book_idea, book-chapter-1, and poem.book. To copy just the first three files, you could type cp book* 
/tmp. When you type book*, you are asking Linux to match all files whose names start with book. In this case, poem.book does not start with book, so there is no way book* can match it. (Note that if your filename were book.poem, book* would match it.)

<BR>

<BLOCKQUOTE>

<BLOCKQUOTE>

<HR ALIGN=CENTER>

<BR>

<NOTE>As you saw at the outset, mv and cp are very simple commands. It's specifying the files that's the complicated part! If things still seem confusing, don't worry. Even experts sometimes mess up &quot;simple&quot; moves and copies. Follow the examples 
and try any different ways you think of. There is a definite logic as to how the files to be moved and copied should be specified. It takes a while to become familiar with this logic, and you will have to practice a while before these things become 
intuitive.</NOTE>

<BR>

<HR ALIGN=CENTER>

</BLOCKQUOTE></BLOCKQUOTE>

<BR>

<A NAME="E69E61"></A>

<H4 ALIGN=CENTER>

<CENTER>

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

<BR>

<P>To move a directory, use the mv command. The syntax is mv &lt;directory&gt; &lt;destination&gt;. In the following example, you would move the newdir subdirectory found in your current directory to the /tmp directory:

<BR>

<PRE>

<FONT COLOR="#000080">darkstar:~$ mv newdir /tmp

darkstar:~$ cd /tmp

darkstar:/tmp$ ls

/newdir</FONT></PRE>

<P>The directory newdir is now a subdirectory of /tmp.

<BR>

<BLOCKQUOTE>

<BLOCKQUOTE>

<HR ALIGN=CENTER>

<BR>

<NOTE>When you move a directory, all its files and subdirectories go with it.</NOTE>

<BR>

<HR ALIGN=CENTER>

</BLOCKQUOTE></BLOCKQUOTE>

<BR>

<A NAME="E68E56"></A>

<H3 ALIGN=CENTER>

<CENTER>

<FONT SIZE=5 COLOR="#FF0000"><B>Removing Files and Directories</B></FONT></CENTER></H3>

<BR>

<P>Now that you know how to create files and directories, it's time to learn how to undo your handiwork.

<BR>

<P>To remove (or delete) a file, use the rm command (rm is a very terse spelling of remove). The syntax is rm &lt;filename&gt;. For instance:

<BR>

<BR>

<PRE>

<FONT COLOR="#000080">darkstar:~$ rm dead_duck</FONT></PRE>

<P>removes the file dead_duck from your current directory.

<BR>

<BR>

<PRE>

<FONT COLOR="#000080">darkstar:~$ rm /tmp/dead_duck</FONT></PRE>

<P>removes the file dead_duck from the /tmp directory.

<BR>

<BR>

<PRE>

<FONT COLOR="#000080">darkstar:~$ rm *</FONT></PRE>

<P>removes all files from your current directory. (Be careful when using wildcards!)

<BR>

<BR>

<PRE>

<FONT COLOR="#000080">darkstar:~$ rm /tmp/*duck</FONT></PRE>

<P>removes all files ending in duck from the /tmp directory.

<BR>

<BLOCKQUOTE>

<BLOCKQUOTE>

<HR ALIGN=CENTER>

<BR>

<NOTE><A NAME="I2"></A>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.</NOTE>

<BR>

<OL>

<LI>Run ls using the same file specification you use with the rm command. For instance:

<BR>

<BR>

<UL>

<UL>

<PRE>

<FONT COLOR="#000080"> darkstar:~$ ls *duck

 dead_duck guiduck lame-duck

 :~$ rm *duck</FONT></PRE></UL></UL>

<BR>

<NOTE>In this case, you thought you wanted to remove all files that matched *duck. To verify that this indeed was the case, you listed all the *duck files (wildcards work the same way with all commands). The listing looked okay, so you went ahead and 
removed the files.</NOTE>

<BR>

<LI>Use the i (interactive) option with rm:

<BR>

</OL>

<HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE>

<UL>

<UL>

<PRE>

<FONT COLOR="#000080"> darkstar:~$ rm -i *duck

 rm: remove 'dead_duck'? y

 rm: remove 'guiduck'? n

⌨️ 快捷键说明

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