Often you may want to use Bash to list all files in a directory that do not match a specific pattern.
You can use the following syntax to do so:
ls -I '*.txt'
This particular example will list all files in the current directory that do not have .txt as the file extension.
Note that the ls statement is used to list all files and the -I parameter is used to specify patterns to “ignore” when listing the files.
The following example shows how to use this syntax in practice.
Example: How to List All Files Not Matching a Pattern in Bash
Suppose that we would like to list all files in our current directory that do not match .txt as the file extension.
We can first use the ls statement to view all files in our current directory:

We can see that our current directory has eight total files with various file extensions.
We can use the following syntax to only list the files that do not match .txt as the file extension:
ls -I '*.txt'
The following screenshot shows how to use this syntax in practice:

Note that this returns the following files:
- locations.xlsx
- records.csv
- regions.xlsx
- schedules.csv
Notice that each of these files do not match .txt as the file extension.
Feel free to replace ‘*.txt’ with any specific pattern that you would like to ignore when listing files.
Related Tutorials
The following tutorials explain how to perform other common tasks in Bash:
Bash: How to List All Files Matching a Specific Pattern
Bash: How to List All Files in Folder Without Extension
Bash: How to List All Files in Folder by Size
