Friday, February 18, 2022

[SOLVED] How to remove files without certain extension?

Issue

How to remove all files without the .txt and .exe extensions recursively in the current working directory? I need a one-liner.

I tried:

find . ! -name "*.txt" "*.exe" -exec rm -r {} \
find -type f -regextype posix-extended -iregex '.*\.(txt|exe)$'

Solution

Try this.

find . -type f ! -name "*.exe" ! -name "*.txt" -exec rm {} \;

The above command will remove all the files other than the .exe and .txt extension files in the current directory and sub directory recursively.



Answered By - sureshkumar
Answer Checked By - Robin (WPSolving Admin)