Terminus
Find Files In Linux

Find Files In Linux

The short answer

In Unix-like operating systems such as Linux and macOS, to find all the regular files that match a specified pattern, you can use the [.inline-code]find[.inline-code] command as follows:

$ find <directory …> -type f -name "<pattern>"

Where:

  • [.inline-code]<directory …>[.inline-code] is a list of paths to the directories you want to perform a search in.
  • [.inline-code]-type f[.inline-code] is used to specify that the search should only include regular files.
  • [.inline-code]<pattern>[.inline-code] is the file name or pattern you are searching for.

For example, this command will list all the files with a [.inline-code].pdf[.inline-code] file extension located in the [.inline-code]~/Downloads[.inline-code] directory and its subdirectories:

$ find ~/Downloads -type f -name "*.pdf"

And this command will list all the files named [.inline-code]package.json[.inline-code] located in both the [.inline-code]app[.inline-code] and [.inline-code]api[.inline-code] directories:

$ find app api -type f -name "package.json"

[#easily-recall-syntax-with-ai] Easily retrieve this command using Warp’s AI Command Suggestions [#easily-recall-syntax-with-ai]

If you’re using Warp as your terminal, you can easily retrieve this command using the Warp AI Command Suggestions feature:

Entering the [.inline-code]linux find file[.inline-code] into the AI Command Suggestions will prompt a [.inline-code]find[.inline-code] command that can then be quickly inserted into your shell by doing [.inline-code]CMD+ENTER[.inline-code].

[#set-a-maximum-recursive-depth] Specifying the depth of the search [#set-a-maximum-recursive-depth]

By default, the [.inline-code]find[.inline-code] command will perform a recursive search in all the subdirectories of the specified directory.

To limit the search to a specific depth, you can use the [.inline-code]-maxdepth[.inline-code] flag as follows:

$ find <directory> -maxdepth <depth> -type f -name "<pattern>"

Where:

  • [.inline-code]<depth>[.inline-code] is an integer representing the maximum number of subdirectories the [.inline-code]find[.inline-code] command can traverse.

For example, this command will list all the files with a [.inline-code].js[.inline-code] file extension located in the [.inline-code]app[.inline-code] directory only:

$ find ./app -maxdepth 1 -type f -name "*.js"

[#filter-files-based-on-properties] Finding files based on properties [#filter-files-based-on-properties]

The [.inline-code]find[.inline-code] command offers a multitude of flags for finding and filtering files based on various properties such as their size, type, owner, and more.

Here is a shortlist of the most popular ones.

[#filter-by-type] Filtering files by type [#filter-by-type]

To find files based on their type, you can use the [.inline-code]-type[.inline-code] flag as follows:

$ find <directory> -type <type>

Where:

  • [.inline-code]<type>[.inline-code] is a character describing the file type, such as [.inline-code]c[.inline-code] for character device, [.inline-code]l[.inline-code] for symbolic link, [.inline-code]f[.inline-code] for regular file, [.inline-code]b[.inline-code] for block device, and [.inline-code]d[.inline-code] for directory.

For example, this command will recursively list all the regular files with a [.inline-code].log[.inline-code] file extension located in the current working directory:

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

[#filter-by-size] Filtering files by size [#filter-by-size]

To find files based on their size, you can use the [.inline-code]-size[.inline-code] flag as follows:

$ find <directory> -size <size>

Where:

  • [.inline-code]<size>[.inline-code] is an integer representing the desired file size, suffixed with an optional size indicator like [.inline-code]k[.inline-code] for kilobytes, [.inline-code]M[.inline-code] for megabytes, [.inline-code]G[.inline-code] for gigabytes, and so on. By default, the size is assumed to be expressed in bytes.

For example, this command will list all the regular files located in the current working directory with a size of about [.inline-code]20[.inline-code] megabytes:

$ find . -type f -size 20M

Additionally, you can prefix the [.inline-code]<size>[.inline-code] parameter with the minus character ([.inline-code]-[.inline-code]) to search for files with a lesser size:

$ find . -type f -size -20M

Or with the plus character ([.inline-code]+[.inline-code]) to search for files with a greater size:

$ find . -type f -size +20M

[#filter-by-owner] Filtering files by owner [#filter-by-owner]

To find files based on their owner, you can use the [.inline-code]-user[.inline-code] flag as follows:

$ find <directory> -user <user>

Where:

  • [.inline-code]<user>[.inline-code] is the username of the user that owns the file.

For example, this command will list all the files owned by the user named [.inline-code]durojaye[.inline-code] located in the [.inline-code]~/shared[.inline-code] directory:

$ find ~/shared -user durojaye

[#filter-by-group] Filtering files by group [#filter-by-group]

To find files based on their group, you can use the [.inline-code]-group[.inline-code] flag as follows:

$ find <directory> -group <group>

Where:

  • [.inline-code]<group>[.inline-code] is the name of the group that owns the file.

For example, this command will list all the files owned by the group named [.inline-code]developers[.inline-code] located in the [.inline-code]/bin[.inline-code] directory:

$ find /bin -group developers

[#filter-by-modification-date] Filtering files by modification date [#filter-by-modification-date]

To find files based on their latest modification date relative to a reference file, you can use the [.inline-code]-newer[.inline-code] flag as follows:

$ find <directory> -newer <reference>

Where:

  • [.inline-code]<reference>[.inline-code]  is the path to the file you want to use as a reference for the search.

For example, this command will list all the regular files that were modified after the [.inline-code]backup.tar.gz[.inline-code] file located in the current working directory:

$ find . -type f -newer backup.tar.gz

[#search-by-contents] Searching for files by contents [#search-by-contents]

To find files based on their contents, you can perform a pattern search using the [.inline-code]grep[.inline-code] command through the [.inline-code]find[.inline-code] command using the [.inline-code]-exec[.inline-code] flag as follows:

$ find <directory> -type f -exec grep -l '<pattern>' {} +

Where:

  • [.inline-code]grep -l[.inline-code] is used to output the names of the files containing the specified pattern.
  • [.inline-code]{}[.inline-code] is a placeholder that represents the list of files found by the [.inline-code]find[.inline-code] command.
  • [.inline-code]+[.inline-code] is used to tell the [.inline-code]find[.inline-code] command that the [.inline-code]grep[.inline-code] command should process multiple files at once.

For example, this command will list all the regular files that contain the word [.inline-code]network[.inline-code] located in the [.inline-code]/etc[.inline-code] directory:

$ find /etc -type f -exec grep -l 'network' {} +