Find Command in Linux [With Useful Examples]

Find Command in Linux

The find command in the Linux shell allows you to search for files in the filesystem. It can find files by name, user permissions, and size. The find command can also perform actions on the files which are found.

Find Command Syntax

The syntax for the find command is as follows:

find [OPTIONS] [PATH] [EXPRESSION]

Where:

  • [OPTIONS] are options from the below table to determine the find behavior
  • [PATH] is the starting point for the search
  • [EXPRESSION] defines the tests to find matching files and any action that should be taken on the found files

Options

Here are the commonly used OPTIONS for the find command, adapted from the manual:

-P Never follow symbolic links. This is the default behavior
-L Follow symbolic links. When find examines or prints information about files, the information used shall be taken from the properties of the file to which the link points, not from the link itself
-H Do not follow symbolic links, except while processing the command line arguments

Expressions

These some common statements from the find manual that can be used to build the EXPRESSION, which tests for matching files and optionally performs actions on them:

Tests (Search conditions)
-empty Find empty files
-executable Matches files that are executable and searchable directories
-group name File belongs to group with name
-mtime n Files data was last modified less than, more than, or exactly n*24 hours ago
-name pattern Base of file name (the path with the leading directories removed) matches pattern pattern
-path pattern File path matches pattern pattern
-readable Matches files which are readable by the current user
-size n File uses less than, more than or exactly n units of space, rounding up (See below table for available units)
-type c File is of type c (See below table for values for c)
-user name File is owned by name user
Actions
-print Outputs the name of the file (Default action)
-prune Ff the file is a directory, do not descend into it
-depth Process each directory’s contents before the directory itself
-delete Delete matched files. The -delete action also acts like an option (since it implies -depth)
-exec command Execute command
-execdir command Like -exec, but the specified command is run from the subdirectory containing the matched file
-ok command Like -exec but ask the user first.
-okdir command Like -execdir but ask the user first in the same way as for -ok.
-printf format Print file details in format

Operators in Expressions

Operators can be used to chain together items in an expression:

-o OR
-a AND (Default operator between expressions when no operator supplied

For the full list of options and expressions for finding files, you can run the following:

man find

Find Command Examples

Options, expressions, and actions can be combined to create simple find operations to list matching files or complex operations that can find files by complex conditions and perform an action. See below for examples:

Find Files by File Name

This example finds all files named myfile in the directory /home/screw:

find /home/screw -name myfile

Search By Type

The above command will return all matching files – including directories, links, and devices – everything in the file system.

The -type f expression limits the search to files only. To search for files named myfile:

find /home/screw  -type f -name myfile

Or, search only for directories:

find /home/screw  -type d -name myfile

The available types are:

b Block (buffered) special
c Character (unbuffered) special
d Directory
p Named pipe (FIFO)
f Regular file
l Symbolic link; this is never true if the -L option or the -follow option is in effect, unless the symbolic link is broken
s Socket

Case Sensitivity

The -name option is case sensitive – to search without case sensitivity use -iname:

find /home/screw -iname mYFiLe

Find Files By Extension Using Wildcards & Regex

You can use wildcards (*) in file names, which allows you to search by file extension:

find -L /home/screw -name '*.pdf'

The above will search for all files with the .pdf extension. The quoted search pattern can contain regex.

Omitting Files By Name Or Extension

The -not operator can be used to omit files by name or pattern, returning all results that do not match:

find /home/screw -type f -not -name '*.pdf'

Omit Entire Directories from Results

Use the -prune option to omit matching paths and their contents. The below example will omit the /home/screw/junk directory from the results:

find /home/screw -path /home/screw/junk -prune -o -print

Search for Files by Size

This example searches for all files less than 500 Kilobytes:

find /home/screw -type f -size -500k

This one will find files between 100 and 200 Kilobytes by passing two size conditions:

find /home/screw -type f -size +100k -size -200k

The available units for searching by size are:

b 512-byte blocks (this is the default if no suffix is used)
c Bytes
w Two-byte words
k Kibibytes (KiB, units of 1024 bytes)
M Mebibytes (MiB, units of 1024 * 1024 = 1048576 bytes)
G Gibibytes (GiB, units of 1024 * 1024 * 1024 = 1073741824 bytes)

Modification or Access Date

The following example finds all .txt files modified in the last 14 days:

find /home/screw -name '*.txt' -mtime 14

To search by last access instead of modification date, use the -atime option instead of -mtime.

You can also add a sign (+ or ) to search for files modified greater or less than the given interval – this example will return files that were modified greater than 14 days ago.

find /home/screw -name '*.txt' -mtime +14

To use minutes instead of days as the unit of time, you can substitute -mmin -amin instead of -mtime and -atime.

As usual, there are a lot more options to mix and match, all available in the manual, available by running:

man find

Owner User/Group

To find all files owned by root in the /home/screw directory, run:

find /home/screw -user root

Permissions

The following command will find all files with global read/write permissions:

find /home/screw -perm 777

See our article on the chmod command and what it does.

Combination

Any combination of search expressions can be used. If no operator is included between them, it will be assumed that both conditions must be met for a file to be a match (i.e., the AND operator is the default).

This example searches for all text files created in the last 14 days less than 500kb:

find /home/screw -name '*.txt' -mtime 14 -type f -size -500k

Actions on Found Files

By default, the find command will print the find operation results (the same as calling the -print action).

find /home/screw -name myfile -print

Customizing the Results Format

You can customize the output of the results to include additional information about any found files. This example prints the file name, followed by a dash, followed by the size of the file in bytes:

find /home/screw -name '*.txt' -printf '%f - %s bytes'

Many variables can be used to customize the output, from the file size to individual date fields for the file modification and access times – check out the manual for the full list.

Writing Results to File

Redirect the results of your find operation to a text file:

find /home/screw -name myfile > find_results.txt

Deleting

The -delete action will delete matching files (without confirmation, so be careful!):

find /home/screw -name '*.junk' -delete

Executing commands with -exec / *-execdir *

You can execute any shell command against each find result using the -exec action:

find /home/screw -name '*.txt' -exec ls -la {} ';'

-exec will execute the command from the current working directory. To execute the command from the location of the found file, use -execdir instead:

find /home/screw -name '*.txt' -execdir ls -la {} ';'

In the above two examples, ls -la is run against each result. The curly braces {} will be replaced with the find command results, and the ‘;’ terminates the command line arguments for the ls command so that find knows where they end.

Prompting for Confirmation with -ok / -okdir

The -ok action works the same as the -exec action, but it asks you before running the given command. This is useful if you want to delete files, but be asked before each file is deleted:

find /home/screw -name '*.txt' -ok rm {} ';'

-okdir also prompts but executes the command from the location of the found file:

find /home/screw -name '*.txt' -okdir rm {} ';'

 

SHARE:
nv-author-image

Brad Morton

I'm Brad, and I'm nearing 20 years of experience with Linux. I've worked in just about every IT role there is before taking the leap into software development. Currently, I'm building desktop and web-based solutions with NodeJS and PHP hosted on Linux infrastructure. Visit my blog or find me on Twitter to see what I'm up to.

Leave a Reply

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