home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Complete Idiot's Guide to Linux
(Publisher: Macmillan Computer Publishing)
Author(s): Manuel Ricart
ISBN: 078971826x
Publication Date: 12/22/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


Locating Files: locate

As you can tell, finding files takes a long time. The find command needs to search the disk for the files you specify. If you are doing a search on a filename, there’s a better and faster way: locate.

The locate command gains its speed by prebuilding a database of all known filenames. Instead of scanning the disk for a match, it uses sophisticated database techniques to find the file instantaneously—or close enough to instantaneously. The drawback of this feature is that if the file you are searching for was created after the database was updated, the database won’t have an entry for it, and therefore locate won’t be able to find it. Typically, this is not an issue because locate updates its database every morning at 5:55 AM or so. What, your computer isn’t on at that time? Well, you can run the command manually, too.

Building the locate Database: updatedb

To create the locate database, you must be root and issue the command /usr/bin/updatedb. The program will take a while to complete its run, but after it does it, you are in business.

Finding a File: locate

To search for a file, just type its name or a portion of its name (no wildcards), and any file path matching your search will be printed. Here’s an example:

     [root@digital alberto]# locate telnet
     /mnt/cdrom/col/install/RPMS/netkit-telnet-0.12-1.i386.rpm
     /usr/bin/telnet
     /usr/include/arpa/telnet.h
     /usr/lib/xemacs-19.16/etc/w3/telnet.xbm
     /usr/lib/xemacs-19.16/lisp/comint/telnet.elc
     /usr/man/man1/telnet.1.gz
     /usr/man/man8/in.telnetd.8.gz
     /usr/man/man8/telnetd.8.gz
     /usr/sbin/in.telnetd
     /var/lib/LST/contents/netkit-telnet
     /var/lib/LST/installed/netkit-telnet

All that is left is for you to decide what file you want! Not bad. The same command could have probably taken 10 minutes to complete using the find command.

Finding Files that Contain a Word or Pattern: grep

Whereas the find command looks for matches according to file system attributes (name of the file, modification time, and so on), the grep command searches for patterns inside of a file. For example, if you wanted to find files that contain the word command, you could do this with grep:

     [alberto@digital alberto]$ grep command *.txt
     intro.txt: Through a command line interface
     intro.txt:The UNIX shell is not going away anytime soon. For this
     reason it is a lso covered in detail. In order for you to gain
     expertise an facility using Linux you have to be as
     comfortable in a command line environment as you are in a
     graphical one.
     intro2.txt: Through a command line interface
     intro2.txt:using Linux you have to be as comfortable in a
     command line

Notice that grep prints the line of text where it found a match. If the file has more than one match, they are all printed. A special option -c prints the number of times the word was matched in the file:

     [alberto@digital alberto]$ grep command -c *.txt

     Hello World.txt:0
     afile.txt:0
     file.txt:0
     file1.txt:0
     file2.txt:0
     file3.txt:0
     intro.txt:2
     intro2.txt:2
     kdetools.txt:0

The grep command can also be used in a pipe to filter output you get from other programs. For example, to see a list of users on the system who are coming from the IP beginning with 192.168, you should type the following:

     [alberto@digital alberto]$ w | grep 192.168
     alberto  ttyp0    192.168.0.10     5:07pm  4:53   1.17s  1.03s  -bash
     alberto  ttyp1    192.168.0.10     5:54pm  0.00s  0.64s  0.09s  w

The grep command is very powerful, and you can combine it with regular expressions for your searches. See the man page for more information.

Text and File Utilities

Here are some utilities for working with textual information. Many of these utilities can be used creatively for purposes very different from text processing.

Counting Lines, Words, and Characters: wc

The word count (wc) utility is useful in many ways—it allows you to count the words, lines, and characters that appear in one or more files. Its syntax is simple:

     wc [options] files

The options provided by wc are

-c Print only the number of bytes (characters)
-w Print only the number of words
-l Print only the number of lines

If you supply more than one file, wc is happy to also provide you with a summary report for all the files. If you don’t supply an option, it prints all the information, lines, words, and characters—in that order. Here’s an example of using this command with the /etc/passwd and /etc/group files:

     [alberto@digital alberto]$ wc /etc/passwd /etc/group
         21     28    770 /etc/passwd
         26     26    346 /etc/group
         47     54    1116 total

Although at simple inspection you might think you don’t need this command (and you might be right), there are a number of situations in which creative use of the wc command can give you the information you need.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.