Unix/Linux find
command “patterns” FAQ: How do I find files or directories that don’t match a specific pattern (files not matching a regex pattern, or filename pattern)?
In my case I just ran into a situation where I needed to find all files below the current subdirectory that are NOT named with the filename pattern *.html . Fortunately with the newer Unix/Linux find
syntax this solution is pretty easy, you just include the -not
argument, like this:
find . -type f -not -name "*.html"
That’s it. This Linux find
command using the “not” operator creates a list of all files not ending with the .html file extension (filename pattern).
Also, if you’re not familiar with it, the -f
argument in that find
command means “just look for files,” and don’t return search results for directories.
Find files not matching a filename pattern and doing something with them
Of course it’s usually not enough to find files not matching a filename pattern; usually you want to do something with them. Here’s how to run a simple Unix ls
command on them:
find . -type f -not -name "*.html" -exec ls -l {} \;
Summary: How to find files that don’t match a filename pattern
I hope this quick tip on finding Unix and Linux files and directories that don't match a filename pattern (not matching a pattern) has been helpful. For more information on the Linux find
command, here’s a link to my Linux ‘find’ command examples article.