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

📄 0069-0071.html

📁 linux-unix130.linux.and.unix.ebooks130 linux and unix ebookslinuxLearning Linux - Collection of 12 E
💻 HTML
字号:




<HTML>

<HEAD>

<TITLE>Sams Teach Yourself Linux in 24 Hours:Manipulation and Searching Commands:EarthWeb Inc.-</TITLE>

<META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW">
<SCRIPT>
<!--
function displayWindow(url, width, height) {
        var Win = window.open(url,"displayWindow",'width=' + width +
',height=' + height + ',resizable=1,scrollbars=yes');
}
//-->
</SCRIPT>
</HEAD>

 -->




<!-- ISBN=0672311623 //-->

<!-- TITLE=Sams Teach Yourself Linux in 24 Hours//-->

<!-- AUTHOR=Bill Ball//-->

<!-- PUBLISHER=Macmillan Computer Publishing//-->

<!-- IMPRINT=Sams//-->

<!-- CHAPTER=05 //-->

<!-- PAGES=0063-0082 //-->

<!-- UNASSIGNED1 //-->

<!-- UNASSIGNED2 //-->







<P><CENTER>

<a href="0066-0068.html">Previous</A> | <a href="../ewtoc.html">Table of Contents</A> | <a href="0072-0074.html">Next</A>

</CENTER></P>



<A NAME="PAGENUM-69"><P>Page 69</P></A>







<P>The mv command can work silently, or as with rm, you can use the

-i (interactive) option, for example:

</P>



<!-- CODE SNIP //-->

<PRE>

# mv -i file2 file3

# mv -i file2 file3

mv: replace `file3'? y

</PRE>

<!-- END CODE SNIP //-->



<P>Here, the mv command asks if you want to overwrite the file (but will keep quiet if

no overwriting takes place, even if you use -i). You can also combine the

-b and -i options with

</P>



<!-- CODE SNIP //-->

<PRE>

# mv -bi file2 file3

</PRE>

<!-- END CODE SNIP //-->



<P>Now that you've seen how to delete, rename, or move your files, how do you copy files?

</P>



<H4><A NAME="ch05_ 10">

Copying with the cp Command

</A></H4>



<P>The cp, or copy, command is used to copy files or directories. This command has

nearly 40 command-line options. They won't all be covered here, but you'll learn about some

of the most commonly used options, which will save you time and trouble.

</P>



<P>You'll most likely first use cp in its simplest form, for example:

</P>



<!-- CODE SNIP //-->

<PRE>

# cp file1 file2

</PRE>

<!-- END CODE SNIP //-->



<P>This creates file2, and unlike mv, leaves file1 in place. But you must be careful when

using cp, because you can copy a file onto a second file, effectively replacing it! In this

regard, cp can act just like mv. To show you how this can happen, try creating three files, each

with a line of text, using the cat command:

</P>



<!-- CODE //-->

<PRE>

# cat &gt; file1

this is file1

# cat &gt; file2

this is file 2

# cat &gt; file3

this is the third file

# ls -l file*

-rw-rw-r--   1 bball    bball          14 Nov 13 16:12 file1

-rw-rw-r--   1 bball    bball          15 Nov 13 16:12 file2

-rw-rw-r--   1 bball    bball          23 Nov 13 16:12 file3

</PRE>

<!-- END CODE //-->



<P>Now, copy a file onto another file, then check the file sizes and the contents of the new file:

</P>



<!-- CODE //-->

<PRE>

# cp file1 file2

# ls -l file*

-rw-rw-r--   1 bball    bball          14 Nov 13 16:12 file1

-rw-rw-r--   1 bball    bball          14 Nov 13 16:14 file2

-rw-rw-r--   1 bball    bball          23 Nov 13 16:12 file3

# cat file2

this is file1

</PRE>

<!-- END CODE //-->



<P>It should be obvious that file1 has replaced

file2. To avoid this problem (unless you really

want to overwrite the file), you can use the -b or

-i options, which work just like mv. Here's an example:

</P>





<A NAME="PAGENUM-70"><P>Page 70</P></A>





<!-- CODE SNIP //-->

<PRE>

# cp -i file1 file2

cp: overwrite `file2'? n

# cp -bi file1 file2

