0358-0361.html
来自「linux-unix130.linux.and.unix.ebooks130 l」· HTML 代码 · 共 651 行
HTML
651 行
<HTML>
<HEAD>
<TITLE>Developer.com - Online Reference Library - 0672311739:RED HAT LINUX 2ND EDITION:GNU Project Utilities</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=0672311739 //-->
<!-- TITLE=RED HAT LINUX 2ND EDITION //-->
<!-- AUTHOR=DAVID PITTS ET AL //-->
<!-- PUBLISHER=MACMILLAN //-->
<!-- IMPRINT=SAMS PUBLISHING //-->
<!-- PUBLICATION DATE=1998 //-->
<!-- CHAPTER=17 //-->
<!-- PAGES=0351-0372 //-->
<!-- UNASSIGNED1 //-->
<!-- UNASSIGNED2 //-->
<P><CENTER>
<a href="0355-0357.html">Previous</A> | <a href="../ewtoc.html">Table of Contents</A> | <a href="0362-0364.html">Next</A>
</CENTER></P>
<A NAME="PAGENUM-358"><P>Page 358</P></A>
<TABLE WIDTH="360">
<TR><TD>
-m, --time=mtime, or --time=modify
</TD><TD>
Changes modification time only
</TD></TR>
<TR><TD>
--date=time
</TD><TD>
Uses time instead of current time
</TD></TR>
<TR><TD>
-r file or --reference=file
</TD><TD>
Uses the times from
file instead of the current time
</TD></TR>
</TABLE>
<P>One of the common uses of touch is to create a large number of files quickly for testing
scripts that read and process filenames.
</P>
<H4><A NAME="ch17_ 6">
Disk Usage
</A></H4>
<P>The GNU disk usage utilities, df and du, are quite similar to their UNIX counterparts,
but implement a few nice options that make their output much easier to read and understand.
</P>
<P>By default, both programs produce output in terms of blocks, which varies depending on
the local machine (strict POSIX machines use 512 bytes per block, while others use 1024
bytes per block), but their output can be changed to be in kilobytes, megabytes, or gigabytes.
The output options are as follows:
</P>
<TABLE WIDTHH="360">
<TR><TD>
-k or --kilobytes
</TD><TD>
Prints in 1KB (1024-byte) blocks
</TD></TR>
<TR><TD>
-m or --megabytes
</TD><TD>
Prints in 1MB (1,048,576 bytes) blocks
</TD></TR>
<TR><TD>
-h or --human-readable
</TD><TD>
Appends a letter to indicate size
(K for kilobytes, M for megabytes, G for gigabytes)
</TD></TR>
</TABLE>
<H3><A NAME="ch17_ 7">
Find Utilities
</A></H3>
<P>The find utilities enable the user to find files that meet given criteria and perform actions
on those files. The three main utilities are
locate, find, and xargs. The locate and find commands are used to locate files, and
xargs is used to act upon those files.
</P>
<H5><A NAME="ch17_ 8">
locate
</A></H5>
<P>The locate command is the simplest and fastest command available for finding files. It
does not actually search the filesystem; instead, it searches through filename databases that
contain a list of files that were in particular directory trees when the databases were last updated.
Typically, the databases are updated nightly, and thus are reasonably up-to-date for executables
and libraries.
</P>
<P>The basic syntax for locate is
</P>
<!-- CODE SNIP //-->
<PRE>
locate [string1 ... stringN]
</PRE>
<!-- END CODE SNIP //-->
<P>Any number of files can be specified and locate will run through the database files and
print out a list of matches. For example,
</P>
<!-- CODE SNIP //-->
<PRE>
locate bash emacs
</PRE>
<!-- END CODE SNIP //-->
<P>prints out a list of files that contain the string
bash or emacs. Some matches on my system include
</P>
<A NAME="PAGENUM-359"><P>Page 359</P></A>
<!-- CODE //-->
<PRE>
/usr/bin/bashbug
/usr/local/bin/bash
/usr/local/man/man1/bash.1
/usr/lib/zoneinfo/Africa/Lumumbashi
/usr/doc/minicom-1.75-2/doc/Todo.emacskey.dif
/usr/local/bin/emacs
/usr/share/emacs/19.34/etc/emacs.1
</PRE>
<!-- END CODE //-->
<P>In addition, locate will also properly handle shell
wildcards. Thus,
</P>
<!-- CODE SNIP //-->
<PRE>
locate *[mM]akefile
</PRE>
<!-- END CODE SNIP //-->
<P>prints out a list of makefiles on the system.
</P>
<P>If the filename databases are not being updated regularly on a system, the system
administrator can update the databases by running the
updatedb command manually. Usually simply running
updatedb without any options and waiting for it to finish is adequate, but sometimes it
is necessary to specify the directories that should and should not be included. To facilitate
this, updatedb understands the following options:
</P>
<TABLE WIDTH="360">
<TR><TD>
--localpaths=path
</TD><TD>
A list of nonnetwork directories to put in the
database; the default is /.
</TD></TR>
<TR><TD>
--netpaths=path
</TD><TD>
A list of network directories to put in the database;
the default is none.
</TD></TR>
<TR><TD>
--prunepaths=path
</TD><TD>
A list of directories not to put in the database;
the default is
</TD></TR>
<TR><TD>
</TD><TD>
`/tmp /usr/tmp /var/tmp /afs'
</TD></TR>
</TABLE>
<H5><A NAME="ch17_ 9">
find
</A></H5>
<P>The find command is much more powerful than
locate and can be given extensive options to modify the search criteria. Unlike
locate, find actually searches the disk (local and/or
remote); thus, it is much slower, but provides the most up-to-date information. The basic syntax of <BR>
find is
</P>
<!-- CODE SNIP //-->
<PRE>
find directory [options]
</PRE>
<!-- END CODE SNIP //-->
<P>The most basic usage of find is to print out the files in a directory and its subdirectories:
</P>
<!-- CODE SNIP //-->
<PRE>
find directory -print
</PRE>
<!-- END CODE SNIP //-->
<P>After learning about the find command, many new users quickly implement an alias or
function as a replacement for locate:
</P>
<!-- CODE SNIP //-->
<PRE>
find / -print | grep $1
</PRE>
<!-- END CODE SNIP //-->
<A NAME="PAGENUM-360"><P>Page 360</P></A>
<P>Generally, this is a bad idea because most systems may have network drives mounted, and
find will end up trying to access them, causing not only the local machine to slow down, but
also remote machines. The correct way to get output like
locate from find is the following:
</P>
<!-- CODE SNIP //-->
<PRE>
find directories -name name -print
</PRE>
<!-- END CODE SNIP //-->
<P>For example, use this line to find all makefiles in
/usr/src/:
</P>
<!-- CODE SNIP //-->
<PRE>
find /usr/src -name "[mM]akefile" -print
</PRE>
<!-- END CODE SNIP //-->
<P>The -name option accepts all shell metacharacters.
An alternative to the preceding method for finding all files named
Makefile or makefile is to use the case-insensitive
-iname option instead of -name. For the truly adventurous,
find also supports a -regex option.
</P>
<P>In addition to specifying which filenames to find,
find can be told to look at files of a specific size, type, owner, or permissions.
</P>
<P>To find a file by size, the following option is used:
</P>
<!-- CODE SNIP //-->
<PRE>
-size n[bckw]
</PRE>
<!-- END CODE SNIP //-->
<P>where n is the size and the letters stand for
</P>
<TABLE WIDTH="360">
<TR><TD>
b
</TD><TD>
512-byte blocks (default)
</TD></TR>
<TR><TD>
c
</TD><TD>
Bytes
</TD></TR>
<TR><TD>
k
</TD><TD>
Kilobytes (1024 bytes)
</TD></TR>
<TR><TD>
w
</TD><TD>
2-byte words
</TD></TR>
</TABLE>
<P>For example, to find all files in /usr over 100KB, use
</P>
<!-- CODE SNIP //-->
<PRE>
find /usr -size 100k
</PRE>
<!-- END CODE SNIP //-->
<P>To find by files by type, the following option is used:
</P>
-
<!-- CODE SNIP //-->
<PRE>
type x
</PRE>
<!-- END CODE SNIP //-->
<P>where x is one of the following letters:
</P>
<TABLE WIDTH="360">
<TR><TD>
b
</TD><TD>
Block (buffered) special
</TD></TR>
<TR><TD>
c
</TD><TD>
Character (unbuffered) special
</TD></TR>
<TR><TD>
d
</TD><TD>
Directory
</TD></TR>
<TR><TD>
p
</TD><TD>
Named pipe (FIFO)
</TD></TR>
<TR><TD>
f
</TD><TD>
Regular file
</TD></TR>
<TR><TD>
l
</TD><TD>
Symbolic link
</TD></TR>
<TR><TD>
s
</TD><TD>
Socket
</TD></TR>
</TABLE>
<A NAME="PAGENUM-361"><P>Page 361</P></A>
<P>Therefore, to find all the symbolic links in
/tmp, use the following:
</P>
<!-- CODE SNIP //-->
<PRE>
find /tmp -type l
</PRE>
<!-- END CODE SNIP //-->
<P>In addition to simply printing out the filename,
find can be told to print out file information by specifying the
-ls option. For example,
</P>
<!-- CODE SNIP //-->
<PRE>
find /var -name "log" -ls
</PRE>
<!-- END CODE SNIP //-->
<P>produces the following output:
</P>
<!-- CODE SNIP //-->
<PRE>
42842 1 drwxr-xr-x 2 root root 1024 Jul 17 14:29 /var/log/httpd
157168 1 -rw-r--r-- 1 root nobody 4 Aug 14 17:44 /var/run/httpd.pid
</PRE>
<!-- END CODE SNIP //-->
<P>The output is similar in form to the output from
ls -il.
</P>
<P>The last option of interest for find is the
-exec option, which allows for the execution of a command on each filename that matches the previous criteria. The basic syntax of the
-exec option is
</P>
<!-- CODE SNIP //-->
<PRE>
-exec [command [options]] `{}' `;'
</PRE>
<!-- END CODE SNIP //-->
<P>The -exec option uses the string `{}' and replaces it with the name of the current file that
was matched. The `;' string is used to tell find where the end of the executed command is.
For example, the following makes a list of all the files that contain the word
foo in the Linux source files:
</P>
<!-- CODE SNIP //-->
<PRE>
find /usr/src/linux -name "*.c" -exec grep -l foo `{}' `;'
</PRE>
<!-- END CODE SNIP //-->
<P>Note that the preceding command should appear all
on one line.
</P>
<H5><A NAME="ch17_ 10">
xargs
</A></H5>
<P>One of the biggest limitations of the -exec command
is that it can only run the specified command on one file at a time. The
xargs command solves this problem. It enables the user to
run a single command on many files at one time. In general, it is much faster to run one
command on many files because this cuts down on the number of commands that need to be
started. Here's how to modify the preceding example to count the number of files with
foo in them:
</P>
<!-- CODE SNIP //-->
<PRE>
find /usr/src/linux -name "*.c" -exec grep -l foo `{}' `;' | wc -l
</PRE>
<!-- END CODE SNIP //-->
<P>Note that this command, also, should appear all on one line.
</P>
<P>In my version of the sources (780 files), there were 27 files with the word
foo in them, and it took about 44 seconds to find that out.
</P>
<P>Now let's modify it to run with xargs. First, you need to replace the
-exec grep foo `{}' `;' part with an xargs so to avoid having to start up new
greps for each file. The basic syntax for xargs is
</P>
<!-- CODE SNIP //-->
<PRE>
xargs [options] [command [options]]
</PRE>
<!-- END CODE SNIP //-->
<P><CENTER>
<a href="0355-0357.html">Previous</A> | <a href="../ewtoc.html">Table of Contents</A> | <a href="0362-0364.html">Next</A>
</CENTER></P>
</td>
</tr>
</table>
<!-- begin footer information -->
</body></html>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?