Bash: How to Delete All Files Without an Extension


Often you may want to use Bash to delete all files in a directory that do not have an extension.

You can use the following syntax to do so:

find . -type f ! -name "*.?*" -delete

This particular example will delete all files in the current directory that do not have an extension.

Here is what the various commands do:

  • find .: Match all items in current directory
  • -type f: Match only the files
  • ! -name “*.?*”: Match only files that do not have an extension
  • -delete: Delete all matched files

Note that you can also replace find . with find /some/directory/path to match all files in a specific directory.

Note: This syntax will also delete any files that end with a period ( . ) followed by no extension.

The following example shows how to use this syntax in practice.

Example: How to Delete All Files Without an Extension in Bash

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

  • c:/users/bobbi/data2

We can use the ls command to first view all items in this directory:

We can see that this directory contains the following items:

  • coaches.txt
  • managers
  • owners.txt
  • players.txt
  • rosters

Suppose that we would like to delete all of the files that do not have an extension.

We can use the following syntax to do so:

find . -type f ! -name "*.?*" -delete

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

Bash delete all files without an extension

Notice that when we use the ls command again to view the updated files in the directory we can see that all files that had no extension have been deleted.

Related Tutorials

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

Bash: How to Delete All Files Except Specific One
Bash: How to Delete All Files with Specific Extension
Bash: How to Delete Files Older than X Days

Leave a Reply