Linux find Command: Syntax, Options, Examples

August 29, 2024

Introduction

The find command in Linux is a powerful tool used to search for files and directories within a specified path based on different criteria. Moreover, it allows users to locate files by name, type, size, permissions, and more, making the tool essential for file management and system administration.

This tutorial shows you how to locate and manage files in Linux with the find command.

Linux find Command: Syntax, Options, Examples

Prerequisites

  • A Linux system (this tutorial uses Ubuntu 22.04).
  • Access to the terminal.
  • A user account with sudo or root privileges

find Command Syntax

The basic find command syntax uses the following format:

find [options] [path] [expression]
  • Options. Customize the find output.
  • Path. Instructs find where to start looking for the search term.
  • Expression. Tells find what information category it's supposed to look for.

find Command Options

The find command has many options that modify how it works. The following table presents common find options:

OptionsDescription
-nameSearches for files or directories by name (case-sensitive).
-prune<br>Follows symbolic links. The command operates on the files or directories that the links point to rather than the links themselves.
-notNegates a condition.
-emptySearches for empty files and directories.
-mindepthStarts searching only after a certain directory depth.
-maxdepth<br>Limits the search to a specific depth.
-printDisplays the full file path of each match. This is the default action if no other action is specified.
-execExecutes a command on each file found.
-permSearches for files with specific permissions.
-groupFinds files owned by a specific group.
-user<br>Delivers files owned by a specific user.
-ctimeSearches for files with their status changed within a certain number of days.
-atimeDelivers files accessed within a certain number of days.
-mtimeSearches for files modified within a certain number of days.
-sizeFinds files of a specific size.
-type<br>Searches for files of a specific type (f for files, d for directories, l for symbolic links).
-inameFinds files or directories by name (case-insensitive).
-LFollows symbolic links. The command operates on the files or directories that the links point to, rather than the links themselves.
-HFollows symbolic links in the command itself but not the links encountered while searching.
-PDefault behavior. Does not follow symbolic links. The search does not resolve or act on the files or directories the links point to.

find Command optimization levels

The find command has three optimization levels. An optimization level in the find command determines how search operations are prioritized and executed for efficiency. Therefore, the command affects the order in which different search types (such as by name or type) are performed, impacting the overall speed and performance of the search process.

Higher optimization levels aim to maximize search speed, while lower levels focus on specific criteria first. The optimization levels are:

Optimization LevelDescription
-O1Performs searches based on file names first. The default optimization level.
-O2Runs searches based on type immediately after name-based searches, optimizing for type-based queries.
-O3Maximizes search speed regardless of other search criteria, optimizing overall performance.

find Command Examples

The find command is a versatile tool in Linux for locating files and directories based on various criteria. The following sections present practical examples demonstrating how to use find to search by file name, type, size, and more.

Find Files by Name

The find command allows you to locate files by their name and quickly identify specific files within a directory hierarchy.

To find a file by name, use the -name option followed by the file's name in quotes. This searches the specified directory (and its subdirectories) for files that match the given name.

For example, find a file named test1.txt in your home directory with:

find ~ -name "test1.txt"
find ~ -name "test1.txt" terminal output

This command searches the current user's directory (~) for any file named test1.txt

Find Files by Approximate Name

To find files with names that approximately match a given pattern, use the find command combined with wildcards (*). This is useful when you're unsure of the exact file name or if you're looking for files that share a common naming pattern.

For example, to search for text files with the term test, run:

find ~ -name "test*.txt"
find ~ -name "test*.txt" terminal output

Find Files by Extension

To search for files with a specific extension, use the find command with the -name option, followed by a pattern that includes the desired file extension.

For example, find all .txt files within the /Documents directory with:

find /home/sara/Documents -name "*.txt"
find /home/sara/Documents -name "*.txt" terminal output

Find Files and Directories by Type

The find command allows you to search for files or directories based on their type. Additionally, by using the -type option, you specify whether you're looking for regular files, directories, symbolic links, or other types of files.

The file types are:

  • f - Regular files.
  • d - Directories.
  • l - Symbolic links.

For example, to search for all directories within the /home/sara directory, use:

find /home/sara -type d

This command lists all directories in /home/sara and its subdirectories. However, to make it easier to read, pipe the command to less:

find /home/sara -type d | less
find /home/sara -type d | less terminal output

Find Files by Location

When searching for files based on their location, the find command allows you to specify a directory path. The command searches search through that directory and its subdirectories. If you do not specify a filename, the command returns a list of all files and directories within the specified path.

