The find command in Ubuntu Linux with examples



The article offers a thorough guide on using the find command in Ubuntu Linux. It demonstrates the usefulness of the command-line tool in locating files and directories based on specific criteria. It also elaborates on how to create complex search patterns, filter by file name, specify search range, exclude certain results, apply time-based criteria, and…

This article explains using the find command in Ubuntu Linux. It also provides examples you can use in your environment.

In Ubuntu Linux, the find command is a command-line tool that allows you to search for files and directories based on various criteria. It recursively searches the provided path, examining each file and directory to determine if it matches the given criteria.

With a wide range of options and expressions, you can create complex search patterns to identify the files you’re looking for precisely. Whether you’re a beginner or an experienced user, understanding how to use them effectively find command is essential for managing your files and optimizing your system.

Table of Contents

  1. Basic Syntax
  2. Searching for Files
  3. Filtering by File Name
  4. Search within a Specific Directory
  5. Exclude Directories or Files
  6. Combining Multiple Conditions
  7. Specifying Time-based Criteria
  8. Executing Commands on Found Files

1. Basic Syntax

The basic structure of the find command is as follows:

find [path] [expression]
  • [path] specifies the starting directory for the search. If not provided, it defaults to the current directory.
  • [expression] represents the conditions or filters applied to the search.

2. Searching for Files

To search for files using the find command, you can specify the -type option followed by the desired file type. For example, to find all the text files in the current directory and its subdirectories, you can use the following command:

find . -type f -name "*.txt"
  • . specifies the current directory as the starting point.
  • -type f indicates that only files should be considered, not directories.
  • -name "*.txt" specifies that the file name should end with .txt.

3. Filtering by File Name

The find command provides several options to filter files by their names. Here are some common examples:

  • -name "pattern": Matches files with the specified pattern in their names.
  • -iname "pattern": Performs case-insensitive matching.
  • -regex "pattern": Uses regular expressions for pattern matching.

4. Search within a Specific Directory

By default, the find command searches recursively within the provided directory and its subdirectories. However, you can also limit the search to a specific directory by using the -maxdepth or -mindepth options. For example:

find /path/to/directory -maxdepth 1 -type f -name "*.txt"
  • /path/to/directory is the directory you want to search within.
  • -maxdepth 1 ensures that only the files in the specified directory are considered, without descending into subdirectories.

5. Exclude Directories or Files

Using the -not or ! operator, you can exclude certain directories or files from the search results. For instance, to find all Java source code files excluding any files in the vendor directory, you can use:

find . -type f -name "*.java" -not -path "./vendor/*"
  • ./vendor/* is the path that needs to be excluded from the search results.

6. Combining Multiple Conditions

You can combine multiple conditions using logical operators like -and, -or, and -not. This enables you to create complex search patterns. Here’s an example that searches for files modified within the last 7 days and with a size greater than 1MB:

find . -type f -mtime -7 -size +1M
  • -mtime -7 matches files modified within the last 7 days.
  • -size +1M filters files larger than 1MB.

7. Specifying Time-based Criteria

The find command allows you to search files based on their timestamps. Here are some useful time-based options:

  • -atime: Access time (last access).
  • -mtime: Modification time (last content modification).
  • -ctime: Change time (last metadata change).
    You can use numeric values with these options to represent a specific timeframe.

8. Executing Commands on Found Files

Besides locating files, the find command can also execute actions on the found files. The -exec option allows you to specify a command to be executed. For example, to delete all the .tmp files within the current directory and its subdirectories, you can use:

find . -type f -name "*.tmp" -exec rm {} \;
  • rm {} is the command executed for each found file.
  • \; denotes the end of the command.

Remember, for more detailed information and examples, you can refer to the find command’s manual page (man find) or explore online resources dedicated to mastering the command.


This post showed you how to use the find command in Ubuntu Linux to find files and folders in directories. Please use the comments form below if you find errors or have something to add.

Like this:

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.