|
|
|
|
8.8 Searching for Files or Contents
Bash offers you several commands to search for files and to search for
contents of files:
-
locate
-
This utility is only available if you have installed the
findutils-locate package.
With this command you can find out in which directory a specified file
is located. If desired, use wild cards to specify filenames. The program
is very speedy, because it uses a database specifically created for the
purpose (rather than searching through the entire file system). This
very fact, however, also results in a major drawback: locate is unable
to find any files created after the latest update of its database. The
database can be generated by root running updatedb.
-
find
-
With find, search for a file in a given
directory. The first argument specifies the directory in which to start
the search. The option -name must be followed by a
search string, which may also include wild cards. Unlike
locate, which uses a database,
find scans the actual directory.
-
grep
-
The grep command finds a specific search string
in the specified text files. If the search string is found, the command
displays the line in which searchstring was found
along with the filename. If desired, use wild cards to specify
filenames.
8.8.1 Examples for Searching
KDE and GNOME desktop store user-specific application data in hidden
directories, for example .kde and
.gnome.
-
To locate these directories on your computer, enter locate .kde
if you have installed KDE desktop or locate .gnome
if you have installed GNOME desktop.
You will see that locate displays all file names
in the database that contain the string .kde or
.gnome anywhere. To learn how to modify this behavior
refer to the man page of locate.
-
To search your home directory for all occurrences of filenames that
contain the file extension .txt, use find ~ -name '*.txt' -print
-
To search a directory (in this case, your home directory) for all
occurrences of files which contain, for example, the word
music, enter grep music ~/*
Note that grep is case-sensitive— unless you use it with the
-i option. With the command above you will not find any
files containing Music.
If you want to use a search string which consists of more than one
word, enclose the string in double quotation marks, for example: grep "music is great" ~/*
|
|
|