📄 225-228.html
字号:
<HTML>
<HEAD>
<TITLE>Linux Configuration and Installation:Basic Linux Tools</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=1558285660//-->
<!--TITLE=Linux Configuration and Installation//-->
<!--AUTHOR=Patrick Volkerding//-->
<!--AUTHOR=Kevin Reichard//-->
<!--AUTHOR=Eric Foster//-->
<!--PUBLISHER=IDG Books Worldwide, Inc.//-->
<!--IMPRINT=M & T Books//-->
<!--CHAPTER=4//-->
<!--PAGES=225-228//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="221-225.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="228-230.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H4 ALIGN="LEFT"><A NAME="Heading20"></A><FONT COLOR="#000077">Finding the Magic Number with File</FONT></H4>
<P>After you’ve been working on your Linux system for a while, you’ll accumulate many files if you do any serious work at all. If you’re a careful worker, you’ll be able to keep track of files by their locations and filenames. However, if you’re not a careful worker, you may run into situations where you have no idea what a file contains. You don’t want to view a binary file with <B>cat</B> or another command designed to view text files, because doing so will probably result in huge amounts of garbage being displayed to your screen (which may require you to try and relogin your system).</P>
<P>Linux features a command, <B>file</B>, that will look at a file and return specific information about the contents of the file (most of the time, anyway). The UNIX world of late has supported <I>magic numbers</I>, and in theory these numbers—found somewhere in the binary file—should match a database of magic numbers on your system. (These magic numbers can be found in the <B>/etc/magic</B> file.) To run <B>file</B> on a file named <B>45edfsdwe</B>, you’d use the following command line:</P>
<!-- CODE SNIP //-->
<PRE>
gilbert:~$ file 45edfsdwe
</PRE>
<!-- END CODE SNIP //-->
<P>At the very least, <B>file</B> will tell you the file’s type (executable, ordinary, etc.) and how it’s compiled (such as <I>dynamically linked</I>). If you’re lucky, the <B>file</B> command will also tell you if the file is related to your machine. However, if this file is merely text, you’ll see the following information:</P>
<!-- CODE SNIP //-->
<PRE>
gilbert:~$ file 45edfsdwe
45edfsdwe text
</PRE>
<!-- END CODE SNIP //-->
<H4 ALIGN="LEFT"><A NAME="Heading21"></A><FONT COLOR="#000077">Copying Files with Cp</FONT></H4>
<P>The <B>cp</B> command is used to copy existing files. When you use the <B>cp</B> command, the original file is left intact. This is handy when copying files to another user’s machine (provided you’re networked, of course) or to another directory for backup purposes. (There are more formal ways to make system backups on your Linux system, of course, but the <B>cp</B> command works well for single files or small groups of files.) The following command line copies a file named <B>textfile</B> to the <B>/home/eric</B> directory:</P>
<!-- CODE SNIP //-->
<PRE>
gilbert:~$ cp textfile /home/eric
</PRE>
<!-- END CODE SNIP //-->
<P>When this command is run, the file named <B>textfile</B> will appear in both your home directory and <I>eric’s</I> home directory.</P>
<P>You may want to give <B>textfile</B> a new name when it’s moved to the new directory. In this case, you’re giving <B>textfile</B> a new name of <B>textfile.kr</B> when it’s moved to the <B>/home/eric</B> directory:</P>
<!-- CODE SNIP //-->
<PRE>
gilbert:~$ cp textfile /home/eric/textfile.kr
</PRE>
<!-- END CODE SNIP //-->
<BLOCKQUOTE>
<P><FONT SIZE="-1"><HR><B>WARNING: </B>Linux will do exactly what you tell it to do. In some cases, this is a good thing. In other cases, this is a very bad thing—as can be the case with the <B>cp</B> and <B>mv</B> commands.<HR></FONT>
</BLOCKQUOTE>
<P>If (using the previous command-line example) there were already a file called <B>textfile.kr</B> in the <B>/home/eric</B> directory, the <B>cp</B> command would overwrite the existing file with the new file. The <B>cp</B> command, by default, doesn’t check to see if there’s a file already in that directory with the same name. (The same goes for the <B>mv</B> command; this will be covered in the next section, “Moving and Renaming Files with mv.”)</P>
<P>On the other hand, both the <B>cp</B> and <B>mv</B> commands have an option (<I>-i</I>) that prevents you from overwriting existing files, as seen in this command line:</P>
<!-- CODE SNIP //-->
<PRE>
gilbert:~$ cp -i textfile /home/eric/textfile.kr
cp: overwrite `textfile.kr'?
</PRE>
<!-- END CODE SNIP //-->
<P>If you type <B>y</B>, <B>cp</B> will overwrite the existing <B>textfile.kr</B>. If you type anything else, <B>cp</B> will not overwrite the file.</P>
<P>Options to the <B>cp</B> command are listed in Table 4.8.</P>
<CENTER>
<TABLE WIDTH="95%"><CAPTION><B>Table 4.8</B> Options to the cp Command
<TR>
<TH WIDTH="20%" ALIGN="LEFT">Option
<TH WIDTH="80%" ALIGN="LEFT">Result
<TR>
<TH COLSPAN="2"><HR>
<TR>
<TD VALIGN="TOP">-d
<TD>Maintains a symbolic link as a link rather than as a copy of the original file.
<TR>
<TD>-i
<TD>Prevents overwriting of existing files with the same filename.
<TR>
<TD>-p
<TD>Retains the existing permissions.
<TR>
<TD>-r
<TD>Copies the entire directory structure, including subdirectories.
<TR>
<TD>-v
<TD>Runs in verbose mode; lists each file as it’s copied.
<TR>
<TD COLSPAN="2"><HR>
</TABLE>
</CENTER>
<P><FONT SIZE="+1"><B>Copying Directories with Cp</B></FONT></P>
<P><B>cp</B> also has the power to copy entire directories (including all files and subdirectories), in the form of the <I>-r</I> option:</P>
<!-- CODE SNIP //-->
<PRE>
gilbert:~$ cp -r /users/data /users/eric
</PRE>
<!-- END CODE SNIP //-->
<P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="221-225.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="228-230.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
</td>
</tr>
</table>
<!-- begin footer information -->
</body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -