Kahibaro
Discord Login Register

2.2.5 Searching files

Locating Files Efficiently from the Command Line

Searching for files from the command line is a core skill that saves time and helps you understand how your system is organized. In this chapter you will see how to locate files and text content using standard Linux tools, without going into topics that belong to later chapters such as advanced scripting or system administration.

Searching by Name with `find`

The find command walks through directories and can select files based on many criteria. Its most frequent use at a beginner level is to search by name.

The basic structure in this context is:

find <start-directory> <conditions>

For example, to search for a file named notes.txt starting from your home directory, you can use:

find ~ -name "notes.txt"

Here ~ is your home directory, and -name specifies a pattern to match the filename. The pattern supports shell-style wildcards. If you want to match all .txt files under the current directory, run:

find . -name "*.txt"

The dot means “start searching from the current directory.” The double quote characters around the pattern protect the asterisk from being expanded by the shell before find sees it.

Linux filesystems are often case sensitive. If you need a case insensitive match, for example to find Report.pdf or report.PDF, you can use -iname:

find . -iname "report.pdf"

Important rule:
Use -name for case sensitive name searches and -iname for case insensitive searches. Always quote patterns that contain * or ?.

By default find will display the paths of all matching files and directories. This output can become long, so try to narrow the starting directory to something specific when possible.

Filtering by Type and Size

find can also filter by file type and basic attributes. This helps you avoid getting results you do not want, such as directories when you only want regular files.

To search only regular files, use -type f:

find . -type f -name "*.log"

To search only directories by name, such as directories called backup, use:

find . -type d -name "backup"

You can also search by size. For example, to list files larger than 10 megabytes starting from the current directory, you can use:

find . -type f -size +10M

The plus sign means “greater than.” Similarly, to find files smaller than 100 kilobytes, you can write:

find . -type f -size -100k

Size units commonly used are c for bytes, k for kilobytes, M for megabytes, and G for gigabytes.

Combining conditions is straightforward. find treats multiple conditions as a logical “and” when you place them one after another. For instance, to search for large .iso files in your home directory:

find ~ -type f -name "*.iso" -size +1G

This will only match entries that satisfy all three conditions at once: it must be a regular file, the name must end in .iso, and the size must be greater than one gigabyte.

Limiting Search Depth

Some directories contain many nested subdirectories. Searching the entire tree can take a lot of time and produce too much output. You can control how deep find descends with -maxdepth.

For example, to search only in the current directory and not in any subdirectories:

find . -maxdepth 1 -type f -name "*.sh"

Here -maxdepth 1 tells find not to go into subdirectories. If you wanted to go two levels deep, you could use -maxdepth 2.

Similarly, -mindepth can skip matches near the start. For instance:

find . -mindepth 2 -type d -name ".git"

This will ignore the current directory and its immediate children and only match .git directories at depth 2 or deeper under the starting point.

These depth options are especially useful in large trees such as system directories, where a full scan could be slow.

Running Commands on Found Files

Once find has selected some files, you often want to perform an action on them. find can run a command for each result with the -exec option.

The basic form is:

find <start> <conditions> -exec <command> {} \;

The {} placeholder represents the current file path, and \; ends the command. For example, to view detailed information about each .log file in the current directory tree, you can use:

find . -type f -name "*.log" -exec ls -l {} \;

If you need to delete files that match a pattern, such as temporary files ending in .tmp, you can run:

find . -type f -name "*.tmp" -exec rm {} \;

Be cautious with such commands. It is sensible to first run find without -exec and inspect the output. Once you are sure the list is correct, add the -exec to perform the action.

Some find versions support a more efficient variant that groups paths, using + instead of \;. For example:

find . -type f -name "*.log" -exec ls -l {} +

This runs fewer ls commands, with several paths at once. For simple uses in small directories, both forms are acceptable.

Important safety rule:
Always test a find search alone before adding -exec with commands that modify or delete files such as rm. Verify that the matches are exactly what you expect.

