Find Files In Linux

Durojaye Olusegun
Published: May 6, 2024

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 find command as follows:

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

Where:

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

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

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

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

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

Easily retrieve this command using Warp’s AI Command Suggestions

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

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

By default, the find 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 -maxdepth flag as follows:

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

Where:

  • <depth> is an integer representing the maximum number of subdirectories the find command can traverse.

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

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

Finding files based on properties

The find 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.

Filtering files by type

To find files based on their type, you can use the -type flag as follows:

Bash
$ find <directory> -type <type>

Where:

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

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

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

Filtering files by size

To find files based on their size, you can use the -size flag as follows:

Bash
$ find <directory> -size <size>

Where:

  • <size> is an integer representing the desired file size, suffixed with an optional size indicator like k for kilobytes, M for megabytes, G 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 20 megabytes:

Bash
$ find . -type f -size 20M

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

Bash
$ find . -type f -size -20M

Or with the plus character (+) to search for files with a greater size:

Bash
$ find . -type f -size +20M

Filtering files by owner

To find files based on their owner, you can use the -user flag as follows:

Bash
$ find <directory> -user <user>

Where:

  • <user> is the username of the user that owns the file.

For example, this command will list all the files owned by the user named durojaye located in the ~/shared directory:

Bash
$ find ~/shared -user durojaye

Filtering files by group

To find files based on their group, you can use the -group flag as follows:

Bash
$ find <directory> -group <group>

Where:

  • <group> is the name of the group that owns the file.

For example, this command will list all the files owned by the group named developers located in the /bin directory:

Bash
$ find /bin -group developers

Filtering files by modification date

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

Bash
$ find <directory> -newer <reference>

Where:

  • <reference>  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 backup.tar.gz file located in the current working directory:

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

Searching for files by contents

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

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

Where:

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

For example, this command will list all the regular files that contain the word network located in the /etc directory:

Bash
$ find /etc -type f -exec grep -l 'network' {} +
Written by
Durojaye Olusegun
Filed under

Related articles


Bash Comments

Comments will help make your scripts more readable

Reading User Input

Via command line arguments and prompting users for input

Curl Post Request

Use cURL to send data to a server

Upload Files With curl

Learn how to upload a file to FTP, SFTP servers, Artifactory, and AWS S3 using the curl command.

How To Copy A Directory In Linux

Learn how to copy directories and their content in Linux using the cp command with options like -r for recursive copying, -i for interactive mode, and -a for preserving attributes.

Create Groups In Linux

Learn how to manually and automatically create and list groups in Linux.

How to Check the Size of Folders in Linux

Learn how to output the size of directories and subdirectories in a human-readable format in Linux and macOS using the du command.

Count Files in Linux

Learn how to count files and folders contained in directories and subdirectories in Linux using the ls, find, and wc commands.

List Open Ports in Linux

Learn how to output the list of open TCP and UDP ports in Linux, as well as their IP addresses and ports using the netstat command.

Format Command Output In Linux

Learn how to filter and format the content of files and the output of commands in Linux using the awk command.

Create Directories Recursively With mkdir

Learn how to recursively create nested directories using the mkdir command, Bash scripts, and Python scripts.

Remover Users in Linux

Learn how to remove local and remote user accounts and associated groups and files in Linux using the userdel and deluser commands.