Finding Files

team bbl


Linux has excellent file-finding utilities for both the desktop and the command line. On the KDE desktop, open the main menu and select Find Files. Or in Konqueror, click Tools and select Find Files. The screen in Figure 9-3 opens with blank fields.

Figure 9-3. File-find utility.


Typing *test* for the filename finds all files with test anywhere in the name, because * is a wildcard meaning any string of characters. Typing or browsing to /home/janet starts the search in the janet directory. "Include subdirectories" is checked. When you click the Find button, files display in the bottom pane. Four directories and four files are found. Two of the files and two of the directories are in the testing subdirectory.

Clicking the Contents tab displays a screen where you can select the type of file and the file contents to search for, such as a string such as "Hello World." Clicking the Properties tab allows you to search by last access date, file size, owner, or group. When you click Find, the parameters set in all three tabs are used in the search. GNOME has a search utility with very similar functionality, also accessed from the main menu.

From the command line, you can use the find command to find files by filename or properties and the grep command to find files by content. The find command syntax is:

 find pathname conditions  

pathname specifies the directory where the search should start. It's common to use dot to mean the current directory and subdirectories or / to search the entire file system. conditions sets the search parameters. Many conditions are available. You can use more than one in a command. Some useful conditions are shown in Table 9-1.

Table 9-1. Parameters for Use with the find Command

Parameter

Means

Example

-print

Display the matches on the screen.

-print

-name filename

Pattern to match filenames.

find . -name "*test*"

-group name

Files own by the specified group.

find . -group janet

-user name

Files owned by the user.

find . -user janet

-atime +n|-n|n

Last access time is more than, less than, or exactly n days ago.

find . -atime +5

-ctime +n|-n|n

Last changed time is more than, less than, or exactly n days ago.

find . -ctime -5

-size nc

Files n characters long.

find . -size 100c

-empty

Files that are empty.

-empty


For example, the following command searches the entire file system for all files beginning with chapter, owned by janet, and last accessed less than 3 days ago.

 find / -name "chapter*" -user janet -atime -3 

The grep command searches the contents of files. It has the general format:

 grep options pattern files 

grep searches the contents of the specified files for the pattern. It outputs any matches found. The options specify the operation of grep, such as -c, which instructs grep to output a count of matched lines, rather than the entire line, and -v, which outputs the lines that don't match, rather than the lines that do match. The pattern can be a string of characters or a regular expressiona pattern used to match strings, such as all words that begin with uppercase A or all lines that contain a number. Regular expressions are explained in Appendix A. The grep command is explained in more detail in Chapter 7 and in Appendix B.

    team bbl



    Spring Into Linux
    Spring Into Linux
    ISBN: 0131853546
    EAN: 2147483647
    Year: 2005
    Pages: 362
    Authors: Janet Valade

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net