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:
$ find <directory …> -type f -name "<pattern>"
Run in Warp
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:
$ find ~/Downloads -type f -name "*.pdf"
Run in Warp
And this command will list all the files named package.json located in both the app and api directories:
$ find app api -type f -name "package.json"
Run in Warp
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.
Specifying the depth of the search
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:
$ find <directory> -maxdepth <depth> -type f -name "<pattern>"
Run in Warp
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:
$ find ./app -maxdepth 1 -type f -name "*.js"
Run in Warp
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:
$ find <directory> -type <type>
Run in Warp
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:
$ find . -type f -name "*.log"
Run in Warp
Filtering files by size
To find files based on their size, you can use the -size flag as follows:
$ find <directory> -size <size>
Run in Warp
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:
$ find . -type f -size 20M
Run in Warp
Additionally, you can prefix the <size> parameter with the minus character (-) to search for files with a lesser size:
$ find . -type f -size -20M
Run in Warp
Or with the plus character (+) to search for files with a greater size:
$ find . -type f -size +20M
Run in Warp
Filtering files by owner
To find files based on their owner, you can use the -user flag as follows:
$ find <directory> -user <user>
Run in Warp
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:
$ find ~/shared -user durojaye
Run in Warp
Filtering files by group
To find files based on their group, you can use the -group flag as follows:
$ find <directory> -group <group>
Run in Warp
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:
$ find /bin -group developers
Run in Warp
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:
$ find <directory> -newer <reference>
Run in Warp
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:
$ find . -type f -newer backup.tar.gz
Run in Warp
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:
$ find <directory> -type f -exec grep -l '<pattern>' {} +
Run in Warp
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:
$ find /etc -type f -exec grep -l 'network' {} +
Run in Warp
Written by
Durojaye Olusegun
Filed Under
Related Articles
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.
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.
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.
Linux Chmod Command
Understand how to use chmod to change the permissions of files and directories. See examples with various chmod options.
POST JSON Data With Curl
How to send valid HTTP POST requests with JSON data payloads using the curl command and how to avoid common syntax pitfalls. Also, how to solve the HTTP 405 error code.
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 Groups In Linux
Learn how to manually and automatically create and list groups in Linux.
Switch Users In Linux
Learn how to switch between users, log in as another user, and execute commands as another user in Linux.
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.
Delete Files In Linux
Learn how to selectively delete files in Linux based on patterns and properties using the rm command.
Copy Files In Linux
Learn how to safely and recursively copy one or more files locally and remotely in Linux using the cp and scp command.
Create Files In Linux
Learn how to create regular files in Linux using command line interface commands like touch, echo, cat, printf, and in-terminal text editors like Vim.