Delete Files Except Certain Extensions
In this article
To delete all files in a folder except those with specific extensions, you use a command that filters the file list and removes everything that doesn't match. The exact method depends on your operating system, but the core idea is the same: list files, exclude the ones you want to keep, and delete the rest.
Why You Might Need This
Cleaning up a directory is a common task, but doing it manually is slow and error-prone. You might have a folder full of downloaded files where you only want to keep the PDFs, or a project directory where you need to remove all temporary files but preserve the source code. Deleting everything except certain extensions saves time and reduces the risk of accidentally removing the wrong file.
How It Works in Windows
Windows users can use Command Prompt or PowerShell. In Command Prompt, you can use a for loop with dir and findstr to filter out the extensions you want to keep, then delete the rest. For example, to delete all files except .txt and .docx in the current folder:
for %f in (*) do @echo %~xf | findstr /V /I ".txt .docx" >nul || del "%f"This loops through every file, extracts its extension, and if it doesn't match the list, deletes it. In PowerShell, the same task is more readable:
Get-ChildItem -File | Where-Object { $_.Extension -notin '.txt', '.docx' } | Remove-ItemThis gets all files, filters out those with the allowed extensions, and pipes the rest to Remove-Item.
How It Works on macOS and Linux
Unix-like systems use the find command with the -not and -name options. To delete all files except .jpg and .png in the current directory and its subdirectories:
find . -type f ! -name "*.jpg" ! -name "*.png" -deleteIf you only want to target the current directory (not subdirectories), add -maxdepth 1:
find . -maxdepth 1 -type f ! -name "*.jpg" ! -name "*.png" -deleteThis is recursive by default, so be careful: it will delete matching files in all subfolders.
Choosing the Right Extensions
Before running any delete command, list the extensions you actually want to keep. Think about hidden files, files with no extension, and case sensitivity. On Linux, *.JPG is different from *.jpg, so you may need to include both. Also consider whether you want to keep files like .gitignore or README that have no extension. In Windows, the findstr method is case-insensitive by default, but on Unix you must be explicit.
Common Problems and Mistakes
- Recursive deletion: The
findcommand without-maxdepthwill delete files in subfolders. Always double-check the directory you are in. - Hidden files: Files starting with a dot (like
.bashrc) are not matched by*in some shells. Usefindwith-name ".*"to include them. - Spaces in filenames: In Command Prompt, filenames with spaces need quotes. The
forloop above handles this, but a simpledelcommand might not. - Accidental deletion of allowed files: A typo in the extension list can wipe out files you meant to keep. Test with
echoinstead ofdelfirst.
Safety First: Dry Run and Backup
Before executing a destructive command, do a dry run to see what would be deleted. On Linux/macOS, replace -delete with -print to list the files. In PowerShell, replace Remove-Item with Select-Object FullName to preview. On Windows Command Prompt, use echo instead of del in the loop. Always have a backup if the files are important; deletion is permanent and does not go to the Recycle Bin.
Alternatives to Command Line
If you prefer a graphical interface, file managers like Windows Explorer or Finder allow you to sort by type and manually select files, but that becomes tedious for large folders. Third-party tools like Bulk File Changer or Advanced Renamer offer batch operations with filters, but they are not built into the OS. For a quick one-off, the command line is usually the fastest and most reliable.
Frequently Asked Questions
How do I delete files except a specific extension in Windows without Command Prompt?
You can use File Explorer's search bar: type ext:.txt to show only text files, then manually select the rest. But for large folders, a PowerShell command is more efficient.
Can I use wildcards like *.tmp to keep multiple extensions?
Yes, in the find command you can use ! -name "*.tmp" for each pattern. In PowerShell, you can use -notlike '*.tmp' or add multiple -notin entries.
What if I want to keep files with no extension?
On Unix, add ! -name "*.*" to keep files without a dot. In Windows, the findstr method will treat files with no extension as having an empty extension, so you can include a blank string in the filter.
Is there a way to undo a mistaken deletion?
If you used the command line, the files are gone permanently. The only way to recover is from a backup or a file recovery tool, but those are not guaranteed. Always test with a dry run first.
Does this work on files in subfolders?
On Unix, find is recursive by default; add -maxdepth 1 to limit to the current folder. In PowerShell, use -Recurse to include subfolders, but be careful. In Command Prompt, you would need to use /s with del, but the loop method above only works on the current directory.