For example, search for all files and directories across the entire hard drive with the following command:

sudo find /

The sudo command lets you see files that are available only with administrative privileges. Pipe the command to less to make it easier to read:

sudo find / | less
sudo find / | less terminal output

To search the directory you're currently in, use a period (.).

sudo find . |less
sudo find . |less terminal output

Use the tilde (~) character to search the current user's home directory:

sudo find ~ |less
sudo find ~ |less terminal output

Search all user directories under /home for a file named test1.txt with:

sudo find /home -name "test1.txt"
sudo find /home -name "test1.txt" terminal output

To find the file test1.txt specifically within the sara user home directory, run:

sudo find /home/sara -name "test1.txt"
sudo find /home/sara -name "test1.txt" terminal output

Find and Delete File

Use the find command to locate files and delete them in one step. This action is useful for cleaning up files that match specific criteria, such as old log files or temporary files.

For example, delete file1.txt file with:

find /home/sara/Documents -name "file1.txt" -delete

The command has no output.

Find Empty Files and Directories

Use the find command to search for empty files and directories on your system. The -empty option is used to locate files and directories that have no content. For example, search for all empty files within the <em>/home/sara</em> directory with:

find /home/sara/ -type f -empty
find /home/sara/ -type f -empty terminal output

Find by Content

To search for files based on their content, use the grep command in combination with find. The grep command searches for specific text within files while find locates the files to be searched.

For example, find all files within the /home/sara/Documents directory that contain the text example with:

find /home/sara/Documents -type f -exec grep -l "example" {} +
find /home/sara/Documents -type f -exec grep -l "example" {} + terminal output

The command consists of:

  • find. Searches for files and directories.
  • /home/sara/Documents. Represents the directory where the search begins. The find command recursively searches through this directory and its subdirectories.
  • -type f. Restricts the search to regular files only.
  • -exec. Allows you to execute a command on each file that find matches. In this case, it is used to run grep on each file.
  • grep. Searches for text patterns within files.
  • -l. Instructs grep to list only the names of files that contain the specified text rather than displaying the matching lines.
  • "example". Represents the text string you are searching for within the files.
  • {}. Is the placeholder replaced by each file name that find matches. grep runs on these file names.
  • +. Ends the -exec command and indicates find should pass multiple filenames to grep at once, rather than invoking grep separately for each file. This is more efficient than running grep for each file individually.

Find by Size

Use the find command to search for files based on their size. The -size option allows you to specify file size criteria for the search. Sizes are:

For example, find files larger than one megabyte in the /home/sara/ directory:

find /home/sara/ -type f -size +1M

find /home/sara/ -type f -size +1M terminal output

Search Files with Specific Permissions

Use the find command to locate files based on their permissions. The -perm option allows you to specify permission criteria for the search. Also, the option goes with different permission criteria. Permission criteria in the find command specify the exact or relative permission settings to search for files.

Permissions criteria are:

  • Exact permissions. Specifies exact permission bits.
  • Permissions with a plus sign (+). Matches files with at least the specified permissions.
  • Permissions with a minus sign (-). Matches files with at most the permissions you specify.

For example, find files with exact permissions of 644 (read and write for owner, read-only for group and others) in the /home/sara directory:

find /home/sara -type f -perm 644 |less
find /home/sara -type f -perm 644 |less terminal output

Conclusion

This article explained how the find command helps you search and locate files and directories. It explained the wide range of search criteria, including file names, types, sizes, permissions, and modification times that the command supports.

If you want to find files and directories quickly, learn how to use the locate command in Linux.

Was this article helpful?
YesNo
Sara Zivanov
Sara Zivanov is a technical writer at phoenixNAP who is passionate about making high-tech concepts accessible to everyone. Her experience as a content writer and her background in Engineering and Project Management allows her to streamline complex processes and make them user-friendly through her content.
Next you should read
How To Check If File or Directory Exists in Bash
November 29, 2023

Searching for specific files or directories can be time-consuming. You can use a bash command or script to...
Read more
How to Copy Files and Directories in Linux
December 28, 2023

Want to learn how to copy files in Linux OS? This guide will show you how to use the Linux commands to copy...
Read more
How to Check CPU Utilization in Linux with Command Line
March 6, 2024

You have probably noticed your Linux OS slowing down, especially when working harder. Understanding CPU...
Read more
How to Install MySQL 8.0 in Ubuntu 18.04
December 12, 2018

MySQL is an open-source relational database server tool for Linux operating systems. It is widely used in...
Read more