How to Recursively Find Files in Bash
This article is about the find command in Bash. The article will discuss ways to find a certain type of file using the find command in Bash.
Use the find Command to Find Files Recursively in Bash
A command-line tool for navigating the file hierarchy is the find command in Linux. It may be used to look up and monitor folders and files.
It allows for searching by name, creation date, modification date, owner, and permissions for files and folders.
It has the following syntax:
$ find [directory where to start searching] [-options] [name of file]
There can be the following attributes for options:
| Sr No | Option | Purpose |
|---|---|---|
| 1 | -links N |
It searches for the files with some no. of links specified. |
| 2 | -name |
Search for a file with a specified name or pattern. |
| 3 | -newer [filename] |
Search for files created after the filename. |
| 4 | -perm |
Search for all the files with specific permissions. |
| 5 | -print |
It is used to find and display the file’s complete path name. |
| 6 | -empty |
It searches for empty files or directories. |
| 7 | -size +N/-N |
It is used to search for a file with a specific size. If N is used as +N, it means files with a size greater than N; if it is used as -N, it means files with a size less than N. |
| 8 | -user |
It searches for the files with a specified owner name. |
Let us look at certain examples for the find command.
Search With the Filename
$ find ./mydir -name myfile.txt
This command will search the directory mydir for the filename myfile.txt.
Output:

Search With a Pattern
$ find ./mydir -name "*.jpeg"
This command will search the directory mydir for all the files with the .jpeg extension.
Output:

Search for Files With Permissions
$ find ./mydir -perm 777
This command will search the directory mydir for all the files with 777 permissions.
Output:

Find Files With Multiple Names or Patterns
There can be situations when you need to search for files with multiple patterns, like when you need to search for the files with the .txt and .jpg extensions.
$ find . -name '*.txt' -o -name '*.jpg'
You can use the -name option multiple times for such situations.
Output:

Search for Empty Files
$ find mydir -empty
This command will search for empty files or directories in mydir.
Output:
