⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch08.htm

📁 linux-unix130.linux.and.unix.ebooks130 linux and unix ebookslinuxLearning Linux - Collection of 12 E
💻 HTM
📖 第 1 页 / 共 5 页
字号:


<HR>







</DL>







<P>So how do you use <TT>cat</TT> to create a file? Simple! You redirect the output



from <TT>cat</TT> to the desired filename:<FONT COLOR="#0066FF"></FONT>



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



Hello world



Here's some text



</FONT></PRE>



<P>You can type as much as you want. When you are finished, press <TT>^D</TT> by



itself on a line; you will be back at the Linux prompt.</P>



<P>Now you want to look at the contents of <TT>newfile</TT>. You could use the <TT>more</TT>



or <TT>less</TT> commands, but instead, let's use <TT>cat</TT>. Yes, you can use



<TT>cat</TT> to look at files simply by providing it with a filename:</P>



<PRE><FONT COLOR="#0066FF">darkstar:~$ cat newfile



Hello world



Here's some text



darkstar:~$



</FONT></PRE>



<P>Neat! You can also add to the end of the file by using <TT>&gt;&gt;</TT>. Whenever



you use <TT>&gt;&gt;</TT>, whether with <TT>cat</TT> or any other command, the output



is always appended to the specified file. (Note that the <TT>^D</TT> character does



not appear on-screen. I show it in the examples for clarity.)<FONT COLOR="#0066FF"></FONT>



<PRE><FONT COLOR="#0066FF">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 <TT>cat</TT> actually stands for, let's first create another



file.<FONT COLOR="#0066FF"></FONT>



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



Different text



^D



darkstar:~$



</FONT></PRE>



<P>Now, try this:<FONT COLOR="#0066FF"></FONT>



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



darkstar:~$ cat thirdfile



Hello world



Here's some text



Some more lines



Different text



darkstar:~$



</FONT></PRE>



<P><TT>cat</TT> stands for concatenate; <TT>cat</TT> 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.</P>



<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 Chapter 16, &quot;Text Editors.&quot;



<H4 ALIGN="CENTER"><A NAME="Heading22<FONT COLOR="#000077">Creating Directories</FONT></H4>



<P>To create a new directory, use the <TT>mkdir</TT> command. The syntax is <TT>mkdir



&lt;</TT>name<TT>&gt;</TT>, where <TT>&lt;</TT>name<TT>&gt;</TT> is replaced by whatever



you want the directory to be called. This creates a subdirectory with the specified



name in your current directory:</P>



<PRE><FONT COLOR="#0066FF">darkstar:~$ ls



anotherfile    newfile      thirdfile



darkstar:~$ mkdir newdir



darkstar:~$ ls



anotherfile    newdir/       newfile        thirdfile



</FONT></PRE>







<DL>



	<DT><FONT COLOR="#0066FF"></FONT></DT>



</DL>











<DL>



	<DD>



<HR>



<A NAME="Heading23<FONT COLOR="#000077"><B>TIP:</B> </FONT>The <TT>mkdir</TT>



	command is already familiar to you if you have used MS-DOS systems. In MS-DOS, you



	can abbreviate <TT>mkdir</TT> as <TT>md</TT>. You might think that <TT>md</TT> would



	work in Linux, because, after all, most of the commands we've seen have extremely



	concise names. However, Linux doesn't recognize <TT>md</TT>; it insists on the full



	<TT>mkdir</TT>. If you frequently switch between Linux and MS-DOS, you might want



	to use <TT>mkdir</TT> for both systems. However, be warned that you might start typing



	other Linux commands in MS-DOS--for example, typing <TT>ls</TT> instead of <TT>dir</TT>!



<HR>







</DL>







<H4 ALIGN="CENTER"><A NAME="Heading24<FONT COLOR="#000077">Moving and Copying



Files</FONT></H4>



<P>You often need to move or copy files. The <TT>mv</TT> command moves files, and



the <TT>cp</TT> command copies files. The syntax for the two commands is similar:<FONT



COLOR="#0066FF"></FONT>



<PRE><FONT COLOR="#0066FF">mv &lt;source&gt; &lt;destination&gt;



cp &lt;source&gt; &lt;destination&gt;



</FONT></PRE>



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



example:<FONT COLOR="#0066FF"></FONT>



<PRE><FONT COLOR="#0066FF">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 <TT>cat</TT> (or <TT>more</TT> or <TT>less</TT>) at any time to verify



that <TT>anotherfile</TT> became <TT>movedfile</TT>, and that the contents of file



<TT>xyz</TT> are identical to the contents of <TT>thirdfile</TT>.</P>



<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--for instance,



<TT>/home/fido/newfile</TT>. However, Linux lets you leave off parts of the file's



name, because it's more convenient to refer to <TT>newfile</TT> rather than <TT>/home/fido/newfile</TT>.</P>



<P>For instance, suppose you want to move <TT>newfile</TT> into the <TT>newdir</TT>



subdirectory. If you want the file to keep the same name, you type<FONT COLOR="#0066FF"></FONT>



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



</FONT></PRE>



<P>However, it's much more common to type<FONT COLOR="#0066FF"></FONT>



<PRE><FONT COLOR="#0066FF">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.</P>



