How to Find All Files With an Extension in Bash

Sheeraz Gul Mar 11, 2025 Bash Bash Find
  1. Using the find Command
  2. Using the ls Command with grep
  3. Using Git to Find Files in a Repository
  4. Using Git Grep for Content and Extension
  5. Conclusion
  6. FAQ
How to Find All Files With an Extension in Bash

Finding files with specific extensions in Bash is a common task for developers and system administrators alike. Whether you’re managing a project, cleaning up directories, or simply searching for files, knowing how to efficiently locate files with particular extensions can save you a lot of time.

In this tutorial, we’ll explore several methods to find all files with a given extension in Bash, using Git commands when applicable. By the end, you’ll have a solid understanding of how to harness the power of Bash for file searching, making your workflow smoother and more efficient.

Using the find Command

The find command is one of the most powerful tools in Bash for locating files. It allows you to search for files recursively in a directory and filter results based on various criteria, including file extensions. Here’s how you can use it to find files with a specific extension.

find . -type f -name "*.txt"

Output:

example.txt
notes.txt

In this command, find starts searching in the current directory (denoted by .) and looks for files (-type f) that match the name pattern *.txt (all files ending with .txt). You can replace .txt with any extension you need. This command is particularly useful because it searches through all subdirectories as well, giving you a comprehensive list of all matching files.

Using the ls Command with grep

Another approach to finding files with specific extensions is to combine the ls command with grep. This method is handy when you want to filter the output of ls to show only files with certain extensions.

ls | grep '\.jpg$'

Output:

image1.jpg
photo2.jpg

In this command, ls lists all files in the current directory, and grep filters this list for files that end with .jpg. The \.jpg$ pattern ensures that only files with the .jpg extension are matched. This method is less comprehensive than find, as it does not search through subdirectories, but it can be quicker for simple tasks.

Using Git to Find Files in a Repository

If you’re working within a Git repository, you can leverage Git commands to find files with specific extensions. This is particularly useful for tracking changes in files or reviewing your project structure.

git ls-files '*.py'

Output:

script1.py
module.py

In this command, git ls-files lists all files tracked by Git that match the given pattern. The *.py argument specifies that you want to find all Python files. This command is incredibly efficient for developers, as it only returns files that are part of the current Git repository, helping you focus on relevant files.

Using Git Grep for Content and Extension

If you want to find files with a specific extension and also search for content within those files, you can use git grep. This command is particularly helpful when you need to locate files based on both their extension and their content.

git grep 'def ' -- '*.py'

Output:

script1.py:    def function_name():
module.py:    def another_function():

In this command, git grep searches through all .py files for the string def , which is commonly used to define functions in Python. The -- argument tells Git to treat everything after it as a pattern for file names. This method allows you to efficiently find both the files with a specific extension and the lines of code that contain the search term, making it a powerful tool for developers.

Conclusion

Finding files with specific extensions in Bash is a valuable skill that can streamline your workflow and enhance your productivity. Whether you choose to use the find command, combine ls with grep, or utilize Git commands, each method has its strengths and can be tailored to your specific needs. By mastering these techniques, you can quickly locate the files you need, making your development process more efficient and organized.

FAQ

  1. How can I find files with multiple extensions in Bash?
    You can use the find command with multiple -o (or) options, like this: find . -type f \( -name "*.txt" -o -name "*.md" \).

  2. Can I search for files in a specific directory using the find command?
    Yes, just replace . with the path to the directory you want to search in, like find /path/to/directory -type f -name "*.log".

  3. Is there a way to ignore case when searching for file extensions?
    Yes, you can use the -iname option with the find command, like this: find . -type f -iname "*.jpg".

  1. How do I find files by extension in a Git repository?
    Use the command git ls-files '*.ext', replacing .ext with your desired file extension.

  2. Can I execute a command on the found files directly?
    Yes, you can use the -exec option with find, like this: find . -type f -name "*.txt" -exec cat {} \;.

Enjoying our tutorials? Subscribe to DelftStack on YouTube to support us in creating more high-quality video guides. Subscribe
Author: Sheeraz Gul
Sheeraz Gul avatar Sheeraz Gul avatar

Sheeraz is a Doctorate fellow in Computer Science at Northwestern Polytechnical University, Xian, China. He has 7 years of Software Development experience in AI, Web, Database, and Desktop technologies. He writes tutorials in Java, PHP, Python, GoLang, R, etc., to help beginners learn the field of Computer Science.

LinkedIn Facebook

Related Article - Bash Find