Searching File Indexes with `locate`

On many systems there is a command called locate that looks up file paths in a prebuilt index. This index is generated periodically by another tool, often updatedb, and stored in a database. Because it uses an index, locate is usually much faster than scanning with find, especially across the entire filesystem.

To search for filenames that include a pattern, you can simply run:

locate report.pdf

This will print all indexed paths that contain report.pdf anywhere in the pathname. You do not need to specify a starting directory.

If you are looking for all paths that contain the word bashrc, you might run:

locate bashrc

locate does not automatically know about files created or removed after the last index update. On some systems the index is updated daily by a scheduled job. On others, you may update it manually. If the command is available with the necessary permissions, you can run:

sudo updatedb

This rebuilds the database so that locate has current information. The exact details can differ slightly depending on your distribution.

locate is suited to quick searches by name. It does not inspect file size or other properties at search time. It also searches the entire database by default, so results may include system paths where you do not have access. You can combine it with tools such as grep if you want to filter the output further.

Searching Inside Files with `grep`

Sometimes you do not know the filename you want, but you know some of the text contained in the file. The standard tool for searching inside text files is grep.

A basic usage is:

grep "pattern" filename

This prints each line in filename that contains the given pattern. For example, to search for the word “error” in a log file called app.log, you can write:

grep "error" app.log

By default, grep is case sensitive. To make the search case insensitive use the -i option:

grep -i "error" app.log

grep can search multiple files at once. If you want to search all .log files in the current directory for “failed”, use:

grep "failed" *.log

For each matching line, grep will print the filename and the line that matches. If you only want to know which files contain a match, and not the actual lines, you can use -l:

grep -l "failed" *.log

This prints just the file names that contain at least one match.

When searching larger directory trees, you can combine grep with a recursive option. Many implementations support -r or -R for this purpose. For example, to search for “TODO” in all files under the current directory:

grep -r "TODO" .

This walks through subdirectories and searches every file it can read. For many source code trees and configuration directories this provides a quick way to find where something is defined or mentioned.

If you want to see the line numbers for each match, you can add -n:

grep -rn "TODO" .

The number printed before each line shows its position in the file, which can be helpful when you open the file in an editor.

Key pattern rule:
When the pattern contains shell characters such as spaces, , or ?, always quote it, for example "some text" or 'somepattern'. This prevents the shell from changing the pattern before grep runs.

Combining `find` and `grep`

find and grep can work together to form a powerful search. find selects the files, and grep searches within them. This is especially useful when you want to limit content searches to files that match certain criteria, such as file type or name pattern.

One common pattern uses -exec to call grep on each file. For example, to search for the word “DEBUG” only in .log files under the current directory:

find . -type f -name "*.log" -exec grep -H "DEBUG" {} \;

The -H option forces grep to show the filename with each match. This is useful when grep is called on one file at a time.

For many situations, an alternative combination with xargs is also used, but that belongs more naturally to later topics. As a beginner, using find with -exec and recursive grep is usually sufficient.

When you search configuration trees such as /etc, you might restrict the search to specific file types for performance and relevance. For example, to search for “Listen 80” in Apache configuration files with .conf extension:

find /etc -type f -name "*.conf" -exec grep -H "Listen 80" {} \;

This prevents grep from scanning binary or unrelated files and keeps your output manageable.

Basic Strategy for Searching Files

When you need to find something on a Linux system, a simple approach makes the choice of tool easier.

If you know the filename or part of it and want very fast results, and your system has locate, try locate first. If you need more control over which directories to search or want to filter by file attributes such as size or type, use find. If you know the text contained in the file but not the name, use grep and possibly combine it with find for more precise targeting.

Over time you will build combinations that suit your own way of working, but the techniques in this chapter offer a practical starting point for locating both files and content on a Linux system using the command line.

Views: 75

Comments

Please login to add a comment.

Don't have an account? Register now!