0063-0065.html
来自「linux-unix130.linux.and.unix.ebooks130 l」· HTML 代码 · 共 268 行
HTML
268 行
<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="../ch04/0059-0062.html">Previous</A> | <a href="../ewtoc.html">Table of Contents</A> | <a href="0066-0068.html">Next</A>
</CENTER></P>
<A NAME="PAGENUM-63"><P>Page 63</P></A>
<H3><A NAME="ch05_ 2">Hour 5</A></H3>
<H2>
<A NAME="ch05_ 3">
Manipulation and Searching<BR>
Commands
</A>
</H2>
<P>In this hour, you'll learn about creating, copying, deleting, and moving
files and directories. You'll also learn about searching through files and how
to compress and uncompress files. This information will build on
information you've learned in the last hour, and the commands you learn here will be
used later on in this book.
</P>
<H3><A NAME="ch05_ 4">
Manipulating Files or Directories
</A></H3>
<P>Using Linux isn't different from any other computer operating system.
You create, delete, and move files on your hard drive in order to organize
your information and manage how your system works or looks. This section
shows you how to do these tasks quickly and easily.
</P>
<P>Although the graphical interface for Linux, the X Window
System, may offer drag and drop or multiple selections in order to copy or delete files, many
of the commands you'll learn here form the base of these operations. It is
worth knowing how these programs work, even if you don't use Linux in the
console mode.
</P>
<A NAME="PAGENUM-64"><P>Page 64</P></A>
<H4><A NAME="ch05_ 5">
Creating Files with the touch Command
</A></H4>
<P>The touch command is easy to use, and generally, there are two reasons to use it. The
first reason is to create a file, and the second is to update a file's modification date. The
touch command is part of the GNU file utilities package, and has several options.
</P>
<P>To create a file with touch, use
</P>
<!-- CODE SNIP //-->
<PRE>
# touch newfile
# ls -l newfile
-rw-rw-r-- 1 bball bball 0 Nov 13 08:50 newfile
</PRE>
<!-- END CODE SNIP //-->
<P>As you can see, touch created a file with a length, or size, of zero. You can also use
</P>
<!-- CODE SNIP //-->
<PRE>
# > newfile2
# ls -l new*
-rw-rw-r-- 1 bball bball 0 Nov 13 08:50 newfile
-rw-rw-r-- 1 bball bball 0 Nov 13 08:54 newfile2
</PRE>
<!-- END CODE SNIP //-->
<P>Like touch, this creates a file with a length of zero. So why use
touch, if you can do this at the command line? Because
touch will update a file's date or time. You can even use
touch to change a file's date or time to the past or the future, for example:
</P>
<!-- CODE SNIP //-->
<PRE>
# touch newfile2
# ls -l newfile2
-rw-rw-r-- 1 bball bball 0 Nov 13 09:04 newfile2
</PRE>
<!-- END CODE SNIP //-->
<P>As you can see, the file newfile2 now has a timestamp 10 minutes younger. You can also
set the time and date of a file to an arbitrary date, for example:
</P>
<!-- CODE SNIP //-->
<PRE>
# touch -t 1225110099 newfile2
# ls -l —full-time new*
-rw-rw-r-- 1 bball bball 0 Thu Nov 13 08:50:00 1997 newfile
-rw-rw-r-- 1 bball bball 0 Sat Dec 25 11:00:00 1999 newfile2
</PRE>
<!-- END CODE SNIP //-->
<P>Using the --full-time option and long format listing of the
ls command shows that the file newfile2 now has a timestamp of 11 a.m., Christmas Day, 1999 (which appears to be, and is
indeed, a Saturday).
</P>
<P>One use for touch is during backup operations.
Either before or after backing up a series of files or directories, you can use
touch to update the timestamps of your files so that
the backup program has a reference time for the next backup session. Another use for
touch is to control deletion or retention of log files during the next automated file cleanup
by scheduled programs managed by cron (see "Using the
cron Daemon" in Hour 24, "Scheduling"). If you make a log file old enough, it will be deleted. If you update it,
the file will be retained.
</P>
<H4><A NAME="ch05_ 6">
Deleting Files with the rm Command
</A></H4>
<P>The rm command deletes files. This command has several simple options, but should
be used cautiously. Why? Because when rm deletes a file, it is gone (you may be able to
recover
</P>
<A NAME="PAGENUM-65"><P>Page 65</P></A>
<P>
portions of text files, though; see the mc command or the Command Reference section
for pointers).
</P>
<P>Always running Linux while logged in as the root
operator and using the rm command has caused many untold tales of woe and grief. Why? Because with one simple command
you can wipe out not only your Linux system, but also any mounted filesystems, including
DOS partitions, flash RAM cards, or removable hard drives, as follows:
</P>
<!-- CODE SNIP //-->
<PRE>
# rm -fr /*
</PRE>
<!-- END CODE SNIP //-->
<P>This command removes all files and directories recursively (with the
-r option), starting at the root or / directory. If you must run Linux as root, make sure to back up your
system, and read Hour 23, "Archiving."
</P>
<P>The rm command will delete one or several files from the command line. You can use
any of the following:
</P>
<!-- CODE SNIP //-->
<PRE>
# rm file
# rm file1 file2 file2
# rm file*
</PRE>
<!-- END CODE SNIP //-->
<P>One of the safer ways to use rm is through the
-i or interactive option, where you'll be asked if you want to delete the file, for example:
</P>
<!-- CODE SNIP //-->
<PRE>
# rm -i new*
rm: remove `newfile'? y
rm: remove `newfile2'? y
</PRE>
<!-- END CODE SNIP //-->
<P>You can also force file deletion by using the
-f option, as in
</P>
<!-- CODE SNIP //-->
<PRE>
# rm -f new*
</PRE>
<!-- END CODE SNIP //-->
<P>However, if rm finds a directory, even if it is empty, it will not delete the directory,
and complains, even if you use -f, as in the following:
</P>
<!-- CODE SNIP //-->
<PRE>
# rm -f temp*
rm: temp: is a directory
rm: temp2: is a directory
</PRE>
<!-- END CODE SNIP //-->
<P>When you combine -f and -r, the recursive option, you can delete directories and all
files or directories found within (if you own them; see Hour 21, "Handling Files"), as in
the following example:
</P>
<!-- CODE SNIP //-->
<PRE>
# rm -fr temp*
</PRE>
<!-- END CODE SNIP //-->
<P>The -fr option also makes rm act like the rmdir command (discussed later in this chapter).
Use this option with caution!
</P>
<P>Some X Window managers, such as CDE, or utilities, such as TkDesk, offer "trash
can" approaches to deleting files, but the files are not really deleted, just moved to a
temporary
</P>
<P><CENTER>
<a href="../ch04/0059-0062.html">Previous</A> | <a href="../ewtoc.html">Table of Contents</A> | <a href="0066-0068.html">Next</A>
</CENTER></P>
</td>
</tr>
</table>
<!-- begin footer information -->
</body></html>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?