cp: overwrite `file2'? y

# ls file*

file1   file2   file2~  file3

</PRE>

<!-- END CODE SNIP //-->



<P>Note that file2, which was overwritten, was backed up. The

cp command may also be used to copy a number of files at one time. The following example shows how to copy all of

the files in directory tempdir1 to directory

tempdir2:

</P>



<!-- CODE //-->

<PRE>

# cp tempdir1/* tempdir2

# tree tempdir2

tempdir2

|-- temp1file1

|-- temp1file2

`-- temp1file3



0 directories, 3 files

</PRE>

<!-- END CODE //-->



<P>Like the rm command, cp also has a -r, or recursive, option. You can use this option to

copy one directory into another. For example, to copy the

tempdir1 directory and its files into tempdir2, use this syntax:

</P>



<!-- CODE //-->

<PRE>

# cp -r tempdir1 tempdir2

# tree tempdir2

tempdir2

|-- temp1file1

|-- temp1file2

|-- temp1file3

`-- tempdir1

    |-- temp1file1

    |-- temp1file2

    `-- temp1file3



1 directory, 6 files

</PRE>

<!-- END CODE //-->



<P>Finally, the cp command has the -p option, which is similar to

mkdir's -p option. Normally, when you copy a file inside several directories into another directory,

only the file is copied. The following example will only copy

temp1file1 into tempdir3:

</P>



<!-- CODE //-->

<PRE>

# tree tempdir2

tempdir2

|-- temp1file1

|-- temp1file2

|-- temp1file3

`-- tempdir1

    |-- temp1file1

    |-- temp1file2

    `-- temp1file3



1 directory, 6 files

</PRE>

<!-- END CODE //-->



<A NAME="PAGENUM-71"><P>Page 71</P></A>





<!-- CODE SNIP //-->

<PRE>

# cp tempdir2/tempdir1/temp1file1 tempdir3

</PRE>

<!-- END CODE SNIP //-->



<P>However, what if you'd like a file, along with its directory structure, copied? To do

this, you can use the -P option:

</P>



<!-- CODE //-->

<PRE>

# cp -P tempdir2/tempdir1/temp1file1 tempdir3

# tree tempdir3

tempdir3

`-- tempdir2

    `-- tempdir1

        `-- temp1file1



2 directories, 1 file

</PRE>

<!-- END CODE //-->



<P>Not only has cp copied a single file, but it has also created the subdirectory structure.

</P>



<H4><A NAME="ch05_ 11">

Creating Hard and Symbolic Links<BR>

with the ln Command

</A></H4>





<P>Linux supports both hard and symbolic links. Although it is not important that

you understand how links work in Linux, you should understand

the difference between these two types of links and how to use links while you use Linux. To create hard or

symbolic links, you use the ln, or link, command.

</P>



<P>The ln command creates both types of links. If you use the

ln command to create a hard link, you specify a second file on the command line you can use to reference the

original file, for example:

</P>



<!-- CODE //-->

<PRE>

# cat &gt; file1

This is file1

# ln file1 file2

# ls -l file*

-rw-rw-r--   2 bball    bball          14 Nov 13 18:54 file1

-rw-rw-r--   2 bball    bball          14 Nov 13 18:54 file2

# cat file2

This is file1

</PRE>

<!-- END CODE //-->



<P>You can see that file2 is exactly the same as

file1. If you delete file1, file2 will remain. If

you make changes to file1, such as adding text, these changes will appear in

file2, and if you make changes to file2, file1 will also be updated. You should also know that although you can

see two files, each 14 characters in size, only 14 characters of hard drive space are used

(OK, technically more than that, but it depends on the block size of the partition or type of

hard drive).

</P>



<P>On the other hand, although a symbolic link can be useful, it also has a drawback. The

next examples show you why. First, to create a symbolic link, use the

ln command -s option:

</P>



<!-- CODE SNIP //-->

<PRE>

# ln -s file1 file2

</PRE>

<!-- END CODE SNIP //-->



<P><CENTER>

<a href="0066-0068.html">Previous</A> | <a href="../ewtoc.html">Table of Contents</A> | <a href="0072-0074.html">Next</A>

</CENTER></P>











</td>
</tr>
</table>

<!-- begin footer information -->









</body></html>

⌨️ 快捷键说明

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