<P>You could also use <TT>cd</TT> to change to the directory you want to move the



file to:<FONT COLOR="#0066FF"></FONT>



<PRE><FONT COLOR="#0066FF">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 <TT>../newfile</TT>, which means &quot;the file <TT>newfile</TT> 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 <TT>mv</TT> to &quot;go up one level, grab <TT>newfile</TT>, 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.</P>



<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:<FONT COLOR="#0066FF"></FONT>



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



</FONT></PRE>



<P>This would create a copy of <TT>newfile</TT> in the directory <TT>newdir</TT>



and name the copied file <TT>anothername</TT>.







<DL>



	<DT></DT>



</DL>











<DL>



	<DD>



<HR>



<A NAME="Heading25<FONT COLOR="#000077"><B>WARNING:</B> </FONT>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. If in the



	example just shown you mistyped <TT>newdir</TT>--for instance, as <TT>mv newfile



	mewdir</TT>--you would wind up with a file called <TT>mewdir</TT> in your current



	directory and no file <TT>newfile</TT> in the <TT>newdir</TT> subdirectory! Another



	way you would get an unexpected result would be to type <TT>cp newfile newdir</TT>



	if you didn't realize that the directory <TT>newdir</TT> existed. In this case, you



	would be expecting to create an identical file called <TT>newdir</TT> in your current



	directory. What you would actually do is create a copy of <TT>newfile</TT>, called



	<TT>newfile</TT>, in the subdirectory <TT>newdir</TT>.



<HR>







</DL>







<P>The <TT>mv</TT> command is much more efficient than the <TT>cp</TT> command. When



you use <TT>mv</TT>, 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.</P>







<P>When you use <TT>cp</TT>, 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!



<H4 ALIGN="CENTER"><A NAME="Heading26<FONT COLOR="#000077">Moving and Copying



with Wildcards</FONT></H4>



<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 <TT>cp</TT> command on each one. Fortunately,



you can use the wildcards <TT>*</TT> and <TT>?</TT> to copy more than one file at



a time.</P>



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



COLOR="#0066FF"></FONT>



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



</FONT></PRE>



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



<P>You can use <TT>*</TT>, along with other characters, to match only certain files.



For instance, suppose you have a directory that contains the files <TT>book1</TT>,



<TT>book_idea</TT>, <TT>book-chapter-1</TT>, and <TT>poem.book</TT>. To copy just



the first three files, you could type <TT>cp book* /tmp</TT>. When you type <TT>book*</TT>,



you are asking Linux to match all files whose names start with <TT>book</TT>. In



this case, <TT>poem.book</TT> does not start with <TT>book</TT>, so there is no way



<TT>book*</TT> can match it. (Note that if your filename were <TT>book.poem</TT>,



<TT>book*</TT> would match it.)







<DL>



	<DT></DT>



</DL>











<DL>



	<DD>



<HR>



<A NAME="Heading27<FONT COLOR="#000077"><B>NOTE:</B> </FONT>As you saw at the



	outset, <TT>mv</TT> and <TT>cp</TT> 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.



<HR>







</DL>







<H4 ALIGN="CENTER"><A NAME="Heading28<FONT COLOR="#000077">Moving Directories</FONT></H4>



<P>To move a directory, use the <TT>mv</TT> command. The syntax is <TT>mv &lt;</TT>directory<TT>&gt;



&lt;</TT>destination<TT>&gt;</TT>. In the following example, you would move the <TT>newdir</TT>



subdirectory found in your current directory to the <TT>/tmp</TT> directory:<FONT



COLOR="#0066FF"></FONT>



<PRE><FONT COLOR="#0066FF">darkstar:~$ mv newdir /tmp



darkstar:~$ cd /tmp



darkstar:/tmp$ ls



/newdir



</FONT></PRE>



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







<DL>



	<DT></DT>



</DL>











<DL>



	<DD>



<HR>



<A NAME="Heading29<FONT COLOR="#000077"><B>NOTE:</B> </FONT>When you move a



	directory, all its files and subdirectories go with it.



<HR>







</DL>







<H3 ALIGN="CENTER"><A NAME="Heading30<FONT COLOR="#000077">Removing Files and



Directories</FONT></H3>



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



to undo your handiwork.</P>



<P>To remove (or delete) a file, use the <TT>rm</TT> command (<TT>rm</TT> is a very



terse spelling of remove). The syntax is <TT>rm &lt;</TT>filename<TT>&gt;</TT>. For



instance:<FONT COLOR="#0066FF"></FONT>



<PRE><FONT COLOR="#0066FF">darkstar:~$ rm dead_duck



</FONT></PRE>



<P>removes the file <TT>dead_duck</TT> from your current directory.<FONT COLOR="#0066FF"></FONT>



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



</FONT></PRE>



<P>removes the file <TT>dead_duck</TT> from the <TT>/tmp</TT> directory.<FONT COLOR="#0066FF"></FONT>



<PRE><FONT COLOR="#0066FF">darkstar:~$ rm *



</FONT></PRE>



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



COLOR="#0066FF"></FONT>



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



</FONT></PRE>



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







<DL>



	<DT></DT>



</DL>











<DL>



⌨️ 快捷键说明

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