Bash: How to Find All Files with Specific Extension


Often you may want to use the find command in Bash to find all files with a specific extension.

You can use the following methods to do so:

Method 1: Find All Files with One Specific Extension

find . -name '*.txt'

This particular example will find all files in the current directory that have .txt as the file extension.

Method 2: Find All Files with One of Several Specific Extensions

find . -name '*.txt' -o -name '*.xlsx'

This particular example will find all files in the current directory that have either .txt or .xlsx as the file extension.

Note that the -o command represents “or” logic. Feel free to use as many -o commands as you would like to search for as many file extensions as you would like.

The following examples show how to use each of these methods in practice.

Example 1: Find All Files with One Specific Extension

Suppose that our current directory is located at the following path:

  • c:/users/bobbi/data1

We can use the ls command to view all files and folders in this directory:

We can see that this directory contains three folders.

Suppose that we would like to search through all of the folders in this directory recursively and find all files that have .txt as the file extension.

We can use the following syntax to do so:

find . -name '*.txt'

The following screenshot shows how to use this syntax in practice:

Bash find all files with extension

This returns all files in all subfolders of the current directory that contain .txt as the file extension.

Example 2: Find All Files with One of Several Specific Extensions

Suppose that we would like to search through all of the folders in our current directory recursively and find all files that have either .txt or .xlsx as the file extension.

We can use the following syntax to do so:

find . -name '*.txt' -o -name '*.xlsx'

The following screenshot shows how to use this syntax in practice:

Bash find all files with multiple extensions

This returns all files in all subfolders of the current directory that contain either .txt or .xlsx as the file extension.

Related Tutorials

The following tutorials explain how to perform other common tasks in Bash:

Bash: How to Use find -exec with grep
Bash: Use find and Exclude Specific Directory
Bash: How to List All Files in Folder Without Extension
Bash: How to Rename Extension of Multiple Files

Leave a Reply