A common command to use in filtering objects in the file system is known as “find”. This can sort through the file system by utilizing a simple conditional mechanism. Find is useful when searching for a file or directory on your file system.  Also, when using the -exec flag, files are able to be found quickly and immediately processed inside the same command.

Search Linux Files by Name or Extension

When using find, type it into the command line to locate a specific file by name or extension. The following example searches for *.err files in the /home/username/ directory and all sub-directories. Please use the example as follows:

find /home/username/ -name “*.err”

Common Linux Find Commands and Syntax

Common find expressions take the following standard form: find options starting/path expression.

  • The options attribute will control the behavior and optimization method within the find process.
  • The starting/path attribute will describe the highest-level directory where find begins filtering.
  • The expression attribute controls the tests that examine the directory hierarchy to produce an output.

From the notes above, consider the following example command: find -O3 -L /var/www/ -name “*.html”

This command permits the maximum optimization level (-O3) and allows find to follow symbolic links (-L). The find command searches the entire directory tree beneath /var/www/ for files that end with .html.

Practical Examples

  1. Command: find . -name testfile.txt

Description: Find a file called testfile.txt in current and sub-directories.

  1. Command: find /home -name *.jpg

Description: Find all .jpg files in the /home and sub-directories.

  1. Command: find . -type f -empty

Description: Find an empty file within the current directory.

  1. Command find /home -user exampleuser -mtime 7 -iname “.db”

Description: Find all .db files (ignoring text case) modified in the last 7 days by a user named exampleuser.

Options and Optimization for Find

The default configuration for find will disregard symbolic links, known as shortcut files. If you want find to follow and return symbolic links, then you must add the -L option to the command, which is shown in the example above.

The find command improves the filtering strategy to boost performance. Three user-selectable optimization levels are specified as -O1, -O2, and -O3. The -O1 optimization is the default and forces find to filter filename before running all other tests.

Optimization at the -O2 level focus file name filters, as in -O1, and then runs all file-type filtering before proceeding with more resource-intensive conditions. Level -O3 optimization allows find to perform the most severe optimization and reorders all tests based on their relative expense and the possibility of their success.

  • Command: -O1

Description: (Default) filter based on file name first.

  • Command: -O2

Description: File name first, then file-type.

  • Command: -O3

Description: Allow find to automatically re-order the search based on efficient use of resources and likelihood. of success

  • Command: -maxdepth X

Description: Search current directory as well as all sub-directories X levels deep.

  • Command: -iname

Description: Search without regard for text case.

  • Command: -not

Description Return only results that do not match the test case.

  • Command: -type f

Description: Search for files.

  • Command: -type d

Description: Search for directories.

Find Files by Using Modification Time

The find command allows you to filter a directory hierarchy based upon when the file was last revised:

find / -name “*conf” -mtime 7

find /home/exampleuser/ -name “*conf” -mtime 3

The first command provides a list of all files in the file system that end with the characters “conf” and have been changed within the past week. The other command filters “exampleuser” user’s home directory for files with names that end with the characters “conf” and have been changed in the past 3 days.

Use Grep to Find Files Based on Content

The find command can only filter the directory hierarchy based on a file’s name and meta data. If you must search based on the content of the file, use a tool like grep. Here is an example:

find . -type f -exec grep “example” ‘{}’ \; -print

This searches every object in the current directory hierarchy (.) that is a file (-type f) and then runs the command grep “example” for all the files that fit the conditions. The files that match are printed on the screen (-print). The curly braces ({}) are a placeholder for the find match results. The {} are enclosed in single quotes (‘) to avoid handing grep a malformed file name. The -exec command is terminated with a semicolon (;), which should be escaped (\;) to avoid interpretation by the shell.

How to Discover and Process Files Using the Find Command

The -exec option runs commands against every object that fits the parameters of the find expression. Please consider the following example:

find . -name “rc.conf” -exec chmod o+r ‘{}’ \;

This filters every object in the current hierarchy (.) for files named rc.conf and runs the chmod o+r command to change file permissions of the find results.

The commands run with the -exec are performed in the root directory of the find process. Use the -execdir to execute the stated command in the directory where the match is found. This may lessen any security concerns and create more desirable performance for some of the operations.

The -exec or -execdir options will run without any further prompts. If you would rather be prompted before an action is taken, replace -exec with -ok or -execdir with -okdir.

How to Find and Delete Files in the Linux Command Line

A word of warning: Use this option with extreme caution!

Add the option -delete to the end of a match expression to delete all files that match. Use this option when you are certain that the results only match the files that you wish to delete.

In the following example, find will locate all files in the hierarchy starting at the current directory and then fully recursing into the directory tree. In this example, find will delete all files that end with the characters .bak:

find . -name “*.bak” -delete